winformイベントを事前に実行

18983 ワード

今日問題が発生しました.Winformのイベントを事前に実行することです.次は解決策を示します.
 
Main関数
 
using System;
using System.Windows.Forms;

namespace FormLoadCompletedDemo
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
	}
}
 
 
using System;
using System.Windows.Forms;

namespace FormLoadCompletedDemo
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void ShowChildForm(ChildForm.LoadStyle ls)
		{
			ChildForm frm = new ChildForm(ls);
			frm.ShowDialog();
		}

		private void btnLoad_Click(object sender, EventArgs e)
		{
			ShowChildForm(ChildForm.LoadStyle.OnLoad);
		}

		private void btnShown_Click(object sender, EventArgs e)
		{
			ShowChildForm(ChildForm.LoadStyle.OnShown);
		}

		private void btnShownDoEvents_Click(object sender, EventArgs e)
		{
			ShowChildForm(ChildForm.LoadStyle.OnShownDoEvents);
		}

		private void btnChildUsingBase_Click(object sender, EventArgs e)
		{
			ChildFormUsingBase frm = new ChildFormUsingBase();
			frm.ShowDialog();
		}
	}
}

 
 
 
namespace FormLoadCompletedDemo
{
	partial class MainForm
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#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.btnShown = new System.Windows.Forms.Button();
			this.btnShownDoEvents = new System.Windows.Forms.Button();
			this.btnLoad = new System.Windows.Forms.Button();
			this.btnChildUsingBase = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// btnShown
			// 
			this.btnShown.Location = new System.Drawing.Point(50, 49);
			this.btnShown.Name = "btnShown";
			this.btnShown.Size = new System.Drawing.Size(193, 23);
			this.btnShown.TabIndex = 1;
			this.btnShown.Text = "Open Form Without DoEvents";
			this.btnShown.UseVisualStyleBackColor = true;
			this.btnShown.Click += new System.EventHandler(this.btnShown_Click);
			// 
			// btnShownDoEvents
			// 
			this.btnShownDoEvents.Location = new System.Drawing.Point(50, 78);
			this.btnShownDoEvents.Name = "btnShownDoEvents";
			this.btnShownDoEvents.Size = new System.Drawing.Size(193, 23);
			this.btnShownDoEvents.TabIndex = 2;
			this.btnShownDoEvents.Text = "Open Form *With* DoEvents";
			this.btnShownDoEvents.UseVisualStyleBackColor = true;
			this.btnShownDoEvents.Click += new System.EventHandler(this.btnShownDoEvents_Click);
			// 
			// btnLoad
			// 
			this.btnLoad.Location = new System.Drawing.Point(50, 20);
			this.btnLoad.Name = "btnLoad";
			this.btnLoad.Size = new System.Drawing.Size(193, 23);
			this.btnLoad.TabIndex = 0;
			this.btnLoad.Text = "Do processing in Load event";
			this.btnLoad.UseVisualStyleBackColor = true;
			this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
			// 
			// btnChildUsingBase
			// 
			this.btnChildUsingBase.Location = new System.Drawing.Point(50, 138);
			this.btnChildUsingBase.Name = "btnChildUsingBase";
			this.btnChildUsingBase.Size = new System.Drawing.Size(193, 23);
			this.btnChildUsingBase.TabIndex = 3;
			this.btnChildUsingBase.Text = "Open Form derived from BaseForm";
			this.btnChildUsingBase.UseVisualStyleBackColor = true;
			this.btnChildUsingBase.Click += new System.EventHandler(this.btnChildUsingBase_Click);
			// 
			// MainForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(292, 183);
			this.Controls.Add(this.btnChildUsingBase);
			this.Controls.Add(this.btnLoad);
			this.Controls.Add(this.btnShownDoEvents);
			this.Controls.Add(this.btnShown);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "MainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "FormLoadCompleteDemo";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Button btnShown;
		private System.Windows.Forms.Button btnShownDoEvents;
		private System.Windows.Forms.Button btnLoad;
		private System.Windows.Forms.Button btnChildUsingBase;
	}
}

 
 
