After completion of today's lab, you should be able to
In addition, an introduction to the Program 1 assignment will be covered.
Message Boxes are secondary windows used to interact with a user outside a primary window. They are commonly used to present brief messages or to request confirmation for actions.
Confirmation takes input from command buttons.
A MessageBox is a predefined dialog box that displays application-related information to the user. Message boxes are also used to request information from the user.
Message boxes are used quite often to display "error messages" to a user
MessageBox.Show("You must enter your name", //message displayed "Microsoft Word", //caption in title bar of window MessageBoxButtons.OK, //type of buttons included MessageBoxIcon.Exclamation); //type of icon displayed
There are six enumerated types of MessageBoxButtons defined: AbortRetryIgnore, OK, OKCancel, RetryCancel, YesNo, and YesNoCancel
There are nine enumeration types of MessageBoxIcons: Asterisk, Error, Exclamation, Hand, Information, None, Question, Stop, Warning, and None (although several of them use the same icon)
Create a New Project called WindowsUI in Solution Lab3
WindowsUI as the project name, U:\Labs as the Location, and Lab3 as the solution name.MainForm.csMessageBox
Create an About Box MessageBox
Add an About Button to your MainForm
MainForm. Set its
aboutButtonaboutButton
MessageBox with your name as the authorIf you finish early, experiment with the other
Message boxes are also frequently used to get simple yes/no responses from the user
DialogResult result = MessageBox.Show("Do you really want to erase your hard disk?",
"Norton Utility",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
MessageBox.Show("Hard Disk Erased");
Where DialogResult has eight enumerated values: None, OK, Cancel, Abort, Retry, Ignore, Yes, and No
Dialog Boxes are secondary windows used to interact with a user outside a primary window. .NET includes a number of pre-defined dialog boxes (e.g. Open, Save As, Print) that we can use in our Windows applications.
Creating your own custom dialog box is done through the declaration of a user-defined class derived from System.WindowsForms.Form class, instantiating an object of that type in a primary window, and then calling its Show method.
A typical use of a custom dialog box is to collect user input.
When creating dialog boxes, the following traits are typically set:
Fixed DialogFalseFalse
Add a custom dialog box to your Windows Application
AboutForm
FixedDialogFalseFalseokButtonOK"OKCall your custom dialog box from your primary window
aboutButton_Click method so that the button click instantiates your AboutForm class and calls it using the ShowDialog method
private void aboutButton_Click(object sender, EventArgs e)
{
AboutForm myAboutBox = new AboutForm();
myAboutBox.ShowDialog();
}
Pretty simple, huh? Almost all applications need an About Dialog accessible from the Help menu. See SSW's guidelines for an effective About Dialog.
Visual Studio 2008 has a pre-defined About Box form you can use in your application.
Demo: add a new About Box item to your application and view its controls with the Document Outline view
There are two types of dialog boxes: modeless and modal.
Most dialog boxes are set up as modal to constrain the user what the user can do next. Many user interface gurus claim that modeless is preferred in many cases. Modeless keeps the user in control of the actions, rather than forced to finish or cancel the dialog box.
To display a dialog box as modeless, use the Show( ) method in place of the ShowDialog( ) method.
You will also need to explicitly handle the button clicks on modeless form.
TopMost property to True
private void okButton_Click(object sender, System.EventArgs e)
{
//Do any processing required in modeless dialog box
this.Close();
}
A common technique is to create a modal dialog box to obtain input from the user.
As a simple example, let us create a dialog box so that users can enter their personal information. To keep things easy, we will only have them enter their name and age.
Add a C# Code Class to your project named Customer
Customer.cspublic (from none)public class Customer { //instance variables private string name; private int age; //constructor public Customer() { name = ""; age = 0; } //Name property public string Name { get { return name; } set { name = value; } } //Age property public int Age { get { return age; } set { age = value; } } }
Add a custom dialog box to collect user input
UserInputForm
FixedDialogFalseFalsenameTextBox, ageTextBox, okButton, cancelButtonokButton's DialogResult property to OK and the cancelButton's DialogResult property to CancelokButton to generate a Click event handler methodModify the code in UserInputForm class in to
user of type CustomeruserokButton_Click to save the user's name and age
public partial class UserInputForm : Form
{
//instance variable
private Customer user;
//constructor method
public UserInputForm(Customer cust)
{
InitializeComponent();
user = cust;
}
//Save the user's name and age
private void okButton_Click(object sender, EventArgs e)
{
user.Name = nameTextBox.Text;
user.Age = Int32.Parse(ageTextBox.Text);
}
}
Note: Since this is a prototype, error checking of input is ignored
Modify the MainForm to call the UserInputForm
MainForm. Set the button's
addCustomerButtonaddCustomerButton_Click event handler method
private void addCustomerButton_Click(object sender, EventArgs e)
{
//declare and instantiate a new customer
Customer newCustomer = new Customer();
//instantiate and call the dialog box
UserInputForm userData = new UserInputForm(newCustomer);
DialogResult result = userData.ShowDialog();
//display the new customer data entered in the dialog box
if (result == DialogResult.OK)
{
MessageBox.Show(String.Format(
"Name is {0}\nAge is {1}", newCustomer.Name, newCustomer.Age),
"New User",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
Introduction to Program 1 assignment