Sunday, April 25, 2010

Your name in a MessageBox

So, you've learned how to create a simple Form, and now it's time to learn what can you do with it. Of course, we'll start with something very, very simple. In this tiny tutorial, you will learn how to add items on the Form, change some of their properties and make them work.

Step 1:

You see the Toolbox on the left? Cool, I need you to drag and drop the following items on the Form: Label, TextBox and Button. You can change their name in the Properties Windows on the right. Scroll down and look for the Text property. That will change the default name of the item. I changed the Form's name here to MessageBox Name. Call it whatever you want. Change the Label name too, and of course, the Button name.

Step 2:

So, now what? The TextBox reads characters from the keyboard, and shows them in a MessageBox when you click the Button. In this example, the Label is just a simple text on the Form, that doesn't do anything when clicked. It's just a label, labeling something. But, how do you make it work, anyway? You add code. To add code to a specific item and make it do something, you click on it. Now what do we need here, again? We need the Button, when clicked, to perform an action, which would be reading the content entered in the TextBox, and shown as a MessageBox.

This code here will do that. Click the Button, and add these lines:

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show ("Your name is " + textBox1.Text);
}
Visual Studio generates code, you will write the line that is between the curly brackets. In the MessageBox, it will show the text "Your name is " plus the name that you type in the TextBox, as one sentence. Now, after you wrote your first line of code, press F5 to see how the application works. The result should look like this:

Saturday, April 24, 2010

Creating a Form in Visual C#

The first thing we need, is a Windows Forms Application. It's a form, a box, like you've already seen in other Windows applications. Before we start, we need a Microsoft Visual Studio 2008. I would recommend using the Visual C# 2008 Express Edition, which is available for free on the Microsoft website. I use the 2008, so all my projects here are created using the same. Download it, install it and you're ready to go.

Once you have the software, run it, and here we go.

Step 1:

Click File from the top menu, a open a New Project (you can use the shortcut Ctrl+Shift+N). You will see few templates that can be used for creating various applications. We need a Windows Form Application, so pick that one.

Step 2:

Now you have a Form. It's default name is Form1. Of course, the initial Form is empty. You can use the Toolbox on the left for adding various items on it and make them do stuff. On the right, we have the Solution Explorer (we'll talk about it later) and the Properties Window. Once you click on your Form, you can use the Properties Window to change it. Color, name, size, font and similar things used by your needs and taste. Every single item, for example, a button, can be changed to fulfill your needs. But, we'll talk about that later, as well.

You have your first Form. It's doesn't do anything for now, it's just a simple empty Form, just standing there. In the next lesson on Tiny C#, I'll teach you how to add stuff on your Form and make it do some interesting things.

Hello World

Hello everybody!

Welcome to Tiny C#, a blog established as a starting point for novice codes, willing to learn the basics of Visual C#. On Tiny C# you will find various concepts on the language, tutorials for creating simple applications and everything else that you'll need for mastering C#.

The ride begins very soon, so fasten your seatbelts, make a coffee and get ready for the lines of code.

Tiny C#, 2010

Back to TOP