how can i select only one checkbox from looped checkboxes in a form and get the value of the textbox corresponds to it. Example is I have an ID and a username, i will add a username, the ID is automatically added because of increment. i have a checkbox and next to this is the textbox for Id and textbox for username, if i checked the checkbox then it will get the username and id. thank you for helping in advance :) this is my code:
button1 is the add button
Code:
Private Sub Form8_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'Dbase1DataSet.tbuser' table. You can move, or remove it, as needed. Me.TbuserTableAdapter.Fill(Me.Dbase1DataSet.tbuser) RefreshForm()
strsql = "select * from tbuser order by ID desc"
sqlcmd.CommandText = strsql
sqlcmd.Connection = GetConnection()
adapter.SelectCommand = sqlcmd
adapter.Fill(dataset, "0")
Dim count = dataset.Tables(0).Rows.Count
If count > 0 Then
Dim r As DataRow
If dataset.Tables(0).Rows.Count <> 0 Then
For Each r In dataset.Tables(0).Rows
For usernametxt = 0 To username_textbox.Length - 1
Next
For chckbx = 0 To chckbox.Length - 1
Next
For txt = 0 To txtboxnew.Length - 1
Next
ReDim Preserve username_textbox(usernametxt)
ReDim Preserve chckbox(chckbx)
ReDim Preserve txtboxnew(txt)
username_textbox(usernametxt) = New TextBox
chckbox(chckbx) = New CheckBox
txtboxnew(txt) = New TextBox
With username_textbox(usernametxt)
.Name = "TextBox" & usernametxt.ToString()
.Text = r("username")
If username_textbox.Length < 2 Then
' Position the first one.
.SetBounds(186, 103, 100, 20)
Else
' Position subsequent controls.
.Left = username_textbox(usernametxt - 1).Left
.Top = username_textbox(usernametxt - 1).Top + username_textbox(usernametxt - _
1).Height + 4
.Size = username_textbox(usernametxt - 1).Size
End If
' Save the control's index in the Tag property.
' (Or you can get this from the Name.)
.Tag = usernametxt
End With
With chckbox(chckbx)
.Name = "TextBox" & chckbx.ToString()
.Text = r("ID")
If chckbox.Length < 2 Then
' Position the first one.
.SetBounds(65, 105, 100, 20)
Else
' Position subsequent controls.
.Left = chckbox(chckbx - 1).Left
.Top = chckbox(chckbx - 1).Top + chckbox(chckbx - _
1).Height + 4
.Size = chckbox(chckbx - 1).Size
End If
' Save the control's index in the Tag property.
' (Or you can get this from the Name.)
.Tag = chckbx
End With
With txtboxnew(txt)
.Name = "TextBox" & txt.ToString()
If txtboxnew.Length < 2 Then
' Position the first one.
.SetBounds(166, 102, 330, 103)
Else
' Position subsequent controls.
.Left = txtboxnew(txt - 1).Left
.Top = txtboxnew(txt - 1).Top + txtboxnew(txt - _
1).Height + 4
.Size = txtboxnew(txt - 1).Size
End If
' Save the control's index in the Tag property.
' (Or you can get this from the Name.)
.Tag = txt
End With
Me.Controls.Add(txtboxnew(txt))
Me.Controls.Add(username_textbox(usernametxt))
Me.Controls.Add(chckbox(chckbx))
Next
End If
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For usernametxt = 0 To username_textbox.Length - 1
usernametextbox.Text = username_textbox(usernametxt).Text
Next
strsql = "insert into tbuser (username) values ('" & usernametextbox.Text & "')"
sqlcmd.CommandText = strsql
sqlcmd.Connection = GetConnection()
sqlcmd.ExecuteNonQuery()
GetConnection.Close()
RefreshForm()
End Sub
Comment