After completion of today's lab, you should be able to
Label, TextBox and Button controls in a Window FormAt the start of today's lab, obtain a CS 446 account and password from your instructor and log in to this account.
There are a few things we should do to help set-up Visual Studio to work best in the Hebeler Labs.
Visual Studio stores all applications in solutions. Contained inside of solutions are one or more projects. Both Solutions and Projects represent folders on the hard drive. Inside the project folders go the actual project files.
To help with grading, it is important that all your graded work, including programming assignments 1-4 and your prototype project, is saved neatly in your account's U:\ drive .
We will have 11 labs, 4 programming assignments, and one prototype project. To be consistent, everyone should save their work in solution folders named
Labs - inside folder Labs will be the individual Visual C# Solutions
Lab1 ... Lab11Programs - inside folder Programs will be the four programming assignment solutions
nnProgram1 ... nnProgram4 where nn is your student account numberPrototype - inside folder Prototype should be one Visual C# solution
nnPrototype where nn is your student account numbernnPrototype Visual C# solution. Plainly label old versions as OldSolution or something equally obviousOtherDocuments folder here or in the U:\ root folder for Word, PowerPoint, and Excel documentsActivity: Create a New Project called WindowsUI in Solution Lab1
WindowsUI as the project name, U:\ as the Location, and Lab1 as the solution name.Note: Only one solution can be open in Visual Studio at one time. To work with multiple solutions, you simple need to open up multiple instances of Visual Studio.
Inside of solutions are one or more projects. Many times, with a simple application done by one person, a single project is sufficient. However, when scaling up in size and number of team members, using multiple projects to modularize the solution is a better idea. Typical projects in a medium-sized solution can be
Activity: Solution and Project files
U:\ drive
C# is an object-oriented language very similar to Java. Classes are the fundamental organizing blocks for code. Projects typically contain one to many C# classes.
Files that contain C# classes use the .cs extension
Visual Studio created two files that contain C# classes for the WindowsUI project
Program.csForm1.csActivity: View the Program class
Program.cs and select View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsUI
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Notes:
System.Windows.Forms.Application). Main method (note the capital M) is always the starting point in a C# windows application. Application is a class defined in the .net frameworkForm1()) displays the window generated by calling the constructor method for the Form1 class, passing no-arguments to the constructor methodGraphical user interface windows are known are called Forms in .NET, and are implemented as derived classes from the System.Windows.Forms.Form class.
Visual Studio added Form1 as the startup Form for your project.
We will add an additional class to our WindowsUI Project called MainForm to implement a GUI for our simple Hello World program.
Form1 class name, right-click, select Refactor » Rename and change the class name to MainFormActivity 3: Initialize your MainForm's properties
in MainForm's main method, use Visual Studio.NET's IntelliSense to manually add the code
System.DateTime.Now)
{0:D}) is a handy way to embed one or more formatted values into a string.
namespace WindowsUI
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
//Initialize the Form's size in pixels
this.Size = new Size(800, 600);
//Display today's date in the Form's Title bar caption
this.Text = String.Format("Hello - Today is {0:D}", DateTime.Now);
}
}
}
Controls are graphical user interface widgets such as buttons, scroll bars, and menus that are displayed inside of Forms.
Controls may be added to Forms by
Activity: Add three controls as instance variable to your MainForm class
MainForm classpublic partial class MainForm : Form { //Instance variables private TextBox nameTextBox; private Button okButton; private Label greetingLabel;
Activity: Add the InitializeMyControls() method to instantiate, set properties, and add the controls to the form's Controls collection
InitializeMyControls() C# method into your MainForm class
private void InitializeControls()
{
//Initialize the text box
nameTextBox = new TextBox();
nameTextBox.Location = new Point(50, 50);
//Initialize the ok button
okButton = new Button();
okButton.Location = new Point(60, 100);
okButton.Text = "OK";
//Initialize the hello greeting
greetingLabel = new Label();
greetingLabel.Location = new Point(50, 150);
greetingLabel.Text = "Hello ";
//Add the controls to the Form's Controls collection
this.Controls.Add(nameTextBox);
Controls.Add(okButton);
Controls.Add(greetingLabel);
}
Activity: Modify the constructor method to call InitializeMyControls()
this.InitializeControls();
Most controls have user-initiated events associated with them. In our simple example, users can type in text into the nameTextBox or click on the okButton.
Events such as these are noticed by the operating system and, if your program desires to take action to handle the event, code must be written to instruct the program on what to do.
Two steps are required to handle events:
Activity: Write the event-handler method for the button click
okButton_Click method into your MainForm class
private void okButton_Click(object sender, System.EventArgs e)
{
//Concatenate the user's name to the greeting label
greetingLabel.Text += nameTextBox.Text;
}
Activity: Add the okButton_Click event handler method to the okButton's Click property (in this case it is the only one) in the InitializeMyControls() method
//Initialize the ok button
...
okButton.Click += new System.EventHandler(okButton_Click);
okButton_Click event handler method to place the user's name in the window's Title bar caption