240 likes | 368 Views
Graphical User Interface Components Version 1.1. Obectives. Students should understand how to use these basic Form components to create a Graphical User Interface (GUI) application. Form TextBox Label Button ListBox MessageBox RichTextBox MenuStrip.
E N D
Obectives Students should understand how to use these basicForm components to create a Graphical User Interface (GUI) application Form TextBox Label Button ListBox MessageBox RichTextBox MenuStrip
These windows controls are discussed in great detail at http://msdn.microsoft.com/en-us/library/k50ex0x9.aspx
Form Control A Form control is created when your GUI application is created by Visual Studio. A form encapsulates your main program and provides a frame, title bar with: icon, text, minimize, maximize/restore and close buttons and a client area containing your GUI controls.
TextBox Control A TextBox control is typically used to get/set a single line of text 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 a TextBox control named “Txtjoe” , you could get the integer by writing int value = int.Parse(Txtjoe.Text); Note that we use three or four charaters to indicate the type of control “Txt” in this case.
TextBox Control Example Given that you want to output a double value, “money” formatted as 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 TextBox control 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” formatted as 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 the Label 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 When the Button is clicked, this event handler reads data from TxtBox1, parses it to an integer, and then writes it out to TxtBox2. private void BtnTest1_Click(object sender, EventArgs e) { string x = TxtBox1.Text; int num = int.Parse(x); TxtBox2.Text = string.Format("Answer = {0}", num); }
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 MessageBoxat design time. Message boxes are created by using the method MessageBox.Show( ). There are several forms of this method, for example // message box with text MessageBox.show(string); // message box with text and caption MessageBox.Show(string, string); // message box with text, caption, and buttons MessageBox.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 click comes 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 Control A RichTextBox control displays one or more lines of text are used to give output or receive input from the user. // Adding text to a RichTextBox Control RTxt.Append(string); // Clearing a RichTextBox Control RTxt.Clear();
Menu Controls To add a menu bar to your form follow these steps
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.
Create an event handler by double clicking on one of the items in your menu private void exitToolStripMenuItem1_Click(object sender, EventArgs e) { this.Close(); }