Introduction to C#; Lesson 3

Hope you are following us from our first post, if so! lets continue printing in c# lesson (windows form), if not, don't worry, you can start form the beginning, here is a link for you to review our first and second lesson:

Introduction to C#; Lesson 1 

Introduction to C#; Lesson 2 

Alright! lets begin our third lesson. As we learnt about printing in textbox and label in previous lessons, lets move on to Messagebox in this lesson. 


 Messagebox:

Lets give a quick introduction to Messagebox first, basically it can be used to:
  1. Display any message at run time such as
  • Warnings
  • Error message.
  • Information, confirmation
  • Prompting user to enter value and so on...
 So basically if user leaves some value empty or something then messagebox comes in handy. As message box creates a pop-up screen on your window so user is bound to give attention to your message. User can't proceed until he/she reads the message and give appropriate input etc thus prompting user to abide by your rules.

Now lets get to our GUI designing and coding. I'll assume you thoroughly read our previous posts and know how to open visual studio and drag tools on your form so I'll be just attaching a screenshot of our GUI design and explain it a bit.

 

 So what we did here? We dragged a button from toolbox, we changed the text property of our button by typing click me! instead of using its default text button1. So at run time when you'll click on button some text will be shown in message box, again as button is invoking this functionality, so we'll be coding in our button. 

For this, double click on button and type the following code:

private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Did you just clicked me?");
        }


 Our output will be something like this:


Now, if you are trying this example at the moment, try clicking on form, the messagebox will prevent you to do so until you click on OK or close button of messagebox, thus prompting user to heed on its message! 

Did you get the concept? If so,lets move to some legit example. Say you are asked to design a form that gets a user first name, last name in text box, displays proper label for each textbox i.e First Name and Last Name, further if user leaves either first name or second name empty a messagebox pop ups to prompt user to enter first/last name with a proper message, if user enters both first and last name then a messagebox pop ups to show a message such as welcome etc.

I bet you all will be able to design all this by using Lesson 1,2,3 but just in case you still feel iffy about your newly developed concept then we have designed and codded this for you.

GUI Design:


Now double click on Click me! and type the following code: 

 private void button1_Click(object sender, EventArgs e)
        {
            if((textBox1.Text == "")&& (textBox2.Text == ""))
            {
                MessageBox.Show("Enter your first and last name.");
            }
           if(textBox1.Text== "")
            {
                MessageBox.Show("First name field cant be empty.");
            }
           else if (textBox2.Text == "")
            {
                MessageBox.Show("Last name field cant be empty.");
            }
           else
            {
                MessageBox.Show("Welcome to CODEHUB101");
            }
        }


So what we did here? We simply wrote in our code that when button "Click me!" is clicked then following checks occur with appropriate messages:

  1. If user leaves both fields empty then messagebox pop up displays a warning message and prompt user to enter some text in first and second textbox  
  2. If user leaves his/her first name field empty then messagebox pop up displays a warning message and prompt user to enter some name in first textbox
  3. Then our code checks if user typed first name but forgot to enter his/her second name, if so, messagebox pop up displays a warning message and prompt user to enter some name in second textbox
  4. Finally our code displays a welcome message when it confirms both the textboxes aren't empty.


 

Comments

Popular posts from this blog

Introduction to C#; Lesson 2

Thank you, next! Linked List in C++; Lesson 4