Chapter 17: Creating, Reading, and Writing Files

 

A General Dialog Box

Suppose the standard Visual Studio C# message box does not meet your needs. (For example, you need options like 50deciBel, 100deciBel, and 200deciBel. ) You could use a three-button message box provided by Visual Studio C#, like the YesNoCancel choice for the third argument above. You could advise the user to pick the Yes button if 50deciBel was desired, the No button if 100deciBel is desired, or Cancel if the 200deciBel choice is desired. But that is an archaic approach to this problem.

A more appropriate solution may be to create your own message box. The remainder of this chapter shows you how to create a general dialog box from scratch.

image from book
Figure 13-1: General Dialog entry (example) ” Form1
image from book
Figure 13-2: General Dialog ” Form2

Form1 is the form the programmer is working on when he must ask the user a question about which volume setting is desired. Form2 must be created at this time to pose the question to the user about the decibel setting. To call the general dialog, lines GD022-GD023 are invoked (see the listing below). Lines GD025-GD036 receive the response from the user. For a discussion on how this data is passed between Form2 and Form1, see Chapter 5.

Form2 is the message box that asks the user to pick a decibel level. The user picks button1, button2, or button3. Each button has a separate event handler (lines GD130-GD156). The main window receives the decibel answer from line GD132, GD142, or GD152.

This general dialog works under all circumstances. It is more difficult to create than the standard MessageBox.Show, but it allows the programmer to ask the right question(s) of the user. Lines GD022, 113, 115, and 117 make the transfer of data from the new message box back to the main program possible.

A complete copy of project GeneralDialog is located at Visual Studio 2005\Projects\DemosSourceCode. To execute this project, bring it into the IDE, compile it, and run it.

