6th Tutorial - If Statements

0 Cardinal Coog · March 28, 2015
Hi. I'm new to C#.

Regarding: 6th video tutorial covering If/else-if statements.

When I run the program it doesn't give the user a chance to enter their name in the text box. It automatically skips to the else statement and ends the program (ie. I can't type in a name to check to see whether the if statement works.)

Thanks for any help.

Here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (textBox1.Text == "Trevor")
            {
             MessageBox.Show("Hello, Trevor!");
            }

            else if (textBox1.Text == "Brittany")
            {
                MessageBox.Show("Hi, Brittany!");
            }
            
            else
            {
                MessageBox.Show("User not recognized!");
            }


        }
    }
}

Post a Reply

Replies

Oldest  Newest  Rating
0 thomas kensah · March 31, 2015
Yeah Branislav is right. the system first checks the condition when the form loads and at that particular time of checking, the
value in the Text view would be null. That is since you checked for the condition when your app launches, the system already
checked the text in the textbox when it first launched your app and found out it was empty and then moved on to execute the else
statement. you should trigger that condition in the event of a button - which would be something like this

button1_click (Object sender, EventArgs e)
{
//this way it checks the condition after you have entered a value into the textbox and triggered the button's event
if (textBox1.Text == "Trevor")
{
MessageBox1.Show("Hello, Trevor!");
}

else if (textBox1.Text == "Brittany")
{
MessageBox1.Show("Hi, Brittany!");
}

else
{
MessageBox1.Show("User not recognized!");
}

}
0 Cardinal Coog · March 30, 2015
Raahim Ghauri: If you had read and comprehended the previous posts you would have understood that the problem was not with the control structure itself (the "if-else" statements) but with attaching event handlers to objects in forms. 

As far as I know, that appears to be a feature of C#, which I stated I'm new to learning. I've never come across them in C++ or Python. 

"Just sayin'"
0 Cardinal Coog · March 29, 2015
Oh, I see. That makes sense.... Thanks for pointing that out. Like I said, I'm *really, really* new to C#.  :)
0 Branislav Lazic · March 29, 2015
No it doesn't. Your event handler is attached to Form. In tutorial, event handler is attached to Button. So basically, add button on form, double click on it and write your code there.
0 Cardinal Coog · March 29, 2015
Thanks for the reply. But, I still don't get it. My code follows the same format as the code in the tutorial but doesn't work like in the tutorial. Any more specific advice?

 
0 Branislav Lazic · March 29, 2015
It's because you are attaching event handler to Form. When Form loads (program starts), event is triggered. Code withing Form1_Load event handler will execute. It doesn't skips "if" statements, it just ignores them since value from textBox1 is an empty string.
  • 1

C#

107,194 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators