Coding User Control VB 2008

I am creating a user control that has a split container and several FPSpread grids on it.
I thought I would create a user control so that I could dynamically add the control to a new tab when a user clicks a button.
I have all the controls settings the way I want.  
Now I need to know if I can actually code this usercontrol to handle all the database insert/update/delete etc. procedures.
First, can I add the main code and class to this control?
Second, are there any problems I should know about if I do?
Third, If there are no problems associated with adding the code, would I do it just like I do when usings a windows form app?
Who is Participating?

If you set up the insert/update/delete statements in there own sub routines then yes, when you are creating the UC use the addHandler statement to point to a sub routine that will activate the insert/update/delete sub routines and you can even pass values to the sql-subs.
Something like:
'code in the button click that creates the UC
Dim uc As New <yourUserControl>
With uc
addHandler .MyRaisedClickEvent, AddressOf MyUCClickEvent
'add more stuff as needed access properties directly here
End With
'then you make the Sub named MyUCClickEvent in the main form
Private Sub MyUCClickEvent(<same signature as the Public Event signature on the UC>)
  MyInsertSub(combobox1.selecteditem) 'say this combobox holds the tables you are using End Sub  
Private Sub MyInsertSub(ByVal table As String)
'in your insert statement
Dim sql As String = " INSERT INTO " & table & " (your fields)......
End Sub
In the UC make a Public Event named 'MyRaisedClickEvent(ByVal something as <whatever data type>)' , then in the click event for the button use
RaiseEvent MyRaiseClickEvent(<any variables that you need to pass from the UC>) to the main form.
This is just an example of passing events thru your UC's much customizing can be added.
I see your code will need to more elabrate poss passing several values and you will have to be clever in the setup of each UC do draw out the right info, but it should be designable.
Also in the MyRaisedClickEvent you have access to the values you passed from the UC's button click event. The variableName in the signature is holding the value.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.