Select only one checkbox in looped checkboxes and get the corresponding textbox value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Princess Zai
    New Member
    • Mar 2013
    • 4

    Select only one checkbox in looped checkboxes and get the corresponding textbox value

    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:

    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
    button1 is the add button
    Last edited by Rabbit; Mar 5 '13, 08:08 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    I'm not actually sure what your question is. If you are saying there are multiple checkboxes on the form and you only want to allow the user to select one, then don't use checkboxes, use radio buttons.

    Comment

    • Princess Zai
      New Member
      • Mar 2013
      • 4

      #3
      if that so, how can i get the textbox value next to that radio button?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        What textbox value next to a radio button? Radio buttons don't have textboxes.

        Comment

        • Princess Zai
          New Member
          • Mar 2013
          • 4

          #5
          i have a radio button and two textboxes, all of them looped. in every radio btton there are 2 corresponding textboxes. example is in the textbox i input username and password, every i press the add button it will insert to the database and it will add another radio button and two textboxes if ever you want to add more. the problem is i will choose a username and password by means of radio button if the radio button is checked, i want to display in another form the username and password next to that radio button i clicked, how will i do that?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Well, it looks like you're uniquely assigning a name to each control. Use the unique name of the radio button to get to the textbox. IE, if I name the textbox, Text58 and the radio button, Radio58, if I click on Radio58, then I know to use Text58.

            Comment

            Working...