User control and dialog box VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • truezplaya
    New Member
    • Jul 2007
    • 115

    User control and dialog box VB.NET

    I am working on a project that has a usercontrol lets say usercontrol1, and dialog box (dialogbox1)
    in the usercontrol there is a list of cars with several other bits of info foe example, registration, colour etc etc, There is also an add button.

    When the user clicks on the add button the dialogbox1 opens.

    Dialogbox1 has several text boxes, This is to input a new car and the cars details. When they click on the add button i add this info to a database and close the form.

    what i want to do is.....
    Once the user clicks on the add button in the dialogbox1 is store the data to a datatable or a list and pass this back to the user control so i can append the item to the list. I think this is possible.

    Other ways of doing it.
    I could just repopulate the list from the database but this could cause the system to slow but i also do not know where (which event) i would put the call to repopulate the list

    What i am having trouble with....
    Passing the data from the dialog box1 to the usercontrol.

    What i have so far ...
    pretty sure that this gathers the data, now all i need to do is send this to the usercontrol on dialogbox1.clos e
    Code:
    Public Shared Function collectAppendingRow() As DataTable
                Dim tmpList As New List(Of SortData.carInfo)
                Dim tmpTbl As DataTable = Nothing
    
                tmpTbl.Rows.Item(0)(0) = Dialog1.MakeTextBox.Text
                tmpTbl.Rows.Item(0)(1) = Dialog1.ModelTextBox.Text
                tmpTbl.Rows.Item(0)(2) = Dialog1.ColourTextBox.Text
                tmpTbl.Rows.Item(0)(3) = Dialog1.RegTextBox.Text
                tmpTbl.Rows.Item(0)(4) = Dialog1.SpecTextBox.Text
                
    
    
                Return tmpTbl
    
            End Function
    i am using visual studio 2008
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    So, it sounds likes your user control is not databound to your database and what you want to do is upon clicking this "Add" button, open a dialog for adding a new car and when that dialog is closed, add that data to both the database and to your usercontrol? Is that right?

    Is there any reason why you can't add a method to your UserControl called AppendItem or AddCar or something that takes a Car object or the seperate bits of information from the dialogs textboxes, i.e. Name, Model, Make as parameters?
    You can the put some read-only properties into your custom dialog to provide access to the new cars information (or just change the modifiers of the textboxes on the form to make them visible outside of the form - looking at your sample it looks like this is what you have done at the moment?) and then put code similar to the following in the event handler for the "Add" buttons Click event?


    Code:
        
    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
            Dim frmMyDialog As Form ' <-- or your custom dialog type
            If frmMyDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then ' user clicked ok button
                ' Call user controls AppendItem method
                .AppendItem(frmMyDialog.NameTextBox.Text, frmMyDialog.ModelTextBox.Text)
            End If
        End Sub

    Comment

    • truezplaya
      New Member
      • Jul 2007
      • 115

      #3
      Sorry i had managed a work around by the time i recieved your reply but i m going to look in to your way of doing it maybe try using a collection Cheers

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Using a collection:

        The first time the page loads, make the database call and populate a custom collection which holds instances of a class made to imitate a record in your table - this works just like a table would, but allows you a more typesafe way of accessing your data.

        Your UserControl can then be programmed to bind to your custom collection rather than load from the database.

        As an instance is changed or created in your dialog box, it can then save the changed/created instance to the database. If this was a new instance that was created, it can be added to your collection.

        Have your UserControl rebind and it will re-render itself displaying the modified collection.

        Using this method is less expensive than reloading the data directly from the database, however, you do have to worry about data concurrency...

        The alternative (although more expensive) approach is to load from the database each time. This way there is less concern for data concurrency which can be a pain if you're not familiar with it...

        Comment

        Working...