Children form
 
 
using System;
using System.Windows.Forms;

namespace FormLoadCompletedDemo
{
	public partial class ChildForm : Form
	{
		public enum LoadStyle
		{
			OnLoad,
			OnShown,
			OnShownDoEvents
		}

		private LoadStyle _ls;

		public ChildForm(LoadStyle ls)
		{
			InitializeComponent();
			_ls = ls;
		}

		private void ChildForm_Load(object sender, EventArgs e)
		{
			if (_ls == LoadStyle.OnLoad)
				LoadList();
		}

		private void ChildForm_Shown(object sender, EventArgs e)
		{
			if (_ls == LoadStyle.OnShown)
				LoadList();
			else if (_ls == LoadStyle.OnShownDoEvents)
			{
				Application.DoEvents();
				LoadList();
			}
		}

		private void LoadList()
		{
			this.Cursor = Cursors.WaitCursor;

			for (int l = 0; l < 50000; l++)
				this.listBox1.Items.Add("Item " + l.ToString());

			this.Cursor = Cursors.Default;
		}

		private void ChildForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			// Without the following, subsequent instances of this form would load
			//  slower than the 1st call, distorting the time-delay of this demo.
			listBox1.Items.Clear();
			GC.Collect();
		}

	}
}

 
 
 
namespace FormLoadCompletedDemo
{
	partial class ChildForm
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#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.listBox1 = new System.Windows.Forms.ListBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.comboBox2 = new System.Windows.Forms.ComboBox();
			this.comboBox3 = new System.Windows.Forms.ComboBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.comboBox4 = new System.Windows.Forms.ComboBox();
			this.comboBox5 = new System.Windows.Forms.ComboBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.FormattingEnabled = true;
			this.listBox1.Location = new System.Drawing.Point(6, 59);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(112, 134);
			this.listBox1.TabIndex = 0;
			// 
			// comboBox1
			// 
			this.comboBox1.FormattingEnabled = true;
			this.comboBox1.Location = new System.Drawing.Point(6, 59);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(174, 21);
			this.comboBox1.TabIndex = 1;
			// 
			// comboBox2
			// 
			this.comboBox2.FormattingEnabled = true;
			this.comboBox2.Location = new System.Drawing.Point(6, 86);
			this.comboBox2.Name = "comboBox2";
			this.comboBox2.Size = new System.Drawing.Size(174, 21);
			this.comboBox2.TabIndex = 2;
			// 
			// comboBox3
			// 
			this.comboBox3.FormattingEnabled = true;
			this.comboBox3.Location = new System.Drawing.Point(6, 113);
			this.comboBox3.Name = "comboBox3";
			this.comboBox3.Size = new System.Drawing.Size(174, 21);
			this.comboBox3.TabIndex = 3;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.comboBox5);
			this.groupBox1.Controls.Add(this.comboBox4);
			this.groupBox1.Controls.Add(this.comboBox3);
			this.groupBox1.Controls.Add(this.comboBox1);
			this.groupBox1.Controls.Add(this.comboBox2);
			this.groupBox1.Location = new System.Drawing.Point(166, 12);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(193, 199);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "These controls don\'t fully render in form_Shown() unless you call DoEvents() *bef" +
				"ore* loading listBox1";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.listBox1);
			this.groupBox2.Location = new System.Drawing.Point(12, 12);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(135, 199);
			this.groupBox2.TabIndex = 5;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "This list is loaded with 50k items in order to delay the initial rendering of the" +
				" form.";
			// 
			// comboBox4
			// 
			this.comboBox4.FormattingEnabled = true;
			this.comboBox4.Location = new System.Drawing.Point(6, 140);
			this.comboBox4.Name = "comboBox4";
			this.comboBox4.Size = new System.Drawing.Size(174, 21);
			this.comboBox4.TabIndex = 4;
			// 
			// comboBox5
			// 
			this.comboBox5.FormattingEnabled = true;
			this.comboBox5.Location = new System.Drawing.Point(6, 167);
			this.comboBox5.Name = "comboBox5";
			this.comboBox5.Size = new System.Drawing.Size(174, 21);
			this.comboBox5.TabIndex = 5;
			// 
			// ChildForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(374, 224);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox2);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ChildForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "ChildForm";
			this.Load += new System.EventHandler(this.ChildForm_Load);
			this.Shown += new System.EventHandler(this.ChildForm_Shown);
			this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ChildForm_FormClosed);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.ComboBox comboBox2;
		private System.Windows.Forms.ComboBox comboBox3;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.ComboBox comboBox5;
		private System.Windows.Forms.ComboBox comboBox4;
	}
}
 
 
 
 
using System;
using System.Windows.Forms;

