DOC PREVIEW
Penn CIS 399 - Multiple Forms

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Multiple FormsMulti ThreadingAbout BoxesMultiple FormsC# ProgrammingJanuary 24Projects•Calculator due tonight at midnight•Instructions for submitting on the webpage•Project 2 will be posted today and will be due Wednesday,February 7Any questions?Note on access modifiers•Recall that the five access modifiers are:•public•private•protected•internal•protected internal•The default visibility for top-level types is internal•The default visibility for instance variables and methods isprivatePart IMultiple FormsMultiple forms•So far, our example applications have only used one window•Most non-trivial applications use several windows•These windows often need to communicateOpening other forms•Suppose the Main() method calls:Application.Run(new MainForm());•This creates an instance of MainForm and displays the window•This is the top-level window for this WinForms app•When this window is closed, the application terminatesOpening other forms•The MainForm object can open other windows in two ways:•new ChildForm().Show();•new ChildForm().ShowDialog();•ShowDialog() requires that the window be closed beforefocus can return to the callerCommunicating forms•Consider an example where MainForm has a button and atextbox, and ChildForm has a textbox•The button launches a ChildForm window•We will set up two simple communication channels:1. When the parent’s text box is updated, the child’s getsupdated to the same value2. When the child’s text box is updated, the parent’s getsupdated to the same value•We will look at three ways to implement these, withincreasingly good designExample 1a•Our first attempt to communicate updates to the child form isby directly accessing its text box•Since the child’s controls are private by default, we need toeither c hange it to public or internal so the main form canaccess itChildForm.Designer.cs:internal System.Windows.Forms.TextBox txt1;MainForm.cs:ChildForm child;private void btn1_Click(object sender, EventArgs e) {child = new ChildForm();child.Show();}private void txt1_TextChanged(object sender, EventArgs e) {if (child != null)child.txt1.Text = this.txt1.Text;}Example 1b•Instead of accessing the child form’s control directly, we caninstead keep the text box private and define a public orinternal method in ChildForm that gets called when theparent’s text box changesChildForm.Designer.cs:private System.Windows.Forms.TextBox txt1;ChildForm.cs:internal void OnParentTextChanged(string s) {this.txt1.Text = s;}MainForm.cs:private void txt1_TextChanged(object sender, EventArgs e) {if (child != null)child.OnParentTextChanged(this.txt1.Text);}Example 1c•Defining the OnParentTextChanged function in the previousexample feels like an event handler•So let’s a use a delegate instead of calling the function directlyChildForm.Designer.cs and ChildForm.cs: same as beforeMainForm.cs:private delegate void SetTextDelegate(string s);private SetTextDelegate SetTextCallback;private void btn1_Click(object sender, EventArgs e) {child = new ChildForm();this.SetTextCallback +=new SetTextDelegate(child.OnParentTextChanged);child.Show();}private void txt1_TextChanged(object sender, EventArgs e) {if (SetTextCallback != null)SetTextCallback(this.txt1.Text);}Example 2a/2b•To communicate changes from the child to the main form, ourfirst two approaches would require a reference to the mainform itself•The parent form would need to pass a reference of itself tothe child so that it could manipulate its c ontrols or call itsmethods•(The code on the next slide skips the version where the childupdates the parent’s text box directly)MainForm.cs:private void btn1_Click(object sender, EventArgs e) {child = new ChildForm(this);child.Show();}internal void OnChildTextChanged(string s) {this.txt1.Text = s;}ChildForm.cs:private MainForm f;public ChildForm(MainForm f) {InitializeComponent(); this.f = f;}private void txt1_TextChanged(object sender, EventArgs e) {f.OnChildTextChanged(this.txt1.Text);}Example 2c•We can also set up this callback through a delegate•Since this time the delegate needs to be accessed by bothclasses, it is defined at the top-level of the namespacenamespace MultipleForms{public delegate void SetTextDelegate(string s);...}ChildForm.cs:public SetTextDelegate SetTextCallback;private void txt1_TextChanged(object sender, EventArgs e) {if (SetTextCallback != null)SetTextCallback(this.txt1.Text);}MainForm.cs:internal void OnChildTextChanged(string s) {this.txt1.Text = s;}private void btn1_Click(object sender, EventArgs e) {child = new ChildForm();child.SetTextCallback += OnChildTextChanged;child.Show();}Summary•Facilitating communication between forms using publicvariables and methods is not a good idea•For applications with tightly coupled behavior between forms,it is sometimes useful to facilitate communication usinginternal variables and methods freely•For most applications, especially when communicating withseparate code, controlling communication channels throughdelegates is the best choicePart IIMulti ThreadingMulti threading•With GUI applications, we want the interface to remainresponsive at al times•For example, if the application is performing expensive I/O ornetwork operations, the GUI should not lock up•To decouple the execution of different tasks (like backgroundcomputation versus a form responding to user events), theseneed to be split up into different threads•If they are not (if the application is single-threaded instead),the rest of the application can be blocked and appear frozenwhile some expensive operation is executingMulti threading•A thread can be created and set to run an arbitrary function•For now, we will look at an example of creating theChildForm object in a separate threadSingle threaded issue•In our examples, a MainForm object has created a ChildFormobject•It then calls Show() to display the form•Show() does not start the child form in a new thread•Therefore, any undue delay in the child form’s execution willprevent the execution of the main formSingle threaded issue•For example, consider the following silly behavior of theChildForm constructor:public ChildForm() {InitializeComponent();while (true) {int i = 0;}}•When the button on the MainForm is clicked, thisinfinitely-looping constructor is called•Since the child form is running in the same thread, the mainform becomes unresponsiveMulti threading•We can spawn a new thread for the child form to


View Full Document

Penn CIS 399 - Multiple Forms

Download Multiple Forms
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Multiple Forms and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Multiple Forms 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?