Windows Programming Using C#
Forms Programming I
Contents
2
Forms Programming
3
System.Drawing
4
System.Drawing.Point
5
System.Drawing.Size
6
System.Drawing.Rectangle
7
System.Drawing.Color
8
System.Drawing.Font
9
System.Drawing.FontStyle
10
System.Windows.Forms
11
Form Class
12
Form Properties
13
A collection of controls owned by the form
Controls
Whether maximized, minimized or normal
WindowState
Size when initially created
DefaultSize
Maximum size window can be resized to
MaximumSize
Minimum size window can be resized to
MinimumSize
Size of drawing area without borders or scrollbars
ClientSize
Foreground or drawing color
ForeColor
Background color
BackColor
DPI resolution of display it was built for. Will be scaled to look correct on other displays.
AutoScaleDimensions
Text displayed or caption
Text
Size of form in pixels
Size
Point of to left corner
Location
Description
Property
Form Events
void EventHandler(object sender, EventArgs e)
14
Form Events
15
Resize operation has ended
ResizeEnd
Resize operation has begun
ResizeBegin
Occurs when a form is first displayed
Shown
When the form is actually closed
Closed
Just before the form is closed
Closing
Just before form is loaded the first time
Load
Description
Event
Form Methods
16
Moves to top of stacking order
BringToFront
Makes the form visible
Show
Makes the form invisible
Hide
Gives the form focus
Focus
Closes the form
Close
Activates the window and gives it focus
Activate
Description
Method
Creating Windows Applications
17
Creating Windows Applications
18
Creating Windows Applications
public class GreetingForm : Form {
Label greetingLabel;
Button cancelButton;
…
}
19
Creating Windows Applications
greetingLabel = new Label();
greetingLabel.Location = new Point(16, 24);
greetingLabel.Text = "Hello, World";
greetingLabel.Size = new Size(216, 24);
greetingLabel.ForeColor = Color.Black;
20
Creating Windows Applications
cancelButton = new Button();
cancelButton.Location = new Point(150, 200);
cancelButton.Size = new Size(112, 32);
cancelButton.Text = "&Cancel";
cancelButton.Click += new EventHandler(cancelButton_Click);
21
Creating Windows Applications
this.AutoScaleDimensions = new SizeF(95.0f, 95.0f);
this.ClientSize = new Size(300, 300);
this.Text = "Hello, World";
22
Creating Windows Applications
this.Controls.Add(cancelButton);
this.Controls.Add(greetingLabel);
protected void cancelButton_Click(
object sender, EventArgs e) {
Application.Exit();
}
23
Visual Studio Designer
24
CheckBoxes
25
GroupBox
26
Panels
27
Radio Buttons
28
Radio Buttons
29
TextBox
30
TextBox
31
File Dialog
32
File Dialog
33
File Dialog
if (openDialog.ShowDialog() == DialogResult.OK) {
Image img = Image.FromFile(openDialog.FileName);
pictureBox1.Image = img;
}
34
Image Class
35
PictureBox Class
36
ToolTips
37
ToolTips
38
ToolTips
39
NumericUpDown
40
MonthCalendar
41
DateTimePicker
42
System.DateTime Structure
43
DateTime
44
DateTime
45
DateTime
46
ListBox
47
ListBox
48
ListBox
49
Populating a ListBox
for(int i = 0; i < 50; i++) {
listBox1.Items.Add(
"Item " + i.ToString());
}
50
ComboBox
51
ComboBox
52
ComboBox
53