Chapter 6: Visual Studio C Code Placement

 

Two-Way Communication

These are the procedures used for two-way communication between a main window and a child window, using the code listing that follows :

  1. Main window

    1. OR011 and OR012: Declare public, global string variables strGrandmaMoses and strGrandmaJuliette so the data the user enters into the text boxes may be sent to Form2 and Form3 (private won t work).

    2. OR251 and OR252: Convert any declarations of variables in main that must receive data from the child windows from private to public (label6 and label7).

    3. OR042: Create child window Form2 and send the main window handle named this to that child window so it can send data back to the main window later.

    4. OR052: Create child window Form3 and send the main window handle named this to that child window so it can send data back to the main window later.

  1. Child window (Grandma Moses House)

    1. OR311: Create a public Form1 variable within Form2 to receive the main window handle: public Form1 frm1Parent.

    2. OR312: Receive the Form1 handle (named frm1) as an argument in the Form2 class: Form1 frm1.

    3. OR314: Place the handle name into frm1Parent: frm1Parent = frm1.

    4. OR322: Transfer the message from the main window to label2 in the child window: label2.Text = frm1Parent.strGrandmaMoses.

    5. OR332: Transfer the message from the child window to a main window address: frm1Parent.label6.Text = textBox1.Text.

  1. Child window (Grandma Juliette s House)

    1. OR481: Create a public Form1 variable in Form3 to receive the main window handle: public Form1 frm1Parent.

    2. OR482: Receive the Form1 handle (named frm1) as an argument in the Form3 class: Form1 frm1.

    3. OR484: Place the handle name into frm1Parent: frm1Parent = frm1.

    4. OR502: Transfer the message from the main window to label2 in the child window: label2.Text = frm1Parent.strGrandma-Juliette.

    5. OR512: Transfer the message from the child window to a main window address: frm1Parent.label7.Text = textBox1.Text.

1.  

Are the two child windows able to talk to one another?

Answers

1.  

No, the child windows can only pass information to the main window. It would be possible to construct a means of communication between the two, but the data would always have to be placed in a persistent location in main by one child window and retrieved by the other child window.

The complete OverTheRiverAndThroughTheWoods code is shown below:

