Learning C# Part 1: Creating Your First Program
Written By: Kevin Jordan
- 28 Jun 2006 -
Description: This series aims to take a beginning programmer to an intermediate level in C#. Topics covered in Part 1 include: a brief intro to the .NET Framework, customizing Visual Studio, the stages of the development process, creating a new Windows application project, using the Forms designer, and running the application.
- Intro To .NET and Visual Studio
- The Stages of the Development Process
- Creating a Windows Application
- Using the Forms Designer and Running the App
- Aww Man... Not Homework
Using the Forms Designer
Form1 already has some basic elements for us: a title bar, minimize, maximize, and close. Isn't that nice? If you double click the form it'll take you to the code view and you can see all the stuff that has been preset up for us by choosing the Windows Application template. You can get by to the forms designer by clicking the Form1.cs[Design] tab at the top or pressing Shift + F7.
On the left side of the screen you should see the Toolbox, if not go to View >> Toolbox. The Toolbox contains a variety of controls that you can use to add images, labels, buttons, list boxes, etc. If you want to close the Toolbox, click the X next to it. I prefer just to hide it by unpinning it (clicking the thumbtack). Then you'll have it in a little tab on the left side of the screen. From the Toolbox drag and drop a textbox and a button onto your form.
If you select the textbox that's on your form, the Properties window for that object should appear in the lower right corner of the screen, if not go to View >> Properties Window. The Properties window lists the property settings for the selected form or control that you can modify while you create or edit an application. A property describes a characteristic of an object such as name, position, or size.
Set the following properties for the newly created textbox:
Location: 100, 50
Name: txtInputBox
Text: Leave this blank
Set the following properties for the newly created button:
Location: 200, 225
Name: btnSubmit
Text: Submit
If you double click the Submit button, it'll take you to the code view and you'll notice that a new method called btnSubmit_Click has been automatically created. Anytime the Submit button is clicked from within the program the code between the { } braces will be executed. Between those braces type the following code:
MessageBox.Show(txtInputBox.Text);
Running the Application
At this point we're ready to run our first piece of code. You can either go to Debug >> Start or you can simply press F5. Form1 should appear. Type something into our text field and then hit the Submit button.
A window should appear displaying whatever you typed into the text field. Beats "Hello World", doesn't it?