C Shop Coding
C Shop Coding
1
RadioButton / GroupBox Controls
Used to solicit a multiple choice input.
Radio buttons work as a group.
Selecting one unselects any previous choice.
The GroupBox defines the group.
Expand “All Windows Forms” in Toolbox.
Drag GroupBox to the form.
Drag four radio buttons from the Toolbox
and drop inside the GroupBox.
You can also copy and paste controls.
Ctrl-C, Ctrl-V
2
GroupBox Properties
Text Property
Location and size are normally set interactively via the designer.
Text
4
Add an OK Button
5
Code for Button Click Event
6
The PictureBox Control
Important Properties:
Image (Browse to file)
Location
Size
SizeMode
7
The PictureBox Control
Need an image file?
Try google > images
From search on University of South Florida
Download to desktop
8
The PictureBox Control
9
Setting the Image Property
Click on Import.
11
Setting the Image
12
Setting the Image
Click OK.
13
Setting the Image
14
SizeMode Values
From the Help page for SizeMode:
Valid values for this property are taken from the
PictureBoxSizeMode enumeration.
By default, in PictureBoxSizeMode.Normal mode, the Image is
placed in the upper left corner of the PictureBox, and any part
of the image too big for the PictureBox is clipped.
PictureBoxSizeMode.Zoom causes the image to be stretched or
shrunk to fit the PictureBox, while maintaining the aspect ratio.
Using the PictureBoxSizeMode.StretchImage value causes the
image to stretch to fit the PictureBox.
Using the PictureBoxSizeMode.AutoSize value causes the control
to resize to always fit the image.
Using the PictureBoxSizeMode.CenterImage value causes the
image to be centered in the client area.
15
The PictureBox Control
SizeMode = Zoom
17
SizeMode Normal
SizeMode = Normal
18
CenterImage
SizeMode = CenterImage
19
AutoSize
SizeMode = Autosize
Most of the PictureBox falls outside the form.
20
AutoSize
Conclusions:
Try to match size of actual image to size that
End of Section
22
Where is the image?
It's embedded in the program
24
Form1.resx
25
Form1.resx
26
Form1.resx
27
Form1.resx
28
End of Section
The ComboBox Control
30
Setting the Choices
31
Setting the Choices
Click OK.
32
Getting the User’s Choice
33
Program Running
34
The ComboBox Control
After clicking on the arrow:
35
The ComboBox Control
36
Getting the User’s Choice
End of Section
37
Handling the FormClosing Event
38
Setting the Event Handler
39
Handling the FormClosing Event
This creates the following stub in the code file:
This function will be called when the user clicks the Close button
on the form.
40
Using a MessageBox to Get User Input
Add this code inside the FormIsClosing function.
DialogResult result =
MessageBox.Show("Are you sure you want to quit?",
"Confirm",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
41
MessageBox Example
42
Add a Close Button
43
Close Button Click Event Handler
44
We still get the FormClosing Event
End of Section
45
Summany
Visual Studio makes it easy to create
simple Windows forms applications.
There is an enormous amount of
information about Windows forms.
Only a small amount of it is necessary in
order to create simple applications.
Need to be aware of what exists.
Need to be able to find what you need
when you need it.
End of Presentation
46