Results 1 to 14 of 14

Thread: how to create radio button at run time

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    34

    how to create radio button at run time

    Hi,

    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.

    Thanks in advance.

    hoomdoom

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Basic demo
    VB Code:
    1. Private Sub CreateRB()
    2.         'You need to set up the commented properites
    3.         Dim r As New RadioButton
    4.         r.Name = "RadioButton1"
    5.         'r.Size = new Size(w,h)
    6.         'r.Location = New Point(xxx, yyy)
    7.         Me.Controls.Add(r)
    8.     End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    34

    Cool

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    34
    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 !

    Ahad

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You just need "New" Keyword when creating the array .
    VB Code:
    1. Dim radiolist() As [B]New[/B] RadioButton
    2.         Dim i As Int16
    3.         Dim j As Int16 = 10
    4.         Dim temp As String = "Radio"
    5.  
    6.         For i = 1 To 10
    7.             radiolist(i).Name = temp + Str(i)
    8.             radiolist(i).Location = New Point(1, j)
    9.             radiolist(i).Text = Str(i)
    10.             j = j + 10
    11.         Next

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .
    Attached Files Attached Files

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    34

    Thumbs up

    thanks a lot !

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Pirate
    VB Code:
    1. Dim radiolist() As [B]New<b>You just need "<b>New</b>" Keyword when creating the array .
    2. </b> RadioButton
    3.         Dim i As Int16
    4.         Dim j As Int16 = 10
    5.         Dim temp As String = "Radio"
    6.  
    7.         For i = 1 To 10
    8.             radiolist(i).Name = temp + Str(i)
    9.             radiolist(i).Location = New Point(1, j)
    10.             radiolist(i).Text = Str(i)
    11.             j = j + 10
    12.         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.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi hoomdoom,

    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.

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I posted a working code in the rared file .

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    34

    Thumbs up

    Thanks guys , I have everything working now. And pirate thanks for your code too, that really help me a lot.

    Ahad

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    34

    a new problem

    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.

    Thanks in advance

    Hoomdoom

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It has been asked many times , search the forum .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width