csci 3328 object oriented programming in c# chapter 10: graphical user interfaces with windows forms...
TRANSCRIPT
![Page 1: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/1.jpg)
CSCI 3328 Object Oriented CSCI 3328 Object Oriented Programming in C# Programming in C#
Chapter 10: Graphical User Chapter 10: Graphical User Interfaces with Windows FormsInterfaces with Windows Forms
1
Xiang Lian
The University of Texas – Pan American
Edinburg, TX 78539
![Page 2: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/2.jpg)
Objectives
• In this chapter, you will– Learn how to process events that are generated by user
interface controls and event handling– Know how to create and use Button, Label,
RadioButton, CheckBox, TextBox, Panel and NumbericUpDown controls
– Know how to add ToolTips to GUI controls– Discover how to process mouse and keyboard events– Become familiar with more controls, such as Menus,
tabbed windows, and multiple document interface (MDI), ListView, TreeView, LinkLabel, ListBox, ComboBox, DateTimePicker
2
![Page 3: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/3.jpg)
Windows Forms
• Dialog• MDI (window multiple document Interface)
Window• Controls and components are placed on the
form• Toolbox allows you to choose controls and
components• A form is a container that holds the controls
and components3
![Page 4: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/4.jpg)
Controls
4
![Page 5: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/5.jpg)
Event Handling
• Events drive the program to perform a task– Click– Change– Time
• Event handler is what you write
5
![Page 6: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/6.jpg)
Example of Event-Driven GUI
• Click event of Button
private void clickButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Button was clicked");
}
6
![Page 7: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/7.jpg)
Visual Studio Generated Code
• Can give great insight into what is going on• See what is in the Designer.cs
– It will have all controls and components you placed there
7
![Page 8: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/8.jpg)
Integrated Development Environment
• Events
Properties window
events
8
![Page 9: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/9.jpg)
Creating Event Handlers
• Double click on a control and write code– Known as default event
• You can use the properties window to create event handlers
• Click on the events (looks like the lightning) in the properties window
9
![Page 10: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/10.jpg)
Control Properties and Layout
• Controls derive from class Control• Common control properties
– BackColor– BackgroundImage– Enabled– Focused– Font– ForeColor– TabIndex– Text– Visible
• Common methods– Hide– Select– Show
10
![Page 11: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/11.jpg)
Anchor Property
• Anchoring causes controls to remain at a fixed distance from the sides of the container, even when the container is resized– Darkened bar indicate the container’s sides to which the
control is anchored
11
![Page 12: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/12.jpg)
Dock Property and Other Properties
12
• Dock
• Other properties– Location– Size– MaximumSize– MinimumSize
![Page 13: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/13.jpg)
Labels, TextBoxes and Buttons
• A Label displays text that the user cannot directly modify
• A TextBox is an area in which either text can be displayed by a program or user can type text via keyboard– A password TextBox
• Set UseSystemPasswordChar to true
• A Button is a control that the user clicks to trigger a specific action or to select an option in a program
13
![Page 14: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/14.jpg)
Common Properties/Events of Labels, TextBoxes and Buttons
• Label– Font, Text, TextAlign
• TextBox– AcceptReturn
• If true in a multiline TextBox, pressing Enter in the TextBox creates a new line
– Multiline, ReadOnly, ScrollBars, Text, UseSystemPasswordChar
– Event: TextChanged• Button
– Text, FlatStyle– Event: Click
14
![Page 15: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/15.jpg)
GroupBoxes and Panels
• GroupBoxes and Panels arrange controls on a GUI• All controls in a GroupBox or Panel move together
when the GroupBox or Panel is moved• Properties of GroupBoxes
– Controls– Text
• Properties of Panels– AutoScroll (when resizing panels)– BorderStyle– Controls
15
![Page 16: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/16.jpg)
CheckBoxes and RadioButtons
• CheckBoxes– Appearance, Checked, CheckState, Text
– ThreeState• Checked, unchecked, and indeterminate (grey color; can be set
programmatically)
– Event: CheckedChanged, CheckStateChanged
• RadioButtons– Checked, Text
– Event: CheckedChanged
16
![Page 17: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/17.jpg)
Example of CheckBoxes and RadioButtons
private void boldCheckBox_CheckedChanged(object sender, EventArgs e)
{
outputLabel.Font = new Font(outputLabel.Font, outputLabel.Font.Style ^FontStyle.Bold);
}
-------------------------------------------------------------------------------------
private MessageBoxButtons buttonType;
private void buttonType_CheckedChanged(object sender, EventArgs e)
{
if (sender == okRadioButton)
buttonType = MessageBoxButtons.OK;
}
17
name of a radio button
![Page 18: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/18.jpg)
PictureBox
• Properties– Image
– SizeMode
• Events– Click
• Example of setting image resources– imagePictureBox.Image=(Image)
(Properties.Resources.ResourceManager.GetObject("image1");
18
![Page 19: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/19.jpg)
ToolTips
• Drag ToolTip to the form– Then each control has a new property "ToolTip on toolTip1"
• Properties– AutoPopDelay
• The amount of time that the tool tip appears while the mouse is over a control– InitialDelay
• The amount of time that a mouse must hover over a control before a tool tip appears
– ReshowDelay: • The amount of time between which two different tool tips appear
• Load Method of the form– tooltip1.SetToolTip(this.button1, "tips here");
• Event– Draw
19
![Page 20: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/20.jpg)
NumericUpDown Control
• Properties– DecimalPlaces
– Increment
– Maximum
– Minmum
– UpDownAlign
– Value
• Event– ValueChanged
20
![Page 21: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/21.jpg)
Mouse Handlingprivate void PainterForm_MouseDown(object sender, MouseEventArgs e){ shouldPaint = true;} private void PainterForm_MouseUp(object sender, MouseEventArgs e){ shouldPaint = false;} private void PainterForm_MouseMove(object sender, MouseEventArgs e){ if (shouldPaint) {
Graphics graphics = CreateGraphics();graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);graphics.Dispose(); //dispose of resources
}}
21
Location of the mouse
![Page 22: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/22.jpg)
Mouse Handling (cont'd)
• Event EventArgs– MouseEnter– MouseLeave
• Event MouseEventArgs– MouseDown– MouseHover– MouseMove– MouseUp
• Properties of class MouseEventArgs– Button (Left, Right, Middle or none button of the mouse)– Clicks (number of clicking times)– X– Y
22
![Page 23: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/23.jpg)
Keyboard Events• Event KeyEventArgs
– KeyDown– KeyUp
• Event KeyPressEventArgs– KeyPress
• Properties of class KeyPressEventArgs– KeyChar– Handled (whether the event was handled)
• Properties of class KeyEventArgs– Alt (whether Alt key was pressed)– Control (whether Ctrl key was pressed)– Shift (whether Shift key was pressed)– Handled (whether the event was handled)
23
![Page 24: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/24.jpg)
Menus
• Main menu bar– Type menu name
– Place & character before a letter to underline it• E.g., &File
• Effect: File (you can press Alt+F keys to select file menu)
– Select shortcut keys for menu items
– You can remove a menu item by
selecting it with mouse and pressing
Delete key
24
![Page 25: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/25.jpg)
Menus (cont'd)
• Options of menu items– Right click a menu item
– ToolStripMenuItem
– ComboBox
– Separator
– TextBox
25
![Page 26: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/26.jpg)
Menus (cont'd)
• MenuStrip Properties– HasChildren
– RightToLeft
• ToolStripMenuItem Properties– Checked
– CheckOnClick
– Index
– MenuItems
• ToolStripMenuItem Event– Click
26
![Page 27: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/27.jpg)
Example of Menus
• Checked Property– blackToolStripMenuItem.Checked = true;
• Eventprivate void blackToolStripMenuItem_Click(object sender, EventArgs e)
{
displayLabel.ForeColor = Color.Black;
blackToolStripMenuItem.Checked = true;
}
27
![Page 28: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/28.jpg)
MonthCalendar Control
• Properties– FirstDayOfWeek– MaxDate– MaxSelectionCount
• Maxixmum number of dates that can be selected at once– MinDate– MonthlyBoldedDates
• An array of dates that will be displayed in bold in the calendar– SelectionEnd– SelectionRange– SelectionStart
• Event– DateChanged
28
![Page 29: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/29.jpg)
DateTimePicker Control
• Properties– CalendarForeColor– CalendarMonthBackground– CustomFormat– Date– Format– MaxDate– MinDate– ShowCheckBox– ShowUpDown– TimeOfDay– Value
• Event– ValueChanged
29
![Page 30: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/30.jpg)
Example of DateTimePicker
private void dateTimePickerDropOff_ValueChanged(object sender, EventArgs e)
{
DateTime dropOffDate = dateTimePickerDropOff.Value;
if (dropOffDate.DayOfWeek == DayOfWeek.Friday ||
dropOffDate.DayOfWeek == DayOfWeek.Saturday ||
dropOffDate.DayOfWeek == DayOfWeek.Sunday)
outputLabel.Text = dropOffDate.Add(3).ToLongDateString();
else
outputLabel.Text = dropOffDate.Add(2).ToLongDateString();
}
30
![Page 31: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/31.jpg)
LinkLabel Control
• Properties– LinkVisited
• If true, the link appears as though it has been visited
– LinkColor– Text– VisitedLinkColor
• Event– LinkClicked
• System.Diagnostics.Process.Start(@"C:\");• System.Diagnostics.Process.Start("http://faculty.utpa.edu/lianx");• System.Diagnostics.Process.Start("notepad");
31
![Page 32: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/32.jpg)
ListBox Control• Properties of ListBox
– SelectionMode– MultiColumn– SelectedIndex
• If no items are selected, SelectedIndex = 1– SelectedIndices
• For multiple selected items– SelectedItem– SelectedItems– Sorted
• Indicate whether items are sorted alphabetically
• Methods of ListBox– ClearSelected– GetSelected(index)
• Return true, if the corresponding item is selected
• Event of List Box– SelectedIndexChanged
32
![Page 33: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/33.jpg)
Example of ListBox Control
• myListBox.Items.Add(myListItem);
• CheckedListBox Control– Inherit from ListBox
33
![Page 34: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/34.jpg)
ComboBox Control
• Properties– DropDownStyle
– Items
– MaxDropDownItems
– SelectedIndex
– SelectedItem
– Sorted
• Event– SelectedIndexChanged
34
![Page 35: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/35.jpg)
Other Controls
• TreeView Control– "Nodes" property
• Add root, or add child
– AfterSelect event• When selected node changes
35
![Page 36: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/36.jpg)
Other Controls (cont'd)• TabControl Control
– Contains TabPage objects which are similar to Panels and GroupBoxes– myTabPage.Controls.Add(myControl);– myTabPage.Controls.Add(myTabPage);
• Properties– ImageList– ItemSize
• Tab size– Multiline– SelectedIndex– SelectedTab– TabCount– TabPages
• Add more tabs
• Event– SelectedIndexChanged
36
![Page 37: CSCI 3328 Object Oriented Programming in C# Chapter 10: Graphical User Interfaces with Windows Forms 1 Xiang Lian The University of Texas – Pan American](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c725503460f94924780/html5/thumbnails/37.jpg)
37