i have this vb.net code where i use foreach to loop through check boxes
but the problem is that it starts from Checkbox 19 to 1 but i want to do this starting from Checkbox1 up to 19
Dim i = 0
For Each myControl As CheckBox In Me.Controls
myControl.Text = Form1.DataGridView1.Columns(i).HeaderText
myControl.Checked = My.Settings("HC" & i)
i+=1
Next
so how can do the above code with normal for loop, i am not sure how to loop through contols using a normal for loop
so here is the new working code
Option Strict On
Option Infer On
For i= 1 To 19
Dim myControl = DirectCast(Me.Controls("CheckBox" + i.ToString()), CheckBox)
myControl.Text = Form1.DataGridView1.Columns(i).HeaderText
MsgBox(i)
myControl.Checked = CBool(My.Settings("HC" & i))
Next
Using a normal
Forloop, you need to manually construct the checkbox name:This uses the ControlCollection indexer to access the control by name and then casts the control to
CheckBox, so that the correct type can be inferred for themyControlvariable.(Obviously, the code sample assumes
Option Strict OnandOption Infer On.)