Click here to Skip to main content
15,897,090 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi How to get value of dynamically created check box in windows application.i am create dynamically check box based data from db inside of group box.

And my code for dynamically create check box is ,and only five check box i create.

VB
Dim CheckBoxes As List(Of CheckBox) = New List(Of CheckBox)

       Dim i As Integer = 1
       Dim x As Integer = 10
       Dim y As Integer = 15
       For Each row As DataRow In objdtr.Rows

           Dim chk As New CheckBox
           chk.Name = row("Voltage")
           chk.Text = row("Voltage")
           chk.Location = New Point(x, y)
           chk.Width = 70
           GroupBox1.Controls.Add(chk)
           x = x + 70

       Next


but if i check this check boxes ,in save time how to get checked box value ?



Regards
Aravind
Posted
Updated 18-Aug-13 22:02pm
v2
Comments
Member 12127618 6-Dec-15 15:35pm    
I have similar problem.
But how did you use the DataRow?
I want to change text of checkbox by inputbox.

I have a context menu strip, and one item as"rename",with that I wanna change text of checkbox.
But the problem is that after using addhandler, when i write this code:


Dim chkBox As CheckBox = TryCast(sender, CheckBox)
chkBox.Text = InputBox("Write new text")

I got this error:

An unhandled exception of type 'System.NullReferenceException' occurred in prosto.exe

Additional information: Object reference not set to an instance of an object.



Can you help me please to solve this problem?
Aravindba 6-Dec-15 22:09pm    
Hi i just store checkbox name in sql table,using datatable i get value and pass to checkbox text.for testing purpose u hardcode text and try,or first get value in one string and pass that string to checkbox text.Better give the name also

chkBox.Text = "HAI"
chkBox.Name= "HAI"
or

Dim aaa as String=InputBox("Write new text")
chkBox.Text = aaa
chkBox.Text = aaa

I think other properties u gave like position and add control in form etc..

In your Save Button Click

VB
For Each chkBox In GroupBox1.Controls.OfType(Of CheckBox)()
 if Ctype(GroupBox1.controls("CheckBox" & i), checkbox).checked = true then
' your Code Here
 end if


If you need to create Event for your Check Box then your code might be
VB
Dim CheckBoxes As List(Of CheckBox) = New List(Of CheckBox)
     
        Dim i As Integer = 1
        Dim x As Integer = 10
        Dim y As Integer = 15
        For Each row As DataRow In objdtr.Rows
 
            Dim chk As New CheckBox
            chk.Name = row("Voltage")
            chk.Text = row("Voltage")
          
            chk.Location = New Point(x, y)
            chk.Width = 70
            GroupBox1.Controls.Add(chk)
            x = x + 70
AddHandler chk.CheckedChanged, AddressOf ChkBox_CheckedChanged
 
        Next


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

    Dim chkBox As CheckBox = TryCast(sender, CheckBox)

    If chkBox IsNot Nothing Then

        MessageBox.Show(chkBox.CheckState)

    End If

End Sub

'In Save Button Click
For Each chkBox In GroupBox1.Controls.OfType(Of CheckBox)()
 if Ctype(GroupBox1.controls("CheckBox" & i), checkbox).checked = true then
' your Code Here
 end if
Next
 
Share this answer
 
There are ways to get them - you can look through the GroupBox.Controls list and identify them that way - but a better idea would be to keep a List of them so that you know exactly where they are when it comes to saving:
Create a private list of CheckBoxes at class level in your form:
VB
Private myBoxes As New List(Of CheckBox)()

Add each box to the list when you add it to the GroupBox:
VB
GroupBox1.Controls.Add(chk)
myBoxes.Add(chk)

In your Save code, use a For Each to loop through the boxes, and save the values:
VB
For Each chk As CheckBox In myBoxes
    ' Save them here.
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900