Archived

This topic is now archived and is closed to further replies.

Problems about event handling using C#

This topic is 5306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a program to hand the event of different components using C#. But i found that is quite difficult to handle it. e.g. 1. I have a mdi parent and a mdi child, when i maximumize the child form, the child form throw two resize events. 2. When i resize the child form size, and then program it to a new width for the child form, two resize events throw. 3. When i maximumize the child form and program it ti a new width for the child form, three resize events throw. Why? How can i just throw one resize event? On the other hand, how can i assign a value to a scroll bar and the srcoll bar does not throw value change event although the scroll bar register the value change event? You can copy the following two classes and test. Pls help me. Thx a lot of.

//form1.cs



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace resize
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			InitializeComponent();
		}

		#region Dispose
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion
		#region Windows Form Designer generated code
		/// <summary>

		/// Required method for Designer support - do not modify

		/// the contents of this method with the code editor.

		/// </summary>

		private void InitializeComponent()
		{
			// 

			// Form1

			// 

			this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
			this.ClientSize = new System.Drawing.Size(456, 358);
			this.IsMdiContainer = true;
			this.Name = "Form1";
			this.Text = "Form1";
			this.Resize += new System.EventHandler(this.Form1_Resize);
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion


		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Form2 form2 = new Form2();
			form2.MdiParent = this;
			form2.Show();
		}

		private void Form1_Resize(object sender, System.EventArgs e)
		{
			MessageBox.Show("Form1 resize");
		}
	}
}




//Form2.cs


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace resize
{
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.VScrollBar vScrollBar1;
		private System.Windows.Forms.Button button1;
		private System.ComponentModel.Container components = null;

		public Form2()
		{
			InitializeComponent();
		}

		#region Dispose
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion
		#region Windows Form Designer generated code
		/// <summary>

		/// Required method for Designer support - do not modify

		/// the contents of this method with the code editor.

		/// </summary>

		private void InitializeComponent()
		{
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
			this.button1 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 

			// dataGrid1

			// 

			this.dataGrid1.DataMember = "";
			this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(504, 350);
			this.dataGrid1.TabIndex = 0;
			this.dataGrid1.Resize += new System.EventHandler(this.dataGrid1_Resize);
			// 

			// vScrollBar1

			// 

			this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Right;
			this.vScrollBar1.Location = new System.Drawing.Point(488, 0);
			this.vScrollBar1.Name = "vScrollBar1";
			this.vScrollBar1.Size = new System.Drawing.Size(16, 350);
			this.vScrollBar1.TabIndex = 1;
			this.vScrollBar1.ValueChanged += new System.EventHandler(this.vScrollBar1_ValueChanged);
			// 

			// button1

			// 

			this.button1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.button1.Location = new System.Drawing.Point(0, 318);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(488, 32);
			this.button1.TabIndex = 2;
			this.button1.Text = "+1 Scroll Value Buttom";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 

			// Form2

			// 

			this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
			this.ClientSize = new System.Drawing.Size(504, 350);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button1,
																		  this.vScrollBar1,
																		  this.dataGrid1});
			this.Name = "Form2";
			this.Text = "Form2";
			this.Resize += new System.EventHandler(this.Form2_Resize);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Form2_Resize(object sender, System.EventArgs e)
		{
			MessageBox.Show("Form2 resize");
			this.Width = 100;
		}

		private void dataGrid1_Resize(object sender, System.EventArgs e)
		{
			MessageBox.Show("DataGrid resize");
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("+1 button clicked");
			vScrollBar1.Value++;
		}

		private void vScrollBar1_ValueChanged(object sender, System.EventArgs e)
		{
			MessageBox.Show("Scroll Bar value changed.");
		}
	}
}

Share this post


Link to post
Share on other sites