namespace FormLoadCompletedDemo
{
	public partial class ChildFormUsingBase : BaseForm
	{
		public ChildFormUsingBase()
		{
			InitializeComponent();
		}

		private void ChildFormUsingBase_LoadCompleted()
		{
			this.Cursor = Cursors.WaitCursor;

			for (int l = 0; l < 50000; l++)
				this.listBox1.Items.Add("Item " + l.ToString());

			this.Cursor = Cursors.Default;
		}

		private void ChildFormUsingBase_FormClosed(object sender, FormClosedEventArgs e)
		{
			// Without the following, subsequent instances of this form would load
			//  slower than the 1st call, distorting the time-delay of this demo.
			listBox1.Items.Clear();
			GC.Collect();

		}
	}
}
 
 
 
namespace FormLoadCompletedDemo
{
	partial class ChildFormUsingBase
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#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.listBox1 = new System.Windows.Forms.ListBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.comboBox2 = new System.Windows.Forms.ComboBox();
			this.comboBox3 = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.FormattingEnabled = true;
			this.listBox1.Location = new System.Drawing.Point(12, 12);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(120, 95);
			this.listBox1.TabIndex = 0;
			// 
			// comboBox1
			// 
			this.comboBox1.FormattingEnabled = true;
			this.comboBox1.Location = new System.Drawing.Point(192, 21);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(121, 21);
			this.comboBox1.TabIndex = 1;
			// 
			// comboBox2
			// 
			this.comboBox2.FormattingEnabled = true;
			this.comboBox2.Location = new System.Drawing.Point(192, 48);
			this.comboBox2.Name = "comboBox2";
			this.comboBox2.Size = new System.Drawing.Size(121, 21);
			this.comboBox2.TabIndex = 2;
			// 
			// comboBox3
			// 
			this.comboBox3.FormattingEnabled = true;
			this.comboBox3.Location = new System.Drawing.Point(192, 75);
			this.comboBox3.Name = "comboBox3";
			this.comboBox3.Size = new System.Drawing.Size(121, 21);
			this.comboBox3.TabIndex = 3;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(22, 129);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(277, 35);
			this.label1.TabIndex = 4;
			this.label1.Text = "This form performs post-Shown() processing by attaching to BaseForm\'s LoadComplet" +
				"ed() event";
			// 
			// ChildFormUsingBase
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(328, 187);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.comboBox3);
			this.Controls.Add(this.comboBox2);
			this.Controls.Add(this.comboBox1);
			this.Controls.Add(this.listBox1);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ChildFormUsingBase";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "ChildFormUsingBase";
			this.LoadCompleted += new FormLoadCompletedDemo.BaseForm.LoadCompletedEventHandler(this.ChildFormUsingBase_LoadCompleted);
			this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ChildFormUsingBase_FormClosed);
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.ComboBox comboBox2;
		private System.Windows.Forms.ComboBox comboBox3;
		private System.Windows.Forms.Label label1;
	}
}
 
 
次は工事ですので、ご連絡くださいね