15
Dr Dat Tran - Week 2 Lectur Dr Dat Tran - Week 2 Lectur e Notes e Notes 1 Forms Forms Programming Graphical User Interfaces PG Programming Graphical User Interfaces PG (7110) (7110) University of Canberra University of Canberra School of Information Sciences & School of Information Sciences & Engineering Engineering

Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Embed Size (px)

Citation preview

Page 1: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 11

FormsForms

Programming Graphical User Interfaces PG (7110)Programming Graphical User Interfaces PG (7110)

University of CanberraUniversity of CanberraSchool of Information Sciences & EngineeringSchool of Information Sciences & Engineering

Page 2: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 22

Create a Shape FormCreate a Shape Form

public TestForm () { InitializeComponent();

System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath(); shape.AddEllipse(0, 0, this.Width, this.Height); this.Region = new System.Drawing.Region(shape); }

Page 3: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 33

Get a Value from Another FormGet a Value from Another Form

In Form 1private Form2 form2 = new Form2();

private void GetLabelControlInForm2(){ label1.Text = form2.Label2.Text;}

In Form 2public Label Label2{ get { return label2; }}

Page 4: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 44

Form EventsForm Events

LoadLoad: Occurs before a form is displayed for the first time : Occurs before a form is displayed for the first time ActivateActivate: Occurs when the form is activated in code or by : Occurs when the form is activated in code or by the user the user DeactivateDeactivate: Occurs when the form loses focus and is no : Occurs when the form loses focus and is no longer the active form longer the active form ClosingClosing: Occurs when the form is closing : Occurs when the form is closing ClosedClosed: Occurs when the form is closed: Occurs when the form is closedShownShown: Occurs whenever the form is first displayed : Occurs whenever the form is first displayed SizeChangedSizeChanged: Occurs when the Size property value : Occurs when the Size property value changes changes

Page 5: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 55

Create a Form Event HandlerCreate a Form Event Handler

double-clickdouble-click

clickclick

private void MDIForm_Load(object sender, EventArgs e){

}

Page 6: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 66

Add a New FormAdd a New Form

At design timeAt design time

At run timeAt run timeDouble-click on the form to add Double-click on the form to add

the Load event handler and the Load event handler and modify it as followsmodify it as follows

Try ShowDialog()Try ShowDialog()

Page 7: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 77

Owner and Owned FormsOwner and Owned Forms

If If ShowDialog()ShowDialog() is used: is used:– Currently active form: Owner formCurrently active form: Owner form– New form: Owned formNew form: Owned form

Show()Show() does not establish an implicit owner- does not establish an implicit owner-owned relationship.owned relationship.In the modeless case, establish the relationship In the modeless case, establish the relationship as follows:as follows:

OwnedForm ownedForm = = new OwnedForm(); ownedForm.Owner = this;

Page 8: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 88

Owner and Owned FormsOwner and Owned Forms private Color colour = Color.Yellow; . . .private void ChangeColorButton_Click(object sender, EventArgs e){ colour = Color.Red;}

public Color Colour{ get { return colour; }}

Page 9: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 99

Owner and Owned FormsOwner and Owned Forms

OwnedForm owned; public OwnerForm(){ InitializeComponent(); owned = = new OwnedForm(); owned.Owner = this; owned.Show(); panel1.BackColor = owned.Colour;}

private void UpdateButton_Click(object sender, EventArgs e){ panel1.BackColor = owned.Colour; }

Page 10: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 1010

Message BoxMessage Box

Page 11: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 1111

Use DialogResult Use DialogResult private void MainForm_Load(object sender, EventArgs e)private void MainForm_Load(object sender, EventArgs e){{ DialogResultDialogResult ds = MessageBox.Show("Hello World", "PGUI", ds = MessageBox.Show("Hello World", "PGUI", MessageBoxButtons.YesNoCancel);MessageBoxButtons.YesNoCancel); if (ds == if (ds == DialogResult.YesDialogResult.Yes)) {{ f2.Text = "Yes";f2.Text = "Yes"; f2.ShowDialog();f2.ShowDialog(); }} if (ds == if (ds == DialogResult.NoDialogResult.No)) {{ f2.Text = "No";f2.Text = "No"; f2.ShowDialog();f2.ShowDialog(); }}}}

Page 12: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 1212

Visual Inheritance Visual Inheritance

To allow a base class to capture common user To allow a base class to capture common user interface controlsinterface controlsBase form serves as base classBase form serves as base classInherited form serves as derived classInherited form serves as derived class

class InheritedFormclass InheritedForm :: Forms.BaseFormForms.BaseForm

Some issues may arise with regard to event Some issues may arise with regard to event handlers being called twice, because each event handlers being called twice, because each event is being handled by both the base class and the is being handled by both the base class and the inherited class inherited class

Page 13: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 1313

Multiple-Document Interface (MDI) Multiple-Document Interface (MDI)

To display multiple documents at the same timeTo display multiple documents at the same timeEach document displayed in its own window. Each document displayed in its own window. MDI applications often have a menu item with MDI applications often have a menu item with submenus for switching between windows or submenus for switching between windows or documents documents The The OpacityOpacity property does not affect the property does not affect the appearance of MDI child forms. appearance of MDI child forms. The The CenterToParentCenterToParent method does not affect the method does not affect the behavior of MDI child forms behavior of MDI child forms

Page 14: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 1414

Create MDICreate MDI

Create a new Windows formCreate a new Windows formSet Set IsMdiContainerIsMdiContainer to true to true(Should set (Should set WindowStateWindowState to Maximized) to Maximized)

Page 15: Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering

Dr Dat Tran - Week 2 Lecture NotesDr Dat Tran - Week 2 Lecture Notes 1515

Add Child Forms to MDI Add Child Forms to MDI public partial class MDIForm : Formpublic partial class MDIForm : Form{{ public MDIForm()public MDIForm() {{ InitializeComponent();InitializeComponent();

Form mdiChild1 = new Form();Form mdiChild1 = new Form(); mdiChild1.MdiParent = this;mdiChild1.MdiParent = this; mdiChild1.Show();mdiChild1.Show();

Form mdiChild2 = new Form();Form mdiChild2 = new Form(); mdiChild2.MdiParent = this;mdiChild2.MdiParent = this; mdiChild2.Show();mdiChild2.Show(); }} }}