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

Enable checkboxes based on textbox value

P: 7
I have a form with a lot of text boxes and an equal number of check boxes. I want the check box to be disabled if the text in the text box is a certain value. I'm still a VB noob, so forgive me if my question is too trivial.

Obviously this doesn't work, otherwise I wouldn't be asking, but by looking at this you should be able to see what I am trying to do. Does anyone have a solution or suggestion?

Expand|Select|Wrap|Line Numbers
  1.         Dim x
  2.         Dim y As New Control
  3.         Dim z As New Control
  5.         For x = 1 To 40
  6.             y = "txtTextBox" & x
  7.             z = "chkcheckBox" & x
  8.             If y.Text = "3" Then
  9.                 z.Enabled = False
  10.             End If
  11.         Next
The error is....
Unable to cast object of type 'System.String' to type System.Windows.Forms.Control'.

Now I know I can't refer to a control using the string combination, but thats all I could think of to get through all of my controls in a

Apr 25 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 8,434
I'm not familiar with VB.Net since I use the older VB6, but I can suggest something to try..
Expand|Select|Wrap|Line Numbers
  1.         Dim x
  2.         Dim y As New Control
  3.         Dim z As New Control
  5.         For x = 1 To 40
  6.             ' y = "txtTextBox" & x
  7.             Set y = Me.Controls("txtTextBox" & Format(x))
  8.             ' z = "chkcheckBox" & x
  9.             Set z = Me.Controls("chkcheckBox" & Format(x))
  10.             If y.Text = "3" Then
  11.                 z.Enabled = False
  12.             End If
  13.         Next
Note that I'm using the Format() function to strip off the extra space that you get when appending a number to a string. This applies in VB6, but may not in your version. Feel free to experiment. :)
Apr 25 '07 #2

Post your reply

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