Soup to Nuts Lab 1: A Tour of Visual Studio.Net - Exercise 3: Write Some Code (Page 4 of 6 ) Scenario In this exercise, you will write a simple button-click event handler for the Get Customer button on the Customers form. You will declare some variables, concatenate strings and write code to display a message box. Tasks Detailed Steps - Create a button-click event.
Note: C# is a case-sensitive language. If using C#, Ensure that the code you type matches the code in to the right exactly.
- Return to the Customers.xx [Design] tab.
- In the forms designer, Double-click the button with the text Get Customer.
The code pane opens and you see an event handler stub, ready for you to write some code into.
- Write some code into the button click event handler to declare a string variable named Value, display a message in a message box, concatenate a value and assign it to the variable, then display the variable’s value in a message box:
C#
private void button1_Click(object sender, EventArgs e) { string Value = textBox1.Text; MessageBox.Show("Hello, Soup to Nuts!"); Value = "You typed " + Value; MessageBox.Show(Value); }
Visual Basic .NET
Private Sub Button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles Button1.Click Dim Value As string = TextBox1.Text MessageBox.Show("Hello, Soup to Nuts!") Value = "You typed " & Value MessageBox.Show(Value)End Sub
- Create an application entry point.
- In the Solution Explorer, right-click Northwind, choose Add | Add Class.
The Add New Item dialog displays with Class selected by default.
- In the Name box, clear the default text and type MainClass.
- Click Open.
The code pane opens for the new class in the center pane of the IDE. - Add a Main sub routine MainClass class. The class should look like the following:
C# using System; namespace Northwind { /// <summary> /// Summary description for MainClass. /// </summary> public class MainClass { public MainClass() { // // TODO: Add constructor logic here // } public static void Main(string[] args) { System.Windows.Forms. Application.Run( new Customers()); } }
Visual Basic .NET Public Class MainClass Public Shared Sub Main(ByVal args as string()) System.Windows.Forms. Application.Run( _ New Customers()) End Sub End Class
- Change the application entry point to be the one you just created.
- Delete the default Form1.
C#
In Solution Explorer, right-click Form1.cs and choose Delete, then click OK.
Visual Basic.NET
In Solution Explorer, right-click Form1.vb and choose Delete, then click OK.
Visual Basic.NET Only
- In Solution Explorer right-click the Northwind project node and choose Properties.
- Change the Startup object in the Northwind Property Pages dialog, under Common Properties->General, in the Startup object: dropdown, choose Sub Main.
- Click OK.
 | Take Microsoft software for a test drive. With MSDN Virtual Labs, you get full access to all available Microsoft products through 90-minute modules, each with its own downloadable manual. Try this lab out now. |
Next: Exercise 4: Basic Debugging >>
More .NET Articles More By MSDN Virtual Labs |