由于開(kāi)發(fā)中業(yè)務(wù)信息的不確定性,,今天制作了個(gè)usercontrol組件,該用戶控件中添加了若干個(gè)子控件(如button,、textbox,、label),但是在winform應(yīng)用程序中調(diào)用時(shí),,發(fā)現(xiàn)無(wú)法對(duì)其子控件(如button)進(jìn)行單擊自動(dòng)生成button click事件代碼的問(wèn)題,,通過(guò)google搜索了下,大致找到了解決方法,,現(xiàn)在記錄下來(lái),,僅作為開(kāi)發(fā)筆記,如瀏覽本帖的人員有其他更好的方法,,或者對(duì)其原理進(jìn)行闡述的,,可以留言,謝謝各位
測(cè)試代碼
1.首先先創(chuàng)建usercontrol用戶控件,,具體如何創(chuàng)建各位可去進(jìn)行g(shù)oogle搜索,,或者查找相關(guān)書(shū)籍教程
設(shè)計(jì)器文件 UserControl1.Designer.cs
- namespace WindowsControlLibrary1
- {
- partial class UserControl1
- {
-
-
-
- private System.ComponentModel.IContainer components = null;
-
-
-
-
-
-
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
-
- #region 組件設(shè)計(jì)器生成的代碼
-
-
-
-
-
-
- private void InitializeComponent()
- {
- this.button1 = new System.Windows.Forms.Button();
- this.label1 = new System.Windows.Forms.Label();
- this.textBox1 = new System.Windows.Forms.TextBox();
- this.SuspendLayout();
-
-
-
- this.button1.Location = new System.Drawing.Point(63, 69);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 0;
- this.button1.Text = "button1";
- this.button1.UseVisualStyleBackColor = true;
-
-
-
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(70, 114);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(41, 12);
- this.label1.TabIndex = 1;
- this.label1.Text = "label1";
-
-
-
- this.textBox1.Location = new System.Drawing.Point(63, 151);
- this.textBox1.Name = "textBox1";
- this.textBox1.Size = new System.Drawing.Size(100, 21);
- this.textBox1.TabIndex = 2;
-
-
-
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;
- this.Controls.Add(this.textBox1);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.button1);
- this.Name = "UserControl1";
- this.Size = new System.Drawing.Size(269, 232);
- this.ResumeLayout(false);
- this.PerformLayout();
-
-
- }
-
- #endregion
-
-
- public System.Windows.Forms.Button button1;
- public System.Windows.Forms.Label label1;
- public System.Windows.Forms.TextBox textBox1;
- }
- }
代碼文件UserControl1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsControlLibrary1
- {
- public partial class UserControl1 : UserControl
- {
- public UserControl1()
- {
- InitializeComponent();
- }
- }
- }
2.需要將usercontrol中的子控件(如 button)屬性中的Modifiers中的值改為Public
3.進(jìn)行編譯,,編譯成功將生成的usercontrol dll通過(guò)鼠標(biāo)拖動(dòng)工具箱中。
4.在該解決方案中,,添加winform應(yīng)用程序項(xiàng)目,,然后將剛剛添加到工具箱中的usercontrol 控件拖動(dòng)到winform窗體中
5.開(kāi)始在winform應(yīng)用程序中編寫(xiě)usercontrol 子控件(button)的單擊事件代碼
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void userControl11_button1_Click(object sender, EventArgs e)
- {
- MessageBox.Show("userControl11 button1 Click");
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- this.userControl11.button1.Click += userControl11_button1_Click;
- }
- }
- }
6.編譯winform應(yīng)用程序,運(yùn)行后點(diǎn)擊usercontrol中的button即可看到效果,。