GeneralDialog (GD) Listing

 Form1.cs: GD001:        #region Using directives GD002:        using System; GD003:        using System.Collections.Generic; GD004:        using System.ComponentModel; GD005:        using System.Data; GD006:        using System.Drawing; GD007:        using System.Windows.Forms; GD008:        #endregion GD009:        namespace GeneralDialog GD010:        { GD011:          partial class Form1 : Form GD012:          { GD013:            public int intAnswerFromGeneralDialog; GD014:            public Form1() GD015:            {InitializeComponent();} //-----------------------------------------------------------------------------------------// GD020:            private void button1_Click(object sender, System.EventArgs e) GD021:            { // Get the general dialog. Include the main window handle, 'this',                     // to send information back. GD022:              Form2 frm2 = new Form2(this); GD023:              frm2.ShowDialog(); GD024:              frm2.Close();                     // Move on to the remainder of the program. This is the response                     // from the General Dialog: GD025:              if(intAnswerFromGeneralDialog == 50) // 50 dB. GD026:              { // Do this. GD027:                MessageBox.Show("You picked '50 db'."); GD028:              } GD029:              else if(intAnswerFromGeneralDialog == 100) // 100 dB. GD030:              { // Do this. GD031:                MessageBox.Show("You picked '100 dB'."); GD032:              } GD033:              else // 200 dB. GD034:              { // Do this. GD035:                MessageBox.Show("You picked '200 dB'."); GD036:              } GD037:            } //-----------------------------------------------------------------------------------------// GD040:            private void button2_Click(object sender, EventArgs e) GD041:            { // Quit. GD042:              Close(); GD043:            } GD044:          } GD045:        } //=========================================================================================// Form1.Designer.cs: GD050:        namespace GeneralDialog GD051:        { GD052:          partial class Form1 GD053:          {                   // Required designer variable. GD054:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. GD055:            protected override void Dispose(bool disposing) GD056:            { GD057:              if (disposing && (components != null)) components.Dispose(); GD058:              base.Dispose(disposing); GD059:            } GD060:            #region Windows Form Designer generated code                   // Required method for Designer support. GD061:            private void InitializeComponent() GD062:            { GD063:              this.button1 = new System.Windows.Forms.Button(); GD064:              this.button2 = new System.Windows.Forms.Button(); GD065:              this.SuspendLayout();                     //                     // button1                     // GD066:              this.button1.Location = new System.Drawing.Point(125, 42); GD067:              this.button1.Name = "button1"; GD068:              this.button1.Size = new System.Drawing.Size(150, 23); GD069:              this.button1.TabIndex = 0; GD070:              this.button1.Text = "Get the General Dialog"; GD071:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // GD072:              this.button2.Location = new System.Drawing.Point(162, 92); GD073:              this.button2.Name = "button2"; GD074:              this.button2.TabIndex = 1; GD075:              this.button2.Text = "Quit"; GD076:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // Form1                     // GD077:              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); GD078:              this.ClientSize = new System.Drawing.Size(384, 132); GD079:              this.Controls.Add(this.button2); GD080:              this.Controls.Add(this.button1); GD081:              this.Name = "Form1"; GD082:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; GD083:              this.Text = "General Dialog"; GD084:              this.ResumeLayout(false); GD085:            } GD086:            #endregion GD087:            private System.Windows.Forms.Button button1; GD088:            private System.Windows.Forms.Button button2; GD089:          } GD090:        } //=========================================================================================// Form2.cs: GD100:        #region Using directives GD101:        using System; GD102:        using System.Collections.Generic; GD103:        using System.ComponentModel; GD104:        using System.Data; GD105:        using System.Drawing; GD106:        using System.Text; GD107:        using System.Windows.Forms; GD108:        #endregion GD109:        namespace GeneralDialog GD110:        { GD111:          partial class Form2 : Form GD112:          {                   // Must add this line to communicate with the main window. GD113:            public Form1 frm1Parent; GD114:            // Must amend this next line to get the main window handle. GD115:            public Form2(Form1 frm1) GD116:            {                     // Put the main window handle into frm1Parent: GD117:              frm1Parent = frm1;                     // Leave this command here; the system needs it: GD118:              InitializeComponent(); GD119:            } //-----------------------------------------------------------------------------------------// GD130:            private void button1_Click(object sender, EventArgs e) GD131:            { // 50 dB. GD132:              frm1Parent.intAnswerFromGeneralDialog = 50; GD133:              Close(); GD134:            } //-----------------------------------------------------------------------------------------// GD140:            private void button2_Click(object sender, EventArgs e) GD141:            { // 100 dB. GD142:              frm1Parent.intAnswerFromGeneralDialog = 100; GD143:              Close(); GD144:            } //-----------------------------------------------------------------------------------------// GD150:            private void button3_Click(object sender, EventArgs e) GD151:            { // 200 dB. GD152:              frm1Parent.intAnswerFromGeneralDialog = 200; GD153:              Close(); GD154:            } GD155:          } GD156:        } //=========================================================================================// Form2.Designer.cs: GD160:        namespace GeneralDialog GD161:        { GD162:          partial class Form2 GD163:          {                   // Required designer variable. GD164:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. GD165:            protected override void Dispose(bool disposing) GD166:            { GD167:              if (disposing && (components != null)) components.Dispose(); GD168:              base.Dispose(disposing); GD169:            } GD170:            #region Windows Form Designer generated code                   // Required method for Designer support. GD171:            private void InitializeComponent() GD172:            { GD173:              this.button1 = new System.Windows.Forms.Button(); GD174:              this.label1 = new System.Windows.Forms.Label(); GD175:              this.button2 = new System.Windows.Forms.Button(); GD176:              this.button3 = new System.Windows.Forms.Button(); GD177:              this.SuspendLayout();                     //                     // button1                     // GD178:              this.button1.Location = new System.Drawing.Point(44, 48); GD179:              this.button1.Name = "button1"; GD180:              this.button1.TabIndex = 0; GD181:              this.button1.Text = "50 dB"; GD182:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // label1                     // GD183:              this.label1.AutoSize = true; GD184:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,                       System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); GD185:              this.label1.Location = new System.Drawing.Point(42, 13); GD186:              this.label1.Name = "label1"; GD187:              this.label1.Size = new System.Drawing.Size(302, 21); GD188:              this.label1.TabIndex = 1; GD189:              this.label1.Text = "Pick a Volume Setting (50, 100, 200 dB):";                     //                     // button2                     // GD190:              this.button2.Location = new System.Drawing.Point(163, 48); GD191:              this.button2.Name = "button2"; GD192:              this.button2.TabIndex = 2; GD193:              this.button2.Text = "100 dB"; GD194:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // button3                     // GD195:              this.button3.Location = new System.Drawing.Point(282, 48); GD196:              this.button3.Name = "button3"; GD197:              this.button3.TabIndex = 3; GD198:              this.button3.Text = "200 dB"; GD199:              this.button3.Click += new System.EventHandler(this.button3_Click);                     //                     // Form2                     // GD200:              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); GD201:              this.ClientSize = new System.Drawing.Size(384, 96); GD202:              this.Controls.Add(this.button3); GD203:              this.Controls.Add(this.button2); GD204:              this.Controls.Add(this.label1); GD205:              this.Controls.Add(this.button1); GD206:              this.Name = "Form2"; GD207:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; GD208:              this.Text = "GD Choices"; GD209:              this.ResumeLayout(false); GD210:              this.PerformLayout(); GD211:            } GD212:            #endregion GD213:            private System.Windows.Forms.Button button1; GD214:            private System.Windows.Forms.Label label1; GD215:            private System.Windows.Forms.Button button2; GD216:            private System.Windows.Forms.Button button3; GD217:          } GD218:        } 
 


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