46
1 Windows Forms II Chapter 20

Windows Forms II

  • Upload
    hei

  • View
    92

  • Download
    1

Embed Size (px)

DESCRIPTION

Windows Forms II. Chapter 20. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Windows Forms II

1

Windows Forms II

Chapter 20

Page 2: Windows Forms II

2

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

Page 3: Windows Forms II

3

GroupBox Properties

Text Property

Location and size are normally set interactively via the designer.

The usual appearance properties are available.

Page 4: Windows Forms II

4

RadioButton Properties

Checked is true for a button that is selected. (Max of 1 per group.)

Text

Page 5: Windows Forms II

5

Add an OK Button

Page 6: Windows Forms II

6

Code for Button Click Event

enum Classification {Unknown, Freshman, Sophomore, Junior, Senior};

Classification year = Classification.Unknown;

private void btnOK_Click(object sender, System.EventArgs e){ if (rbFreshman.Checked) year = Classification.Freshman; else if (rbSophomore.Checked) year = Classification.Sophomore; else if (rbJunior.Checked) year = Classification.Junior; else if (rbSenior.Checked) year = Classification.Senior;

if (year == Classification.Unknown) MessageBox.Show("Please select year"); else MessageBox.Show("You will be recorded as a " + year.ToString());}

End of Section

Page 7: Windows Forms II

7

The PictureBox Control

Allows you to include images on your form.

Typically a jpeg or gif file Other possibilities.

Important Properties: Image (Browse to file) Location Size SizeMode

Normal, StretchImage, AutoSize, CenterImage, Zoom BorderStyle

Page 8: Windows Forms II

8

The PictureBox Control

Need an image file? Try google > images

From search on University of South Florida

Image file is in Downloads area of class web site: http://www.cse.usf.edu/~turnerr/Software_Systems_Develo

pment/Downloads/USF_Bull.gif

Download to desktop

Page 9: Windows Forms II

9

The PictureBox Control

Page 10: Windows Forms II

Setting the Image Property

Select the Image property.

Click on the elipses (...) button. 10

Page 11: Windows Forms II

Setting the Image Property

Click on Import.

11

Page 12: Windows Forms II

Setting the Image

Navigate to the image file.

Select the file and click Open.

12

Page 13: Windows Forms II

Setting the Image

Click OK.

13

Page 14: Windows Forms II

Setting the Image

The image file has been copied into the project.

14

Page 15: Windows Forms II

15

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

Page 16: Windows Forms II

The PictureBox Control

Set size mode to Zoom.

Build and run. 16

Page 17: Windows Forms II

17

SizeMode Zoom

SizeMode = Zoom

Page 18: Windows Forms II

18

SizeMode Normal

SizeMode = Normal

Page 19: Windows Forms II

19

CenterImage

SizeMode = CenterImage

Page 20: Windows Forms II

20

AutoSize

SizeMode = Autosize

Most of the PictureBox falls outside the form.

Page 21: Windows Forms II

AutoSize

Window resized to hold entire PictureBox control. 21

Page 22: Windows Forms II

22

The PictureBox Control

Conclusions: Try to match size of actual image to size

that you need on the form.

If not, Zoom is usually the best bet.

But, at least, be sure the ratio of height to width is about the same.

End of Section

Page 23: Windows Forms II

Where is the image?

It's embedded in the program

You don't need to retain or deploy the original image file. 23

Page 24: Windows Forms II

Form1.resx

In the project folder, you can drill down and find the file.

24

Page 25: Windows Forms II

Form1.resx

25

Page 26: Windows Forms II

Form1.resx

26

Page 27: Windows Forms II

Form1.resx

27

Page 28: Windows Forms II

Form1.resx

End of Section28

Page 29: Windows Forms II

29

The ComboBox Control

“Combination” of text entry box and dropdown list. Select from list or enter text.

Important Properties: Items (Collection of strings to display.)

Can be set using the designer or by program. DropDownStyle

Simple Text Entry Only DropDown Text Entry or Select DropDownList Select Only

Text Whatever was selected or entered

Page 30: Windows Forms II

30

The ComboBox Control

Set name to cbYear.

Page 31: Windows Forms II

Setting the Choices

31

Page 32: Windows Forms II

Setting the Choices

Click OK.

32

Page 33: Windows Forms II

Getting the User’s Choice

Build and run.

33

Page 34: Windows Forms II

Program Running

34

Page 35: Windows Forms II

35

The ComboBox Control

After clicking on the arrow:

Page 36: Windows Forms II

36

The ComboBox Control

After user selects Sophomore

Page 37: Windows Forms II

Getting the User’s Choice

End of Section

37

Page 38: Windows Forms II

38

Handling the FormClosing Event

In Design View, select the form (by clicking anywhere on the background.)

In the Properties window, select Events. (Lightening bolt icon at top of window.)

Beside “FormClosing” type the name that you want to give to the event handler (e.g., FormIsClosing) Don't use the event name, FormClosing.

Press Enter.

Page 39: Windows Forms II

Setting the Event Handler

39

Page 40: Windows Forms II

40

Handling the FormClosing Event

This creates the following stub in the code file:

private void FormIsClosing(

object sender, System.ComponentModel.CancelEventArgs e)

{

}

This function will be called when the user clicks the Close button on the form.

Page 41: Windows Forms II

41

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);

e.Cancel = (result == DialogResult.No);

Page 42: Windows Forms II

42

MessageBox Example

MessageBox.Show("Are you sure you want to quit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

Page 43: Windows Forms II

43

Add a Close Button

Page 44: Windows Forms II

44

Close Button Click Event Handler

private void btnClose_Click(object sender, EventArgs e)

{

this.Close();

}

Page 45: Windows Forms II

45

We still get the FormClosing Event

End of Section

Page 46: Windows Forms II

46

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