⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycomposite.cs

📁 很好的学习资料,对菜鸟学习c#很有帮助啊,希望你们好好学习
💻 CS
字号:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomControls 
{
	/// <summary>
	/// WebCustomControl1 的摘要说明。
	/// </summary>
	[DefaultProperty("Text"),
		ToolboxData("<{0}:Composite runat=server></{0}:Composite>")]
	public class Composite : Control, INamingContainer
	//标识在 Page 对象的控件层次结构内创建新 ID 命名空间的容器控件。这仅是一个标记接口。
	{
		private int number = 100;
		private Label label;
            
		public int Number
		{
			get
			{
				return number;
			}
			set
			{
				number = value;
			}
		}
            
		private int Sum
		{
			get 
			{
				EnsureChildControls();
				return Int32.Parse(((TextBox)Controls[1]).Text) + 
					Int32.Parse(((TextBox)Controls[4]).Text);
			}
                  
		}

		public string Text
		{
			get
			{
				EnsureChildControls();
				return label.Text;
			}
			set
			{
				EnsureChildControls();
				label.Text = value;
			}
		}
            
            
		public event CheckEventHandler Check;
            
		protected virtual void OnCheck(CheckEventArgs ce)
		{
			if (Check != null)
			{
				Check(this,ce);
			}
		}
            
		protected override void CreateChildControls() 
		{
                  
			Controls.Add(new LiteralControl("<h3>第一个数字 : "));
                  
			TextBox box1 = new TextBox();
			box1.Text = "0";
			Controls.Add(box1);
                  
			Controls.Add(new LiteralControl("</h3>"));
                  
			Controls.Add(new LiteralControl("<h3>第二个数字 : "));
                  
			TextBox box2 = new TextBox();
			box2.Text = "0";
			Controls.Add(box2);
                  
			Controls.Add(new LiteralControl("</h3>"));
                  
			Button button1 = new Button();
			button1.Text = "提交";
			Controls.Add(new LiteralControl("<br>"));
			Controls.Add(button1);
			button1.Click += new EventHandler(this.ButtonClicked);
                  
			Controls.Add(new LiteralControl("<br><br>"));
			label = new Label();
			label.Height = 50;
			label.Width = 500;
			label.Text = "点击提交按钮看是否匹配";
			Controls.Add(label);
                  
		}
            
		protected override void OnPreRender(EventArgs e)
		{
			((TextBox)Controls[1]).Text = "0";
			((TextBox)Controls[4]).Text = "0";
		}
            
		private void ButtonClicked(Object sender, EventArgs e)
		{
			OnCheck(new CheckEventArgs(Sum - Number));
		}
	}
	public class CheckEventArgs : EventArgs
	{
		private bool match = false;
            
		public CheckEventArgs (int difference)
		{
			if (difference == 0)
			{
				match = true;
			}
		}
		public bool Match
		{
			get
			{
				return match;
			}
		}
	}
      
	public delegate void CheckEventHandler(object sender, CheckEventArgs ce);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -