3
Reply

Help - Problem with Events in sub Form

hhinman

hhinman

Mar 17 2005 9:22 PM
1.7k
I have an application where the main form (Form1) creates an instance of Form2 and shows it using the Form2.Show method. Form1 then loops through some logic and populates Form2. The problemI have is that I have a button (button1) on Form2 that I wish the user to be able to click on to stop the process. But because the Form2.Show method is used to bring Form2 up, button1 on Form2 is not activated (the same problem exists for the Close button in the upper right corner). I've created a sample project to show the problem. Here is the code for Form1: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace SubFormEvents { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(96, 48); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 48); this.button1.TabIndex = 0; this.button1.Text = "Run Test"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(296, 158); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Show(); for (int i = 0; i < 100000; i++) { if (form2.stopCount == 1) { MessageBox.Show("Count Stopped"); return; } form2.label1.Text = i.ToString(); form2.Refresh(); } } } } Here is the code for Form2: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace SubFormEvents { /// /// Summary description for Form2. /// public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; public System.Windows.Forms.Label label1; public System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public int stopCount = 0; public Form2() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(48, 120); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(200, 56); this.button1.TabIndex = 0; this.button1.Text = "Stop Test "; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.Location = new System.Drawing.Point(96, 40); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(160, 12); this.label1.TabIndex = 1; this.label1.Text = "0"; // // label2 // this.label2.Location = new System.Drawing.Point(16, 40); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(64, 16); this.label2.TabIndex = 2; this.label2.Text = "Count:"; // // label3 // this.label3.Location = new System.Drawing.Point(8, 192); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(272, 32); this.label3.TabIndex = 3; this.label3.Text = "How to get the button above to work before counting stops?"; // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.button1); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); } #endregion private void button1_Click(object sender, System.EventArgs e) { this.stopCount = 1; } } }

Answers (3)