Title: Graphical User Interfaces with Windows Forms
1Graphical User Interfaces with Windows Forms
2Visual Studio Form Designer
- For designing forms, changing the properties or
adding events to forms and form elements can be
achieved with the Visual Studio Form Designer - Toolbox
- Properties
- Lets have a look!
3Visual Studio Form Designer
4Event Management
- In order to address handler methods we need a
delegate and an event defined for form objects - EventHandler object is a pre-defined delegate for
working with form events - Every form element and the form itself have
pre-defined events like Click, MouseClick,
Keydown, Keypressed
5Example Button Click Event
firstButton new Button() firstButton.Text
"Click Me!" firstButton.Location new Point(50,
50) firstButton.Size new Size(150,
50) firstButton.Click new EventHandler(firstBu
tton_Click) void firstButton_Click(object
sender, EventArgs e) MessageBox.Show("Butto
n clicked!")
Adding an event handler to button
Handler method for button click event
6Examining Designer Generated Code
- All element initializations like buttons or the
form itself occur under InitializeComponent()
method - Designer seperates the actual class and its
design using the partial class declaration - User class Form1.cs
- Designer generated class Form1.Designer.cs
7Some of Common Controls
Controls
Containers
- Button
- CheckBox
- ComboBox
- DateTimePicker
- Label
- ListBox
- RadioButton
- TextBox
- ToolTip
- Panel
- GroupBox
- SplitContainer
- TabControl
8Some General Properties and Events for Form
Controls
Events
Properties
- Location
- (Point)
- Text
- Name
- TabIndex
- Visible
- Enabled
- Dock
-
- Click
- MouseClick
- MouseOver
- MouseLeave
- KeyDown
- KeyPress
- Paint
- TextChanged
-
9Message Boxes
- Can be used for giving info to users or getting
input from them - Returns a DialogueResult object when a button is
clicked
10Hands On - Coordinate Displayer
- Display the x and y locations of the mouse
position on a tooltip
11Hands On Creating a Layout
- Create a layout with two columns and a
StatusStrip - On the left column insert a ListBox
- On the right column insert a TextBox
- As mouse enters a component show which component
entered on the StatusStrip - Add items to ListBox, when user clicks an item it
should be added to the TextBox
12Hands On Text Formatter
- Create a form with an editable text box
- Users should be able to change the style of the
text in the textbox using comboboxes,
radiobuttons, etc
13Working with Multiple Forms
- Multiple forms can be used in an application
- In order to call another form we call Show()
method of the Form class
MyForm frm2 new MyForm() frm2.Show()
- As you have the reference to the new form you can
change the properties of it using that reference
frm2.label1.Text "New form opened" frm2.Text
"This is a new form"
14Working with Multiple Forms - MDI Forms
- MDI forms can show other form instances inside
themselves - In order to create an mdi form
- Set the isMdiContainer property of the main
form to true - Then create a new form as usual and set its
MdiParent property to true and show the form
FrmChild frmChild new FrmChild()
frmChild.MdiParent this frmChild.Show()
15Hands On Multiple Forms
- Get the name and surname of the user via a form
and show the information with the date and time
in a new form - When main form is closing ask the user if he/she
is sure to exit
16Menus
- Two types of menu
- MenuStrip ? Menu residing at the top of forms,
contains MenuItem objects - ContextMenu ? Menu appears when mouse right
clicked on a component
17Best Practices Forms and Events
- In order to reach fields of a child form from the
main form, just use child forms reference - If a message will be sent from the child form to
the main form, use events - Child form will have an event to fire
- In the main form an event handler should be
registered to the the child form
18Hands On Customer Registration
- Create one main form and a child form
- In the main form insert a menu with an Add
Customer menu item - The child form will include text boxes for name
and surname of the customer - When a new customer is saved it should be listed
in a list box in your main form
19Hands On (Contd)
20Timer Control
- It is for calling methods in pre-defined time
intervals (so are like threads) - Properties
- Interval time intervals for methods to be
executed in milliseconds - Enabled for timer to execute, this property
should be set True - Tick Event Event fired when an interval is
complete