Connecting Tech Pros Worldwide Forums | Help | Site Map

User control and dialog box VB.NET

Member
 
Join Date: Jul 2007
Posts: 87
#1: Nov 10 '08
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.close
Expand|Select|Wrap|Line Numbers
  1. Public Shared Function collectAppendingRow() As DataTable
  2.             Dim tmpList As New List(Of SortData.carInfo)
  3.             Dim tmpTbl As DataTable = Nothing
  4.  
  5.             tmpTbl.Rows.Item(0)(0) = Dialog1.MakeTextBox.Text
  6.             tmpTbl.Rows.Item(0)(1) = Dialog1.ModelTextBox.Text
  7.             tmpTbl.Rows.Item(0)(2) = Dialog1.ColourTextBox.Text
  8.             tmpTbl.Rows.Item(0)(3) = Dialog1.RegTextBox.Text
  9.             tmpTbl.Rows.Item(0)(4) = Dialog1.SpecTextBox.Text
  10.  
  11.  
  12.  
  13.             Return tmpTbl
  14.  
  15.         End Function
  16.  
i am using visual studio 2008

nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#2: Nov 15 '08

re: User control and dialog box VB.NET


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?


Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
  3.         Dim frmMyDialog As Form ' <-- or your custom dialog type
  4.         If frmMyDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then ' user clicked ok button
  5.             ' Call user controls AppendItem method
  6.             .AppendItem(frmMyDialog.NameTextBox.Text, frmMyDialog.ModelTextBox.Text)
  7.         End If
  8.     End Sub
Member
 
Join Date: Jul 2007
Posts: 87
#3: Nov 21 '08

re: User control and dialog box VB.NET


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
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#4: Nov 21 '08

re: User control and dialog box VB.NET


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...
Reply