您的当前位置: 源码爱好者 » 网页制作教程(编程教程) » C#技术教程

C#中的模式与非模式对话框窗口实例

  • 标签:非模态对话框 更新时间:2014-05-07
  • C#创建模式对话框与非模式对话框,在窗口中生成这两种类型的对话框,点击窗口中按钮可演示这两种窗口的效果,两个代码文件,后附完整代码的下载链接:

    Form1.cs

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    namespace Modal
    {
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.Button button2;
    		private System.ComponentModel.Container components = null;
    		public Form1()
    		{
    			InitializeComponent();
    		}
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null)
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    		private void InitializeComponent()
    		{
    			this.button1 = new System.Windows.Forms.Button();
    			this.button2 = new System.Windows.Forms.Button();
    			this.SuspendLayout();
    			//
    			// button1
    			//
    			this.button1.Location = new System.Drawing.Point(104, 64);
    			this.button1.Name = "button1";
    			this.button1.TabIndex = 0;
    			this.button1.Text = "模式窗体";
    			this.button1.Click += new System.EventHandler(this.button1_Click);
    			//
    			// button2
    			//
    			this.button2.Location = new System.Drawing.Point(104, 144);
    			this.button2.Name = "button2";
    			this.button2.TabIndex = 1;
    			this.button2.Text = "非模式窗体";
    			this.button2.Click += new System.EventHandler(this.button2_Click);
    			//
    			// Form1
    			//
    			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    			this.ClientSize = new System.Drawing.Size(292, 266);
    			this.Controls.Add(this.button2);
    			this.Controls.Add(this.button1);
    			this.Name = "Form1";
    			this.Text = "模式与非模式";
    			this.ResumeLayout(false);
    		}
    		#endregion
    		/// <summary>
    		/// 应用程序的主入口点。
    		/// </summary>
    		[STAThread]
    		static void Main()
    		{
    			Application.Run(new Form1());
    		}
    		//button1,显示一个模式对话框
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			Form form = new Form2( );
    			form.ShowDialog( );
    		}
    		//button2,显示一个非模式对话框
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			Form form = new Form2( );
    			form.Show( );
    		}
    	}
    }

    Form2.cs

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    namespace Modal
    {
    	public class Form2 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Label label1;
    		private System.ComponentModel.Container components = null;
    		public Form2()
    		{
    			InitializeComponent();
    		}
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if(components != null)
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    		private void InitializeComponent()
    		{
    			this.label1 = new System.Windows.Forms.Label();
    			this.SuspendLayout();
    			//
    			// label1
    			//
    			this.label1.Location = new System.Drawing.Point(80, 16);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(152, 40);
    			this.label1.TabIndex = 0;
    			this.label1.Text = "label1";
    			//
    			// Form2
    			//
    			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    			this.ClientSize = new System.Drawing.Size(292, 66);
    			this.Controls.Add(this.label1);
    			this.MaximizeBox = false;
    			this.MinimizeBox = false;
    			this.Name = "Form2";
    			this.Text = "窗体演示";
    			this.Load += new System.EventHandler(this.Form2_Load);
    			this.ResumeLayout(false);
    		}
    		#endregion
    
    		//Form2,窗体初始化
    		private void Form2_Load(object sender, System.EventArgs e)
    		{
    			if( Modal )
    				label1.Text = "这是一个模式窗口";
    			else
    				label1.Text = "这是一个非模式窗口";
    		}
    	}
    }

    C#创建模态与非模态窗口完整源码下载。

  • 已读: 次 收藏本文关闭本文打印本文复制链接