Presentation is loading. Please wait.

Presentation is loading. Please wait.

Self Study GUI Properties & Events

Similar presentations


Presentation on theme: "Self Study GUI Properties & Events"— Presentation transcript:

1 Self Study GUI Properties & Events
CS1S467 GUI Programming Self Study GUI Properties & Events

2 Visual Studio Controls
(Button, CheckBox, Label,…) Properties (colour, position, visible,…) Events (clicked, dragged, changed,…)

3 Changing Properties/Events
Via Visual Studio GUI (simple to do) Controls (Button, CheckBox, Label,…) Properties (colour, position, visible,…) Events (clicked, dragged, changed,…) Programmatically (a bit more tricky)

4 PART 1: Properties

5 Changing Properties Programmatically
Simple OiO Constructors OiO Properties 3 Groups of Properties Simple, direct (e.g. ProgressBar value > int, Button visible > boolean) Using Object-in-Object Constructors (fonts, points) Using Object-in-Object Properties (colour, text position)

6 Changing Properties Programmatically
Simple OiO Constructors OiO Properties Copying a Boolean Property Setting directly private void visible_cBx _CheckedChanged(object sender, EventArgs e) { button1.Visible = visible_cBx.Checked; } button1.Visible = true; button1.Visible = false;

7 Changing Properties Programmatically
Simple OiO Constructors OiO Properties Using Object-in-Object Constructors GUI Controls are made from many sub-objects (lines, areas, fonts, …) Constructors create new objects (note the keyword 'new') These new objects can then be used to replace existing objects (e.g. an old font by a new font)

8 Changing Properties Programmatically
Simple OiO Constructors OiO Properties Using Object-in-Object Constructors (here: the 'Font' constructor) private void bold_cBx_CheckedChanged(object sender, EventArgs e) { if (bold_cBx.Checked) button1.Font = new Font(button1.Font.Name, button1.Font.Size+4, FontStyle.Bold); } else button1.Font = new Font(button1.Font.Name, button1.Font.Size-4 ,FontStyle.Regular);

9 Changing Properties Programmatically
Simple OiO Constructors OiO Properties Using Object-in-Object Constructors (here: the 'Point' constructor) private void xPos_UpDown_ValueChanged(object sender, EventArgs e) { button1.Location = new Point( (int) xPos_UpDown.Value, 77); }

10 Changing Properties Programmatically
Simple OiO Constructors OiO Properties Using Object-in-Object Properties (here: properties of the 'Color' class) private void backColour_comboBx_SelectedIndexChanged(object sender, EventArgs e) { if (backColour_comboBx.SelectedIndex == 0) button1.BackColor = Color.Red; if (backColour_comboBx.SelectedIndex == 1) button1.BackColor = Color.Green; if (backColour_comboBx.SelectedIndex == 2) button1.BackColor = Color.Blue; }

11 Changing Properties Programmatically
Simple OiO Constructors OiO Properties Copying Object-in-Object Properties private void textColourBtn_Click(object sender, EventArgs e) { // Show the colour dialog. DialogResult result = colorDialog1.ShowDialog(); // See if user pressed ok or cancel. if (result == DialogResult.OK) // Set button text colour to the selected color. button1.ForeColor = colorDialog1.Color; } Although dragged onto Form … …….… it will appear here

12 Changing Properties Programmatically
Simple OiO Constructors OiO Properties Using Object-in-Object Properties (here: properties of the 'ContentAlignment' class) private void radioButton1_CheckedChanged(object sender, EventArgs e) { button1.TextAlign = ContentAlignment.BottomLeft; } private void radioButton2_CheckedChanged(object sender, EventArgs e) button1.TextAlign = ContentAlignment.MiddleCenter; private void radioButton3_CheckedChanged(object sender, EventArgs e) button1.TextAlign = ContentAlignment.TopRight;

13 PART 2: Events

14 Control Events When double-clicking on a Control
the default event handler method stub will be created: private void button1_Click(object sender, EventArgs e) { } once an event handler has been created you can't change the control name any more without an unwanted side effect: control name changes event handler name doesn't private void button1_Click(..

15 Control Events Therefore
Give EVERY control a MEANINGFUL name immediately after creation: private void SortButton_Click(object sender, EventArgs e) { }

16 Control Events When double-clicking on a Control
usually the 'Click' event handler gets created: but not always: private void button1_Click(object sender, EventArgs e) { } private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejected… e) { }

17 Control Events To set other Event Handlers
double-click on the correct event private void maskedTextBox1_ModifiedChanged(object sender, EventArgs e) { }

18 Drag Event Handling Controls Events Form1 label1 label2 MouseDown
MouseMove MouseUp

19 Drag Event Handling private Point MouseDownLocation; // stores starting X and Y position private void label1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) MouseDownLocation = e.Location; Cursor.Current = Cursors.Hand; } private void label1_MouseMove(object sender, MouseEventArgs e) label1.Left = e.X + label1.Left - MouseDownLocation.X; label1.Top = e.Y + label1.Top - MouseDownLocation.Y; label2.Text = "Dragging"; private void label1_MouseUp(object sender, MouseEventArgs e) label2.Text = "Dropped";


Download ppt "Self Study GUI Properties & Events"

Similar presentations


Ads by Google