Coding User Control VB 2008

Posted on 2009-04-18
Last Modified: 2012-05-06
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?
Question by:Sheritlw
    LVL 9

    Accepted Solution


    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.
    LVL 9

    Expert Comment

    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.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now