OverTheRiverAndThroughTheWoods (OR) Listing

 Form1.cs: OR001:        using System; OR002:        using System.Collections.Generic; OR003:        using System.ComponentModel; OR004:        using System.Windows.Forms; OR005:        using System.Data; OR006:        using System.Drawing; OR007:        namespace OverTheRiverAndThroughTheWoods OR008:        { OR009:          partial class Form1 : Form OR010:          {                   // These are two programmer-added items: OR011:            public string strGrandmaMoses; OR012:            public string strGrandmaJuliette; OR013:            public Form1() OR014:            { OR015:              InitializeComponent(); OR016:            } //-----------------------------------------------------------------------------------------// OR020:            private void button1_Click(object sender, EventArgs e) OR021:            { // Send Grandma Moses message.                     // Place this message into a public string, for transport to Form2. OR022:              strGrandmaMoses = textBox1.Text; OR023:            } //-----------------------------------------------------------------------------------------// OR030:            private void button2_Click(object sender, EventArgs e) OR031:            { // Send Grandma Juliette message.                     // Place this message into a public string, for transport to Form3. OR032:              strGrandmaJuliette = textBox2.Text; OR033:            } //-----------------------------------------------------------------------------------------// OR040:            private void button3_Click(object sender, EventArgs e) OR041:            { // Going to Grandma Moses' house.                     // Create Form2 (Grandma Moses' house).                     // 'this' in the next line refers to the main window's 'handle'. OR042:              Form2 frm2 = new Form2(this); OR043:              frm2.ShowDialog(); OR044:              frm2.Close(); OR045:            } //-----------------------------------------------------------------------------------------// OR050:            private void button4_Click(object sender, EventArgs e) OR051:            { // Going to Grandma Juliette's house.                     // Create Form3 (Grandma Juliette's house).                     // 'this' in the next line refers to the main window's 'handle'. OR052:              Form3 frm3 = new Form3(this); OR053:              frm3.ShowDialog(); OR054:              frm3.Close(); OR055:            } //-----------------------------------------------------------------------------------------// OR060:            private void button5_Click(object sender, EventArgs e) OR061:            { // Quit this project. OR062:              Close(); OR063:            } OR064:          } OR065:        } //=========================================================================================// Form1.Designer.cs: OR080:        namespace OverTheRiverAndThroughTheWoods OR081:        { OR082:          partial class Form1 OR083:          {                   // Required designer variable. OR084:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. OR085:            protected override void Dispose(bool disposing) OR086:            { OR087:              if (disposing && (components != null)) OR088:              { OR089:                components.Dispose(); OR090:              } OR091:              base.Dispose(disposing); OR092:            } OR093:            #region Windows Form Designer generated code                   // Required method for Designer support - do not modify OR094:            private void InitializeComponent() OR095:            { OR096:              this.label1 = new System.Windows.Forms.Label(); OR097:              this.label2 = new System.Windows.Forms.Label(); OR098:              this.label3 = new System.Windows.Forms.Label(); OR099:              this.textBox1 = new System.Windows.Forms.TextBox(); OR100:              this.textBox2 = new System.Windows.Forms.TextBox(); OR101:              this.groupBox1 = new System.Windows.Forms.GroupBox(); OR102:              this.label7 = new System.Windows.Forms.Label(); OR103:              this.label6 = new System.Windows.Forms.Label(); OR104:              this.label5 = new System.Windows.Forms.Label(); OR105:              this.label4 = new System.Windows.Forms.Label(); OR106:              this.button1 = new System.Windows.Forms.Button(); OR107:              this.button2 = new System.Windows.Forms.Button(); OR108:              this.button3 = new System.Windows.Forms.Button(); OR109:              this.button4 = new System.Windows.Forms.Button(); OR110:              this.button5 = new System.Windows.Forms.Button(); OR111:              this.groupBox1.SuspendLayout(); OR112:              this.SuspendLayout();                     //                     // label1                     // OR113:              this.label1.AutoSize = true; OR114:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F,                        System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR115:              this.label1.Location = new System.Drawing.Point(100, 8); OR116:              this.label1.Name = "label1"; OR117:              this.label1.Size = new System.Drawing.Size(341, 24); OR118:              this.label1.TabIndex = 0; OR119:              this.label1.Text = "Over the River and Through the Woods";                     //                     // label2                     // OR120:              this.label2.AutoSize = true; OR121:              this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR122:              this.label2.Location = new System.Drawing.Point(24, 52); OR123:              this.label2.Name = "label2"; OR124:              this.label2.Size = new System.Drawing.Size(206, 17); OR125:              this.label2.TabIndex = 1; OR126:              this.label2.Text = "Send Note to Grandma Moses ->";                     //                     // label3                     // OR127:              this.label3.AutoSize = true; OR128:              this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        (byte)(0))); OR129:              this.label3.Location = new System.Drawing.Point(24, 86); OR130:              this.label3.Name = "label3"; OR131:              this.label3.Size = new System.Drawing.Size(209, 17); OR132:              this.label3.TabIndex = 2; OR133:              this.label3.Text = "Send Note to Grandma Juliette ->";                     //                     // textBox1                     // OR134:              this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR135:              this.textBox1.Location = new System.Drawing.Point(235, 50); OR136:              this.textBox1.Name = "textBox1"; OR137:              this.textBox1.Size = new System.Drawing.Size(200, 23); OR138:              this.textBox1.TabIndex = 3;                     //                     // textBox2                     // OR139:              this.textBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR140:              this.textBox2.Location = new System.Drawing.Point(235, 84); OR141:              this.textBox2.Name = "textBox2"; OR142:              this.textBox2.Size =new System.Drawing.Size(200,23);                        this.textBox2.TabIndex = 4;                     //                     // groupBox1                     // OR143:              this.groupBox1.Controls.Add(this.label7); OR144:              this.groupBox1.Controls.Add(this.label6); OR145:              this.groupBox1.Controls.Add(this.label5); OR146:              this.groupBox1.Controls.Add(this.label4); OR147:              this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR148:              this.groupBox1.Location = new System.Drawing.Point(28, 120); OR149:              this.groupBox1.Name = "groupBox1"; OR150:              this.groupBox1.Size = new System.Drawing.Size(488, 64); OR151:              this.groupBox1.TabIndex = 5; OR152:              this.groupBox1.TabStop = false; OR153:              this.groupBox1.Text = "Messages From Our Grandmothers"; OR154:              this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);                     //                     // label7                     // OR155:              this.label7.AutoSize = true; OR156:              this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR157:              this.label7.Location = new System.Drawing.Point(75, 40); OR158:              this.label7.Name = "label7"; OR159:              this.label7.Size = new System.Drawing.Size(0, 0); OR160:              this.label7.TabIndex = 3;                     //                     // label6                     // OR161:              this.label6.AutoSize = true; OR162:              this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR163:              this.label6.Location = new System.Drawing.Point(75, 20); OR164:              this.label6.Name = "label6"; OR165:              this.label6.Size = new System.Drawing.Size(0, 0); OR166:              this.label6.TabIndex = 2;                     //                     // label5                     // OR167:              this.label5.AutoSize = true; OR168:              this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR169:              this.label5.Location = new System.Drawing.Point(16, 40); OR170:              this.label5.Name = "label5"; OR171:              this.label5.Size = new System.Drawing.Size(52, 17); OR172:              this.label5.TabIndex = 1; OR173:              this.label5.Text = "Juliette:";                     //                     // label4                     // OR174:              this.label4.AutoSize = true; OR175:              this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR176:              this.label4.Location = new System.Drawing.Point(16, 20); OR177:              this.label4.Name = "label4"; OR178:              this.label4.Size = new System.Drawing.Size(49, 17); OR179:              this.label4.TabIndex = 0; OR180:              this.label4.Text = "Moses:";                     //                     // button1                     // OR181:              this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR182:              this.button1.Location = new System.Drawing.Point(452, 50); OR183:              this.button1.Name = "button1"; OR184:              this.button1.Size = new System.Drawing.Size(60, 22); OR185:              this.button1.TabIndex = 6; OR186:              this.button1.Text = "Send"; OR187:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // OR188:              this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR189:              this.button2.Location = new System.Drawing.Point(452, 84); OR190:              this.button2.Name = "button2"; OR191:              this.button2.Size = new System.Drawing.Size(60, 22); OR192:              this.button2.TabIndex = 7; OR193:              this.button2.Text = "Send"; OR194:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // button3                     // OR195:              this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR196:              this.button3.Location = new System.Drawing.Point(50, 195); OR197:              this.button3.Name = "button3"; OR198:              this.button3.Size = new System.Drawing.Size(200, 23); OR199:              this.button3.TabIndex = 8; OR200:              this.button3.Text = "Going to Grandma Moses\' House"; OR201:              this.button3.Click += new System.EventHandler(this.button3_Click);                     //                     // button4                     // OR202:              this.button4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR203:              this.button4.Location = new System.Drawing.Point(300, 195); OR204:              this.button4.Name = "button4"; OR205:              this.button4.Size = new System.Drawing.Size(200, 23); OR206:              this.button4.TabIndex = 9; OR207:              this.button4.Text = "Going to Grandma Juliette\'s House"; OR208:              this.button4.Click += new System.EventHandler(this.button4_Click);                     //                     // button5                     // OR209:              this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR210:              this.button5.Location = new System.Drawing.Point(208, 226); OR211:              this.button5.Name = "button5"; OR212:              this.button5.Size = new System.Drawing.Size(125, 23); OR213:              this.button5.TabIndex = 10; OR214:              this.button5.Text = "Quit this Project"; OR215:              this.button5.Click += new System.EventHandler(this.button5_Click);                     //                     // Form1                     // OR216:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); OR217:              this.ClientSize = new System.Drawing.Size(542, 266); OR218:              this.Controls.Add(this.button5); OR219:              this.Controls.Add(this.button4); OR220:              this.Controls.Add(this.button3); OR221:              this.Controls.Add(this.button2); OR222:              this.Controls.Add(this.button1); OR223:              this.Controls.Add(this.groupBox1); OR224:              this.Controls.Add(this.textBox2); OR225:              this.Controls.Add(this.textBox1); OR226:              this.Controls.Add(this.label3); OR227:              this.Controls.Add(this.label2); OR228:              this.Controls.Add(this.label1); OR229:              this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                        System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR230:              this.Name = "Form1"; OR231:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; OR232:              this.Text = "Over The River and Through The Woods"; OR233:              this.groupBox1.ResumeLayout(false); OR234:              this.groupBox1.PerformLayout(); OR235:              this.ResumeLayout(false); OR236:              this.PerformLayout(); OR237:            } OR238:            #endregion OR239:            private System.Windows.Forms.Label label1; OR240:            private System.Windows.Forms.Label label2; OR241:            private System.Windows.Forms.Label label3; OR242:            private System.Windows.Forms.TextBox textBox1; OR243:            private System.Windows.Forms.TextBox textBox2; OR244:            private System.Windows.Forms.GroupBox groupBox1; OR245:            private System.Windows.Forms.Label label4; OR246:            private System.Windows.Forms.Button button1; OR247:            private System.Windows.Forms.Button button2; OR248:            private System.Windows.Forms.Button button3; OR249:            private System.Windows.Forms.Button button4; OR250:            private System.Windows.Forms.Button button5;                   // These next two labels must be converted from private to public: OR251:            public System.Windows.Forms.Label label6; OR252:            public System.Windows.Forms.Label label7; OR253:            private System.Windows.Forms.Label label5; OR254:          } OR255:        } //=========================================================================================// Form2.cs: OR300:        using System; OR301:        using System.Collections.Generic; OR302:        using System.ComponentModel; OR303:        using System.Data; OR304:        using System.Drawing; OR305:        using System.Text; OR306:        using System.Windows.Forms; OR307:        namespace OverTheRiverAndThroughTheWoods OR308:        { OR309:          partial class Form2 : Form OR310:          {                   // This is a programmer-added item: OR311:            public Form1 frm1Parent;                   // OR312:            public Form2(Form1 frm1) OR313:            { OR314:              frm1Parent = frm1; OR315:              InitializeComponent(); OR316:            } //-----------------------------------------------------------------------------------------// OR320:            private void Form2_Load(object sender, EventArgs e) OR321:            { // Place the main window's message into 'label2'. OR322:              label2.Text = frm1Parent.strGrandmaMoses; OR323:            } //----------------------------------------------------------------------------------------// OR330:            private void button1_Click(object sender, EventArgs e) OR331:            { // Place the return message into persistent storage in Form1: OR332:              frm1Parent.label6.Text = textBox1.Text; OR333:              Close(); OR334:            } OR335:          } OR336:        } //========================================================================================// Form2.Designer.cs: OR360:        namespace OverTheRiverAndThroughTheWoods OR361:        { OR362:          partial class Form2 OR363:          {                   // Required designer variable. OR364:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. OR365:            protected override void Dispose(bool disposing) OR366:            { OR367:              if (disposing && (components != null)) OR368:              { OR369:                components.Dispose(); OR370:              } OR371:              base.Dispose(disposing); OR372:            } OR373:            #region Windows Form Designer generated code                   // Required method for Designer support - do not modify OR374:            private void InitializeComponent() OR375:            { OR376:              this.label1 = new System.Windows.Forms.Label(); OR377:              this.label2 = new System.Windows.Forms.Label(); OR378:              this.label3 = new System.Windows.Forms.Label(); OR379:              this.textBox1 = new System.Windows.Forms.TextBox(); OR380:              this.button1 = new System.Windows.Forms.Button(); OR381:              this.SuspendLayout();                     //                     // label1                     // OR382:              this.label1.AutoSize = true; OR383:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR384:              this.label1.Location = new System.Drawing.Point(16, 16); OR385:              this.label1.Name = "label1"; OR386:              this.label1.Size = new System.Drawing.Size(140, 17); OR387:              this.label1.TabIndex = 0; OR388:              this.label1.Text = "Message From Home:";                     //                     // label2                     // OR389:              this.label2.AutoSize = true; OR390:              this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR391:              this.label2.Location = new System.Drawing.Point(16, 40); OR392:              this.label2.Name = "label2"; OR393:              this.label2.Size = new System.Drawing.Size(0, 0); OR394:              this.label2.TabIndex = 1;                     //                     // label3                     // OR395:              this.label3.AutoSize = true; OR396:              this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR397:              this.label3.Location = new System.Drawing.Point(16, 72); OR398:              this.label3.Name = "label3"; OR399:              this.label3.Size = new System.Drawing.Size(156, 17); OR400:              this.label3.TabIndex = 2; OR401:              this.label3.Text = "Send Message to Home:";                     //                     // textBox1                     // OR402:              this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                        ((byte)(0))); OR403:              this.textBox1.Location = new System.Drawing.Point(24, 94); OR404:              this.textBox1.Name = "textBox1"; OR405:              this.textBox1.Size = new System.Drawing.Size(340, 23); OR406:              this.textBox1.TabIndex = 3;                     //                     // button1                     // OR407:              this.button1.Location = new System.Drawing.Point(137, 128); OR408:              this.button1.Name = "button1"; OR409:              this.button1.Size = new System.Drawing.Size(125, 23); OR410:              this.button1.TabIndex = 4; OR411:              this.button1.Text = "Return to Home"; OR412:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // Form2                     // OR413:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); OR414:              this.ClientSize = new System.Drawing.Size(392, 166); OR415:              this.Controls.Add(this.button1); OR416:              this.Controls.Add(this.textBox1); OR417:              this.Controls.Add(this.label3); OR418:              this.Controls.Add(this.label2); OR419:              this.Controls.Add(this.label1); OR420:              this.Name = "Form2"; OR421:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; OR422:              this.Text = "Grandma Moses\' House"; OR423:              this.Load += new System.EventHandler(this.Form2_Load); OR424:              this.ResumeLayout(false); OR425:              this.PerformLayout(); OR426:            } OR427:            #endregion OR428:            private System.Windows.Forms.Label label1; OR429:            private System.Windows.Forms.Label label2; OR430:            private System.Windows.Forms.Label label3; OR431:            private System.Windows.Forms.TextBox textBox1; OR432:            private System.Windows.Forms.Button button1; OR433:          } OR434:        } //=========================================================================================// Form3.cs: OR470:        using System; OR471:        using System.Collections.Generic; OR472:        using System.ComponentModel; OR473:        using System.Data; OR474:        using System.Drawing; OR475:        using System.Text; OR476:        using System.Windows.Forms; OR477:        namespace OverTheRiverAndThroughTheWoods OR478:        { OR479:          partial class Form3 : Form OR480:          {                   // This is a programmer-added item: OR481:            public Form1 frm1Parent;                   // OR482:            public Form3(Form1 frm1) OR483:            { OR484:              frm1Parent = frm1; OR485:              InitializeComponent(); OR486:            } //-----------------------------------------------------------------------------------------// OR500:            private void Form3_Load(object sender, EventArgs e) OR501:            { // Place the main window's message into 'label2'. OR502:              label2.Text = frm1Parent.strGrandmaJuliette; OR503:            } //-----------------------------------------------------------------------------------------// OR510:            private void button1_Click(object sender, EventArgs e) OR511:            { // Place the return message into persistent storage in Form1: OR512:              frm1Parent.label7.Text = textBox1.Text; OR513:              Close(); OR514:            } OR515:          } OR516:        } //=========================================================================================// Form3.Designer.cs [This is a repeat of Form2 Designer, replacing Grandma Moses with Grandma Juliette. //=========================================================================================// 
 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net