How can I create radio button or any form element on run time. I mean for my application I extract some information from XML document and then I want to make some choices through radio button. And I can't hard code radio button as their number can be vary on form.
Thank a lot ! it works.
But what if I want to have more than 1. There might be an array of radio button, here is the sample code which I have written. It has an exception when it reads the name.
code:
Dim radiolist() As RadioButton
Dim i As Int16
Dim j As Int16 = 10
Dim temp As String = "Radio"
For i = 1 To 10
radiolist(i).Name = temp + Str(i)
radiolist(i).Location = New Point(1, j)
radiolist(i).Text = Str(i)
j = j + 10
Next
Exception:
An unhandled exception of type 'System.NullReferenceException' occurred in dynamic.exe
Additional information: Object reference not set to an instance of an object.
Do you know what is the problem or what could be the right way to do this.
I got some positive result with the help of your code, the new code now looks like,
Dim i As Int16
Dim j As Int16 = 10
Dim temp As String = "Radio"
For i = 1 To 10
Dim r As New RadioButton
r.Name = temp + Str(i)
r.Location = New Point(1 + 10, j)
r.Text = Str(i)
j = j + 25
Me.Controls.Add(r)
Next
But this raises a big question. As there is no arrays involved, how can I access radio button at run time. For example, the above code will put 10 radio button on the form. Now suppose that I have a button on the form which when clicked must change the text of the radio button 1 or make it checked. The problem is I don't know the name in advance. So how this will be solved !
Originally posted by hoomdoom
But this raises a big question. As there is no arrays involved, how can I access radio button at run time. For example, the above code will put 10 radio button on the form. Now suppose that I have a button on the form which when clicked must change the text of the radio button 1 or make it checked. The problem is I don't know the name in advance. So how this will be solved !
Ahad
See this demo . You need AddHandler keyword to wire up the event with the control you created on the fly .
Dim radiolist() As [B]New<b>You just need "<b>New</b>" Keyword when creating the array .
</b> RadioButton
Dim i As Int16
Dim j As Int16 = 10
Dim temp As String = "Radio"
For i = 1 To 10
radiolist(i).Name = temp + Str(i)
radiolist(i).Location = New Point(1, j)
radiolist(i).Text = Str(i)
j = j + 10
Next
[/B]
Hi,
Are you sure you can do
Dim radiolist() As New RadioButton?
I did not know you could create a control array in VB.NET just like that.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Originally posted by taxes Hi,
Are you sure you can do
Dim radiolist() As New RadioButton?
I did not know you could create a control array in VB.NET just like that.
oops , that's the problem when I remember things from my memory . That's my fault(after I tried it now) . I'm sorry guys .
You can not create controls array with "NEW" keyword .
Search through this forum for "Creating Controls at runtime". You will find a lot but make sure they refer to VB.NET.
You CAN simulate a VB6 control array (which was what Pirate was suggesting) but you have to do a lot of work first. Have a look at the MSDN Help section on Control Arrays and work through the various sections - but it is going to be a hard learning curve
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Hi,
Thanks for helping me in dynamic form element generation. Now I have a different kind of question ready for you.
I have seen many applications which include xp styles form elements or have the xp look and also contain good looking buttons. How this can be done. Any link of free libraries.