pzaj
pzaj

Reputation: 1092

retrieving controls from database

I am creating an application which requires dynamically loaded controls (to be precise buttons). These buttons are exactly the same, therefore the problem is not creating controls programatically, but making them work somehow. Each button should open a form, the form is being based on some data from the button. I'd like to have all the buttons use one event handler and open forms accordingly to the data it contains. Is there any way to do so? I don't know the types of these forms yet, therefore I can't use pre-defined "select case" or anything that would limit possibilities.

Another problem is that I would like to have some of the forms (modules) being "installed" independently. Is there any way to make that possible? I believe that could be completed using dlls, but I'm not sure. Is there any information on that, I've been looking for that for a while, but found nothing.

Upvotes: 0

Views: 115

Answers (3)

rheitzman
rheitzman

Reputation: 2297

Adding to Heinzi's post:

Keep a list of the button names you created for use with AddHandler().

This code isn't exactly on point but it shows how the AddHandler() can be used at runtime:

   ' hook TextChanged/CheckedChanged for dirty logic
    Dim chk As CheckBox
    Dim rdo As RadioButton
    For Each c As Control In FormDataControls
        If c.Name.StartsWith("chk") Then
            chk = c
            AddHandler chk.CheckedChanged, AddressOf SetDirty
        ElseIf c.Name.StartsWith("rdo") Then
            rdo = c
            AddHandler rdo.CheckedChanged, AddressOf SetDirty
        Else
            AddHandler c.TextChanged, AddressOf SetDirty
        End If
    Next

FormDataControls is my list of controls to target. In my case I use the Tag property to designate data controls.

Upvotes: 0

Mathieu Le Touzé
Mathieu Le Touzé

Reputation: 33

Maybe i have not understand well your answer... But for your buttons:

1) Create a custom control, inheriting the Button control, and add the properties used for the creation of your forms.

2) In the Sub referenced by your eventHandler, the first argument is the control (the button) who raises the event, so you can use it's own properties to create your forms...

Upvotes: 0

Heinzi
Heinzi

Reputation: 172390

Regarding your first problem: You can create buttons in code; the answers to the following question should be enough to get you started (it's C#, but the principle is the same):

In a nutshell:

  • Read your button configuration data from the database (or configuration file or whatever),
  • add the buttons to your user interface (see above),
  • use AddHandler to add an event handler to each of the buttons. For example:

    AddHandler myButton.Click, Sub(sender, e)
                                   ' Do something
                               End Sub
    

(Regarding your second question: 1. It is unclear to me what you are asking and 2. you shouldn't ask two questions in the same SO question. I suggest that you open a new SO question for that and try to explain your requirement more clearly.)

Upvotes: 1

Related Questions