Enable checkboxes based on textbox value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poops2468
    New Member
    • Apr 2007
    • 7

    Enable checkboxes based on textbox value

    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?

    Code:
            Dim x
            Dim y As New Control
            Dim z As New Control
    
            For x = 1 To 40
                y = "txtTextBox" & x
                z = "chkcheckBox" & x
                If y.Text = "3" Then
                    z.Enabled = False
                End If
            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 for..next.


    Thanks
    Last edited by Killer42; Apr 25 '07, 10:54 PM. Reason: Please use [CODE]...[/CODE] tags around your code.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I'm not familiar with VB.Net since I use the older VB6, but I can suggest something to try..
    Code:
            Dim x
            Dim y As New Control
            Dim z As New Control
    
            For x = 1 To 40
                ' [I]y = "txtTextBox" & x[/I]
                [B]Set y = Me.Controls("txtTextBox" & Format(x))[/B]
                ' [I]z = "chkcheckBox" & x[/I]
                [B]Set z = Me.Controls("chkcheckBox" & Format(x))[/B]
                If y.Text = "3" Then
                    z.Enabled = False
                End If
            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. :)

    Comment

    Working...