`

winform 让事件提前运行

    博客分类:
  • C#
 
阅读更多

今天遇到了一个问题,就是让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;
	}
}
 

 

下面给出工程,有问题请联系我哦

 

 

 

 

0
0
分享到:
评论

相关推荐

    winform带常用数据验证功能的文本框

    6. **异常处理**:有效的数据验证能减少程序运行时的异常,因为输入错误会提前被捕捉到。如果输入不符合规则,控件可能会抛出异常,或者显示错误消息给用户。 7. **用户体验优化**:通过实时反馈,用户可以立即看到...

    一个优秀的ASP.NET(及WinForm)开发人员需要知道

    NGEN 则是在安装时提前生成本机代码,以提高应用程序的启动速度。 **2.13 .NET CLR 的内存管理机制** .NET Common Language Runtime (CLR) 负责管理内存分配和回收,使用自动垃圾回收机制来释放不再使用的对象所...

    WinForm的延时加载控件概述

    总的来说,WinForm的延时加载控件通过合理控制数据加载的时间点,提高了应用程序的运行效率,特别是在处理大数据量或复杂交互场景时。通过重写控件的特定方法、监听特定事件以及自定义数据绑定策略,开发者可以灵活...

    c#插件开发相关资料

    .NET Framework和.NET Core提供了强大的反射机制,可以让我们在运行时检查和操作类型。在插件开发中,反射常用于实例化动态加载的插件类,调用其方法,甚至创建未提前知道类型的对象。 5. **依赖注入** 为了降低...

    C#调用matlab画图,解决图像嵌入Winform窗体和首次画图慢的问题

    可以考虑提前加载和缩放图像,减少运行时负担。 3. 系统性能:硬件配置较低的系统可能会遇到性能问题。优化代码、提高系统性能或使用更高效的图像处理库可能有所帮助。 总结来说,通过C#调用MATLAB绘图并在Winform...

    面向对象C#

    - JIT 编译与传统的提前编译 (Ahead-Of-Time Compilation, AOT) 不同,AOT 在程序编译阶段就已经完成了所有编译工作。 - 通过 JIT 编译,C# 程序可以在多种不同的硬件平台上高效运行,而无需为每个平台单独编译。 ...

    图片懒加载

    4. **配合服务端合作**:服务端可以根据用户的设备特性、网络状况等信息,智能地决定哪些图片需要提前加载。 ### 五、实战案例——模仿京东懒加载 京东作为国内知名的电商平台,其图片懒加载策略值得学习。京东的...

    C#性能优化

    在C#性能优化领域,开发者们常常关注如何提高代码运行效率、减少资源消耗以及提升应用程序的整体性能。本文将深入探讨C#中涉及的几个关键优化点,包括数据库优化、服务器优化、线程管理和内存管理。 首先,我们来看...

    web service第一次启动慢

    4. 应用程序启动过程中的其他因素:除了web service本身,WinForm应用程序在首次运行时也可能需要初始化各种组件、资源加载等,这些都可能导致整体启动时间变长。 5. Web service的网络配置和硬件性能:Web service...

    dingshiqi-modify.rar

    对于winform应用来说,"dingshiqi-modify"可能包含一个图形用户界面(GUI),让用户直观地设置定时器参数,如提醒时间、提醒方式等。在WinForms中,我们可以使用DateTimePicker控件让用户选择提醒时间,CheckBox或...

    ListView显示大量图片时的性能优化

    在Android开发中,ListView是一个非常常见的组件...附带的文件"VirtualListView_Test"可能是用于测试和演示这些优化技术的代码实例,而".suo"和".sln"则是Visual Studio解决方案和配置文件,帮助开发者构建和运行项目。

    C#托盘程序

    本篇文章通过一个具体的C# Winform托盘程序案例,详细介绍了如何使用C#来开发一个托盘程序,包括托盘图标的初始化、上下文菜单的定义、事件处理以及显示和隐藏托盘图标等核心知识点。这些技术点不仅适用于本文中的...

    C#Winfrom4 4s客户关系管理系统

    同时,根据历史销售数据,系统可进行销售预测,帮助4S店提前规划库存和销售策略。 6. 售后服务管理:4S店的售后服务是客户满意度的重要来源。系统需包含预约管理、维修工单、配件管理等模块,确保服务流程顺畅,...

    讯飞语音离线命令词识别示例demo

    开发者需要根据应用场景提前设定并录入这些命令词,例如“打开灯”、“关闭电视”等。 3. **录音与音频处理**:在用户说出命令词后,Demo会捕获音频数据,然后对其进行降噪、分割等预处理,确保语音信号的质量,...

    C#实现的MES车间信息控制系统

    8. **设备管理**:集成设备管理系统,监控设备运行状态,预测故障,提前安排维修,减少停机时间。 9. **物料跟踪**:通过条形码或RFID技术,追踪物料从入库到出库的全程,确保物料流向的透明度。 10. **接口集成**...

    自定义表达式计算 (c#)

    5. **性能优化**:对于计算量大的表达式,引擎可能需要进行优化,例如,提前计算常量表达式,或者在构建AST时消除无用的操作。 在"ExpressionEngine1"这个压缩包中,很可能包含了实现上述功能的源代码。通过研究...

    c# 图书管理系统,基于sql 2000, vs2008

    9. **后台管理**:提供给管理员的专用界面,用于监控系统运行情况,处理异常问题,以及执行高级操作。 10. **图书多条件搜索**:用户可以根据书名、作者、出版社等信息进行多条件搜索,快速找到目标图书。 11. **...

    C#程序员应该养成的程序性能优化写法

    优化后的代码通过提前存储这两个值,减少了冗余计算。类似地,如果在方法中多次创建相同对象,如正则表达式,应当考虑将其声明为静态常量或局部变量,以便重用。 2. **避免不必要的对象实例化**:在`Check`方法的...

    深入本机影像生成器(Ngen.exe)工具使用方法详解

    如果你在做的是一个较大的winform程序或者silverlight等客户端程序时就需要考虑提前编译了。.Net framework安装目录下(类似C:\Windows\Microsoft.NET\Framework\v4.0.30319)有一个ngen.exe工具,就是做这件事儿的...

Global site tag (gtag.js) - Google Analytics