Graphical User Interface Components Version 1.1. Obectives Students should understand how to use...

Post on 18-Jan-2018

217 views 0 download

description

These windows controls are discussed in great detail at

Transcript of Graphical User Interface Components Version 1.1. Obectives Students should understand how to use...

Graphical User InterfaceComponents

Version 1.1

Obectives

Students should understand how to use these basic Form componentsto create a Graphical User Interface (GUI) application

FormTextBoxLabelButtonListBoxMessageBoxRichTextBoxMenuStrip

These windows controls are discussed in great detail at

http://msdn.microsoft.com/en-us/library/k50ex0x9.aspx

A Form control is created when your GUI application iscreated by Visual Studio. A form encapsulates your mainprogram and provides a frame, title bar with: icon, text, minimize, maximize/restore and close buttons and a client area containing your GUI controls.

Form Control

TextBox Control

A TextBox control is typically used to get/set a single line oftext from/to the user.

The most important member of a TextBox is its Text property.The Text property of a TextBox control is a getter/setter string object. You can operate (via get/set) on this string just like any other string object.

TextBox Control Example

Given that the user has typed an integer value into aTextBox control named “Txtjoe” , you could get the integerby writing

int value = int.Parse(Txtjoe.Text);

Note that we use three or four charaters to indicate the typeof control “Txt” in this case.

TextBox Control Example

Given that you want to output a double value, “money” formattedas currency in a TextBox named “Txtjoe”, you could write Txtjoe.Text = string.Format(“{0:C}”, money);

Label Control

Label controls are typically used to provide descriptive text for a control. For example, you can use a Label to add descriptive text for a TextBoxcontrol to inform the user about the type of data expected. Label controls can also be used to add descriptive text to a Form to provide the user with helpful information. For example, you can add a Label to the top of a Form that provides instructions to the user on how to input data in the controls on the form. Label controls can be also used to output data to your Form.

Label Control Example

Given that you want to output a double value, “money” formattedas currency in a Label named “Lbljoe”, you could write Lbljoe.Text = string.Format(“{0:C}”, money);

Note that the Text property is a getter/setter property of theLabel control.

Button Control

Button controls are typically used to let the user initiate some action.When you click on a Button with the mouse, it generates a Click event.

Button Example

private void BtnTest1_Click(object sender, EventArgs e) { string x = TxtBox1.Text; int num = int.Parse(x); TxtBox2.Text = string.Format("Answer = {0}", num);}

When the Button is clicked, this event handler readsdata from TxtBox1, parses it to an integer, and thenwrites it out to TxtBox2.

ListBox Control

A ListBox control provides the user with a list of items.An event is generated when one of these items is selected.

You can provide the list if items to be shown at design time,or they can be added at run time using the Add method.

ListBox Example

private void LstBox1_SelectedIndexChanged(object sender, EventArgs e){ if (listBox1.SelectedIndex != -1) switch (LstBox1.SelectedIndex) { case 0: TxtBox2.Text = "Pizza"; break; case 1: TxtBox2.Text = "Hamburger"; break; case 2: TxtBox2.Text = "Salad"; break; case 3: TxtBox2.Text = "Drink"; break; case 4: TxtBox2.Text = "Hot Dog"; break; case 5: TxtBox2.Text = "Apple Pie"; break; }// end switch}

MessageBox Control

A MessageBox control displays a Modal Dialog Box some text and optionally a Button and an icon. Message boxes are used to provide feedback information to the user.

You cannot add a MessageBox at design time. Message boxes arecreated by using the method MessageBox.Show( ). There are several forms of this method, for example

// message box with textMessageBox.show(string);

// message box with text and captionMessageBox.Show(string, string);

// message box with text, caption, and buttonsMessageBox.Show(string, string, MessageBoxButtons);

MessageBoxButtons

* OK The message box contains an OK button.

* OKCancel The message box contains OK and Cancel buttons.

* AbortRetryIgnore The message box contains Abort, Retry, and Ignore buttons.

* YesNoCancel The message box contains Yes, No, and Cancel buttons.

* YesNo The message box contains Yes and No buttons.

* RetryCancel The message box contains Retry and Cancel buttons.

The response to a MessageBoxButton clickcomes back as a DialogResult.

MessageBox Example

private void BtnQuit_Click(object sender, EventArgs e){ DialogResult response = MessageBox.Show("Do you really want to quit?",Quit", MessageBoxButtons.YesNo); if (response == DialogResult.Yes) this.Close(); }

RichTextBox ControlA RichTextBox control displays one or more lines of text are used togive output or receive input from the user.

// Adding text to a RichTextBox ControlRTxt.Append(string);

// Clearing a RichTextBox ControlRTxt.Clear();

Menu Controls

To add a menu bar to your form follow these steps

(1)Drag a MenuStrip from the tool box onto your form.

(2) Type then text that you want for the first item on the menu here.

You can continue to add 1st level menu items

Or add sub-menu items here.

Create an event handler by double clicking onone of the items in your menu

private void exitToolStripMenuItem1_Click(object sender, EventArgs e) { this.Close();}