none
Enable one Textbox, Disable the other in a Vb.net Winform

    Question

  • I have three textboxes and a Submit Button in a GroupBox on a Winform.

     

    Textbox1 (holds a single computername)

    Textbox2 (holds multiple computernames from a text file)

    Textbox3 (software application name entered here)

     

    When I enter information in one of the first two textboxes

    As I enter information in the one textbox, I want it to disable the other textbox.

    I would like it to work both ways, no matter which box I enter data into.

    Is there a way to do this. I'm not finding the methods/properties I hoped to find.

     

    Thank you,

    Wally 

    Thursday, May 10, 2007 6:06 PM

Answers

  • Actually, you both helped me figure it out. I realized in order to get the Textbox control to respond, I had to create a TextChanged Event for each control and it now works fine:

     

    Private Sub txtComputerName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtComputerName.TextChanged

    txtComputerName.Enabled = True

    txtMultCmp.Enabled = False

    End Sub

    Private Sub txtMultCmp_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMultCmp.TextChanged

    txtMultCmp.Enabled = True

    txtComputerName.Enabled = False

    End Sub

     

    Thanks again for your guidance.

    Wally

    Thursday, May 10, 2007 8:17 PM

All replies

  • I think the .Text and .Enabled properties are what you're looking for. Put this in the first textbox TextChanged event:

     

    Code Snippet

    If textBox1.Text = "" Then

       textBox2.Enabled = False

    Else

       textBox2.Enabled = True

     End If

     

    Thursday, May 10, 2007 6:35 PM
  • I tried the exact same solution but it doesn't disabled or grey out the textbox control.

     

    Here's the code:

    Private Sub btnSingleSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSingleSubmit.Click

    Dim strComputer As String

    strComputer = txtComputerName.Text

    If Me.txtComputerName.Text = "" Then

    MsgBox("Unable to Proceed - Please enter a Computer Name", _

    MessageBoxButtons.OK, "Computer Name(s)")

    Me.txtMultCmp.Enabled = False

    Else

    txtMultCmp.Enabled = True

    End If

     

    I've tested this with Checkboxes and it works just fine.

     

    Actually, the code for the textboxes are coded within this sub routine:

    Private Sub btnSingleSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSingleSubmit.Click

    How would you setup a TextChanged event within the code above?

     

    Thursday, May 10, 2007 7:14 PM
  • Add as Sub to handle it for

    Private Sub Enable(Tx1 as TextBox,Tx2 as TextBox,Tx4 as TextBox)

    Tx1.Enabled=True

    Tx2.Enabled=False

    Tx3.Enabled=False

    End Sub

    Tx1 the text to be enabled

    tx2 and tx3 the ones to be disabled

     

     

    Thursday, May 10, 2007 7:28 PM
  • I added this Sub and then call it from the btnSingleSubmit_Click Sub.

     

    Public Sub Enable(ByVal txtComputerName As TextBox, ByVal txtMultCmp As TextBox)

    txtComputerName.Enabled = True

    txtMultCmp.Enabled = False

    End Sub

     

    Still doesn't work. Now, if I actually go into the Form[Design] and change it to False manually and then run the program,

    the Box is greyed out like I want it to do. Not sure why this or a couple of the other simple routines won't work.

     

    Any other suggestions. I'm sure I'm missing something simple but it .....isn't that simple...yet.

     

    Thanks,

    W

    Thursday, May 10, 2007 7:58 PM
  • Actually, you both helped me figure it out. I realized in order to get the Textbox control to respond, I had to create a TextChanged Event for each control and it now works fine:

     

    Private Sub txtComputerName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtComputerName.TextChanged

    txtComputerName.Enabled = True

    txtMultCmp.Enabled = False

    End Sub

    Private Sub txtMultCmp_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMultCmp.TextChanged

    txtMultCmp.Enabled = True

    txtComputerName.Enabled = False

    End Sub

     

    Thanks again for your guidance.

    Wally

    Thursday, May 10, 2007 8:17 PM