By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,423 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

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

P: 4
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:

Expand|Select|Wrap|Line Numbers
  1. 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()
  2.  
  3.     strsql = "select * from tbuser order by ID desc"
  4.     sqlcmd.CommandText = strsql
  5.     sqlcmd.Connection = GetConnection()
  6.     adapter.SelectCommand = sqlcmd
  7.     adapter.Fill(dataset, "0")
  8.     Dim count = dataset.Tables(0).Rows.Count
  9.     If count > 0 Then
  10.         Dim r As DataRow
  11.         If dataset.Tables(0).Rows.Count <> 0 Then
  12.             For Each r In dataset.Tables(0).Rows
  13.                 For usernametxt = 0 To username_textbox.Length - 1
  14.  
  15.                 Next
  16.  
  17.                 For chckbx = 0 To chckbox.Length - 1
  18.  
  19.                 Next
  20.  
  21.                 For txt = 0 To txtboxnew.Length - 1
  22.  
  23.                 Next
  24.  
  25.                 ReDim Preserve username_textbox(usernametxt)
  26.                 ReDim Preserve chckbox(chckbx)
  27.                 ReDim Preserve txtboxnew(txt)
  28.  
  29.                 username_textbox(usernametxt) = New TextBox
  30.                 chckbox(chckbx) = New CheckBox
  31.                 txtboxnew(txt) = New TextBox
  32.  
  33.                 With username_textbox(usernametxt)
  34.                     .Name = "TextBox" & usernametxt.ToString()
  35.                     .Text = r("username")
  36.  
  37.                     If username_textbox.Length < 2 Then
  38.                         ' Position the first one.
  39.                         .SetBounds(186, 103, 100, 20)
  40.                     Else
  41.                         ' Position subsequent controls.
  42.                         .Left = username_textbox(usernametxt - 1).Left
  43.                         .Top = username_textbox(usernametxt - 1).Top + username_textbox(usernametxt - _
  44.                             1).Height + 4
  45.                         .Size = username_textbox(usernametxt - 1).Size
  46.                     End If
  47.  
  48.                     ' Save the control's index in the Tag property.
  49.                     ' (Or you can get this from the Name.)
  50.                     .Tag = usernametxt
  51.                 End With
  52.  
  53.  
  54.  
  55.  
  56.                 With chckbox(chckbx)
  57.                     .Name = "TextBox" & chckbx.ToString()
  58.                     .Text = r("ID")
  59.  
  60.                     If chckbox.Length < 2 Then
  61.                         ' Position the first one.
  62.                         .SetBounds(65, 105, 100, 20)
  63.                     Else
  64.                         ' Position subsequent controls.
  65.                         .Left = chckbox(chckbx - 1).Left
  66.                         .Top = chckbox(chckbx - 1).Top + chckbox(chckbx - _
  67.                             1).Height + 4
  68.                         .Size = chckbox(chckbx - 1).Size
  69.                     End If
  70.  
  71.                     ' Save the control's index in the Tag property.
  72.                     ' (Or you can get this from the Name.)
  73.                     .Tag = chckbx
  74.                 End With
  75.  
  76.  
  77.  
  78.  
  79.                 With txtboxnew(txt)
  80.                     .Name = "TextBox" & txt.ToString()
  81.  
  82.  
  83.                     If txtboxnew.Length < 2 Then
  84.                         ' Position the first one.
  85.                         .SetBounds(166, 102, 330, 103)
  86.                     Else
  87.                         ' Position subsequent controls.
  88.                         .Left = txtboxnew(txt - 1).Left
  89.                         .Top = txtboxnew(txt - 1).Top + txtboxnew(txt - _
  90.                             1).Height + 4
  91.                         .Size = txtboxnew(txt - 1).Size
  92.                     End If
  93.  
  94.                     ' Save the control's index in the Tag property.
  95.                     ' (Or you can get this from the Name.)
  96.                     .Tag = txt
  97.  
  98.                 End With
  99.                 Me.Controls.Add(txtboxnew(txt))
  100.                 Me.Controls.Add(username_textbox(usernametxt))
  101.                 Me.Controls.Add(chckbox(chckbx))
  102.             Next
  103.  
  104.         End If
  105.     End If
  106.  
  107. End Sub
  108.  
  109.  
  110. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  111.     For usernametxt = 0 To username_textbox.Length - 1
  112.         usernametextbox.Text = username_textbox(usernametxt).Text
  113.     Next
  114.  
  115.     strsql = "insert into tbuser (username) values ('" & usernametextbox.Text & "')"
  116.     sqlcmd.CommandText = strsql
  117.     sqlcmd.Connection = GetConnection()
  118.     sqlcmd.ExecuteNonQuery()
  119.     GetConnection.Close()
  120.     RefreshForm()
  121.  
  122. End Sub
button1 is the add button
Mar 5 '13 #1
Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,383
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.
Mar 5 '13 #2

P: 4
if that so, how can i get the textbox value next to that radio button?
Mar 6 '13 #3

Rabbit
Expert Mod 10K+
P: 12,383
What textbox value next to a radio button? Radio buttons don't have textboxes.
Mar 6 '13 #4

P: 4
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?
Mar 6 '13 #5

Rabbit
Expert Mod 10K+
P: 12,383
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.
Mar 6 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.