CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: IF sectence

  1. #1
    Join Date
    Oct 2010
    Posts
    15

    IF sectence

    I have two textboxes.

    txtbox1
    txtbox2

    I want to make a IF sectence which does this:

    When i do NOT write txt in txtbox1, the other textbox, txtbox2, will be set to visibility = false

    This require that the IF sectence can notice if there is any text in txtbox1.

    How can i do this?
    Last edited by Fnyx; January 13th, 2011 at 07:06 AM.

  2. #2
    Join Date
    Oct 2010
    Posts
    2

    Re: IF sectence

    Just call this method to wherever you want to do the checking. You could also make a string variable and check its length, but for a basic solution this should work.

    private void checkIfText()
    {
    if(textbox1.Text != "")
    {
    textbox2.visible=true;
    textbox2.enabled=true;

    }
    else
    {
    textbox2.visible=false;
    textbox2.enabled=false;
    }
    }
    Last edited by Jermay; January 13th, 2011 at 10:47 AM.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: IF sectence

    There's really no reason to make that so verbose. You are already evaluating a boolean in the if statement, so you can accomplish the same thing with a single line.

    Code:
    textbox2.Visible = (textbox1.Text != String.Empty);
    You don't need to set Enabled since an invisible textbox cannot be interacted with anyway.

    I would handle the TextChanged event of textbox1 and place that code there.

  4. #4
    Join Date
    Jul 2010
    Posts
    82

    Re: IF sectence

    I would use the LostFocus or the Leave event of the first textbox and then do what the above post says. If you are populating the textbox as it loads, you may want to check if the form is visible first and then make this check.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured