locked
How to Check Condition Using IF in C#

    Question

  • Hello, I want to Check a condition in textbox and also in datetimepicker. Before execute, the program should check condition of both textbox and datetimepicker then show results.

    I have created the code like this:

    if (dateTimePicker1.Text == dateTimePicker2.Text)
    {
        btnConfirm.Enabled = true;
    }
    else if (textBox4.Text != textBox1.Text)
    {
        btnConfirm.Enabled = true;
        return;
    }
    


    However, when the program Check the first condition, then the "btnConfirm" enabled.

    How to create the correct code, so that the program should check the first and second condition and then enabled "btnConfirm".

    Thank you in advance.

    Friday, October 07, 2011 1:39 PM

Answers

  • Hi,

    Try this

     if (dateTimePicker1.Text == dateTimePicker2.Text)
                {
                    if (textBox4.Text != textBox1.Text)
                    {
                        btnConfirm.Enabled = true;
                        return;
                    }  
                }
    
    OR
                if ((dateTimePicker1.Text == dateTimePicker2.Text) && (textBox4.Text != textBox1.Text))
                {
                    btnConfirm.Enabled = true;
                    return;
                }
    

    Hope it helps,

    Regards,
    A.Murugan
     


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    • Edited by Murugan A Friday, October 07, 2011 1:44 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:44 PM
  • Your code here is all correct. The problem is just that btnConfirm is already enabled when this code starts executing.

    Open up your form in Visual Studio, right click on the btnConfirm button, and choose Properties. In the Properties window, scroll down until you find Enabled, and set it to false. That will set the button to be false by default, so it will not be enabled until after your code is hit.

    EDIT: Another small point, you can get rid of the return line, unless you really want to return from the method immediately when the text matches.

    EDIT2: I misread the original question. I didn't notice you were trying to meet both conditions in order to enable the button. See Murugan's answer for the correct way to solve this.

     


    Check out My Blog for tech news, development tips, and other information for geeks like me.

    • Edited by Tim Copenhaver Friday, October 07, 2011 1:47 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:45 PM
  • You should use &&(and) operator.

    Here is a sample.

    if (dateTimePicker1.Text == dateTimePicker2.Text && textBox4.Text != textBox1.Text)
    {
        btnConfirm.Enabled = true;
    }
    else
    {
    btnConfirm.Enabled=false;
    }
    

     

     

     


    • Edited by Tamer Oz Friday, October 07, 2011 1:47 PM
    • Proposed as answer by Matthew Watson Friday, October 07, 2011 2:21 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:46 PM
  • I think you're meaning something like this?

                if ((dateTimePicker1.Text == dateTimePicker2.Text) && (textBox4.Text != textBox1.Text))
                {
                    btnConfirm.Enabled = true;
                    return;
                }
    
    

     


    Tom Overton
    • Edited by Tom_Overton Friday, October 07, 2011 1:47 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:46 PM
  • Hi Jun,

    I thnk this is what you're looking for.  

                if((dateTimePicker1.Text == dateTimePicker2.Text && textBox4.Text != textBox1.Text))
                {
                    btnConfim.Enabled = true;
                }
    

     

     

    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:48 PM

All replies

  • Hi,

    Try this

     if (dateTimePicker1.Text == dateTimePicker2.Text)
                {
                    if (textBox4.Text != textBox1.Text)
                    {
                        btnConfirm.Enabled = true;
                        return;
                    }  
                }
    
    OR
                if ((dateTimePicker1.Text == dateTimePicker2.Text) && (textBox4.Text != textBox1.Text))
                {
                    btnConfirm.Enabled = true;
                    return;
                }
    

    Hope it helps,

    Regards,
    A.Murugan
     


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    • Edited by Murugan A Friday, October 07, 2011 1:44 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:44 PM
  • Your code here is all correct. The problem is just that btnConfirm is already enabled when this code starts executing.

    Open up your form in Visual Studio, right click on the btnConfirm button, and choose Properties. In the Properties window, scroll down until you find Enabled, and set it to false. That will set the button to be false by default, so it will not be enabled until after your code is hit.

    EDIT: Another small point, you can get rid of the return line, unless you really want to return from the method immediately when the text matches.

    EDIT2: I misread the original question. I didn't notice you were trying to meet both conditions in order to enable the button. See Murugan's answer for the correct way to solve this.

     


    Check out My Blog for tech news, development tips, and other information for geeks like me.

    • Edited by Tim Copenhaver Friday, October 07, 2011 1:47 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:45 PM
  • You should use &&(and) operator.

    Here is a sample.

    if (dateTimePicker1.Text == dateTimePicker2.Text && textBox4.Text != textBox1.Text)
    {
        btnConfirm.Enabled = true;
    }
    else
    {
    btnConfirm.Enabled=false;
    }
    

     

     

     


    • Edited by Tamer Oz Friday, October 07, 2011 1:47 PM
    • Proposed as answer by Matthew Watson Friday, October 07, 2011 2:21 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:46 PM
  • I think you're meaning something like this?

                if ((dateTimePicker1.Text == dateTimePicker2.Text) && (textBox4.Text != textBox1.Text))
                {
                    btnConfirm.Enabled = true;
                    return;
                }
    
    

     


    Tom Overton
    • Edited by Tom_Overton Friday, October 07, 2011 1:47 PM
    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:46 PM
  • Hi Jun,

    I thnk this is what you're looking for.  

                if((dateTimePicker1.Text == dateTimePicker2.Text && textBox4.Text != textBox1.Text))
                {
                    btnConfim.Enabled = true;
                }
    

     

     

    • Marked as answer by Jun Harefa Friday, October 07, 2011 2:25 PM
    Friday, October 07, 2011 1:48 PM
  • Big Thanks to Murugan A, Tim Copenhaver, Tamer Oz, Tom Overton, amoline.. I've try your code, and it work!

    Thanks again...:)

    Friday, October 07, 2011 2:27 PM