creating and hadling controls dynamically

smartpreeti03

Member
Joined
Jun 19, 2008
Messages
8
Programming Experience
Beginner
i was trying to create some controls dynamically in a button click event. i created a panel and added control array of radio buttons to this panel. i also added to command buttons for which i used handlers(addhandler). but when writing code in the handler, i cannot access the radio buttons created in the previous event(sine they were locally declared). i also tried to pass the panel by reference to the handler but that did not help.

i want to write code for checking which of the radio button was clicked. all this needs to be done dynamically. please suggest
 
The "sender" parameter of any event is always the class instance that raised the event. You can subscribe to multiple objects events with a single handler method. This sender parameter is type Object and you can check the type if you need to:
VB.NET:
If TypeOf sender Is Label Then..
and you can cast the object to a more specific type if you need to with CType function (or with DirectCast/TryCast):
VB.NET:
Dim b As RadioButton = CType(sender, RadioButton)
 
VB.NET:
Private Sub Button1_Click(ByVal Sender As Object, ByVal e as System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

Dim sButton as Button = DirectCast(sender, Button)

End Sub

Ok so in the above example we have one event handler but it handles the click event for 3 buttons on the form. By casting the sender parameter to a button type we now know which button was clicked and can access it's own properties.
 
thats not the problem. lemme explain. the code is as follows:

Code:

VB.NET:
        dim panel1 as new panel
        Me.Controls.Add(panel1)
        panel1.Show()
        Dim label1 As New Label
        panel1.Controls.Add(label1)
        label1.Text = "What do you want to add"

        Dim radio1(cols) As RadioButton
        i = 0
        Do Until i = cols
            radio1(i) = New RadioButton
            panel1.Controls.Add(radio1(i))
            radio1(i).Text = CStr(drd2.GetName(i))
            radio1(i).Location = New Point(xpos, ypos)
            ypos = ypos + 30
            i = i + 1
        Loop
        Dim cancelbutton1, okbutton1 As New Button
        panel1.Controls.Add(okbutton1)
        panel1.Controls.Add(cancelbutton1)


        AddHandler cancelbutton1.Click, AddressOf cancelbuttonhandler
        AddHandler okbutton1.Click, AddressOf okbuttonhandler


All the following things are done dynamically(on click() event of a button):
1. create a panel
2. add radio buttons to the panel(number of radio buttons known at runtime. hence created a control array)
3. add two button "okbutton" and "cancelbutton" to the panel
4. now on the click event of the "okbutton" i need to check which of the radio buttons are clicked(index) and return the index.

Please let me know if it explains the behavior.
 
Your array of RadioButtons should be declared in the class scope as opposed to locally as you have done it. You can then reference the array from anywhere in the class, interate through the array and query the Checked property to see if it has been selected. Or you can add an event handler for all the radio buttons in the array and do as I have suggested below.
 
You can also loop the panel1.Controls and see which is typeof RadioButton and which of those is checked. If you can't reference "panel1" from where you are you can go via the button clicked and get the Parent, which in your case is the panel.
 
i am a beginer in VB.net and don't know much much about you. Sorry if it is annoying but i could not understand your previous post about looping the panel controls.
 
VB.NET:
for each c as control in panel1.controls

next
Accessing a control directly from the Controls collection by integer or string index could also be an option. panel1.Controls(2), panel1.Controls("radio2")
 
Back
Top