Adding controls at run time

I am a very new at vb so please forgive me. I know the answer will be simple.

I want to add radiobuttons to a group box @ runtime I used this code and it works

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' Backing storage -- a generic list

Dim names As New List(Of String)()

' Add an item to the Collection

names.Add("one")

names.Add("two")

Dim radio As RadioButton

Dim y As Integer = 30

For Each button As String In names

radio = New RadioButton()

With radio

.Location = New Point(10, y)

.Text = button

End With

y += 30

Me.GroupBox1.Controls.Add(radio)

Next

My question is how to set properties or values of each radiobutton

Thanks




Answer this question

Adding controls at run time

  • OoLee

    Steven Ramacher wrote:

    You are missing the event handler on the Public sub radiobutton1_checkedChanged event. Needs to look like this, the bold handler is what is missing.

    Public Sub radiobutton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radiobutton1.CheckedChanged

    Hope this helps,

    Steven

    This was the answer I Just did not know that I had to add this in the declaration of the forms

    this is what I had

    Dim RadioButton1 as new RadioButton

    This is what I needed

    Private WithEvents radiobutton1 As New RadioButton

    It Works!!!!!!

    Thanks to everyone for helping a novice.



  • Aneo

    Thanks I will try that.

  • pradeepp

    Private Sub radiobutton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles radiobutton1.CheckedChanged

    If radiobutton1.Checked Then

    pressure1 = 900

    testlabel.Text = pressure1

    End If

    End Sub

    Build fails on this line

    Public Sub radiobutton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radiobotton1.CheckedChanged

    With the following message

    Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

    The WithEvents variable L HELP

    I really appricate help, I wish that someday I could climb to the ranks of a novice VB hack

    Thanks



  • Raju_Sreenivasan

    You are missing the event handler on the Public sub radiobutton1_checkedChanged event. Needs to look like this, the bold handler is what is missing.

    Public Sub radiobutton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radiobutton1.CheckedChanged

    Hope this helps,

    Steven


  • dreadjr

    Thanks for the help. It still has me stumped I need to back away from it for a while.

    Coding is fun



  • amitabhcy

    I am still lost I have tried everything I can think of read every book I have and I am still lost.

    I can add the controls no problem. It’s when I try to make the control do something is where I am lost.

    I have dropped adding controls with an array took a step back and am trying to understand what’s going on.

    To add a radiobutton to groupbox 4 on my form I followed these steps

    Top of form

    Dim raidobutton1 as new radiobutton

    On button1 click event I have this code

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If counter = 1 Then

    Button1.Text = "5000# Lubricator"

    Me.GroupBox4.Controls.Add(radiobutton1)

    radiobutton1.Text = "0 to 1000 PSI"

    radiobutton1.Location = New Point(25, 70)

    GroupBox4.Controls.Add(radiobutton2)

    radiobutton2.Text = "1001 to 2000 PSI"

    radiobutton2.Location = New Point(25, 95)

    ElseIf counter = 2 Then

    Button1.Text = "10000# Lubricator"

    ElseIf counter = 3 Then

    Button1.Text = "No Pressure"

    ElseIf counter = 4 Then

    counter = 1

    End If

    counter += 1

    End Sub

    This adds the raidobutton to group box 4

    I then created a new sub for the radiobutton with the following code

    Public Sub radiobutton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    If radiobutton1.Checked Then

    pressure1 = 900

    testlabel.Text = pressure1

    End If

    End Sub

    When the program runs it does not give me anything on the label. I know this has an easy fix what am I doing wrong

    Every VB.net book I have just skims over the subject, leaving me beating my head into the wall J

    Thanks



  • Shahid Mahmood

    Try adding a handler statement after you add the control.

    Dim ctrls As New Button

    Me.Controls.Add(ctrls)

    AddHandler ctrls.Click, AddressOf radiobutton1_CheckedChanged


  • gshaf

    Your already setting properties at runtime but if your asking how to address the controls after youve initially created them then the following should work for you. I've highlighted some key lines.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Backing storage -- a generic list

    Dim names As New List(Of String)()
    ' Add an item to the Collection
    names.Add("one")
    names.Add("two")

    '//Your creating a setting properties at runtime
    Dim radio As RadioButton
    Dim y As Integer = 30
    Dim i As Integer = 1
    For Each button As String In names
    radio = New RadioButton()
    With radio
    .Name = "RadioButton" & i.ToString
    .Location = New Point(10, y)
    .Text = button
    i = i + 1
    End With
    y += 30
    Me.GroupBox1.Controls.Add(radio)
    Next

    '//Your now further setting properties after initial creation
    For Each button As Control In Me.GroupBox1.Controls
    Dim b1 As RadioButton = CType(button, RadioButton)

    If b1.Name.Substring(0, 11) = "RadioButton" Then
    b1.FlatStyle = FlatStyle.Popup
    End If

    If b1.Name.Substring(0, 12) = "RadioButton1" Then b1.Checked = True
    Next

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    CType(Me.Controls("GroupBox1").Controls("RadioButton2"), RadioButton).Checked = True
    End Sub
    End Class


  • Adding controls at run time