本文整理汇总了C#中System.Windows.Forms.Control.Name属性的典型用法代码示例。如果您正苦于以下问题:C# Control.Name属性的具体用法?C# Control.Name怎么用?C# Control.Name使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。
在下文中一共展示了Control.Name属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Form1_Load
// This example demonstrates the use of the ControlAdded and
// ControlRemoved events. This example assumes that two Button controls
// are added to the form and connected to the addControl_Click and
// removeControl_Click event-handler methods.
private void Form1_Load(object sender, System.EventArgs e)
{
// Connect the ControlRemoved and ControlAdded event handlers
// to the event-handler methods.
// ControlRemoved and ControlAdded are not available at design time.
this.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.Control_Removed);
this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Control_Added);
}
private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
MessageBox.Show("The control named " + e.Control.Name + " has been added to the form.");
}
private void Control_Removed(object sender, System.Windows.Forms.ControlEventArgs e)
{
MessageBox.Show("The control named " + e.Control.Name + " has been removed from the form.");
}
// Click event handler for a Button control. Adds a TextBox to the form.
private void addControl_Click(object sender, System.EventArgs e)
{
// Create a new TextBox control and add it to the form.
TextBox textBox1 = new TextBox();
textBox1.Size = new Size(100,10);
textBox1.Location = new Point(10,10);
// Name the control in order to remove it later. The name must be specified
// if a control is added at run time.
textBox1.Name = "textBox1";
// Add the control to the form's control collection.
this.Controls.Add(textBox1);
}
// Click event handler for a Button control.
// Removes the previously added TextBox from the form.
private void removeControl_Click(object sender, System.EventArgs e)
{
// Loop through all controls in the form's control collection.
foreach (Control tempCtrl in this.Controls)
{
// Determine whether the control is textBox1,
// and if it is, remove it.
if (tempCtrl.Name == "textBox1")
{
this.Controls.Remove(tempCtrl);
}
}
}
示例2: Form1
//引入命名空间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
Label Label1;
TextBox TextBox1;
Button Button1;
public Form1()
{
InitializeComponent();
}
private void ctrlClick(System.Object sender, EventArgs e)
{
Control ctrl = (Control)sender;
MessageBox.Show("You clicked: " + ctrl.Name);
}
private void InitializeComponent()
{
this.Label1 = new System.Windows.Forms.Label();
this.TextBox1 = new System.Windows.Forms.TextBox();
this.Button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Label1
//
this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.Label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Label1.Location = new System.Drawing.Point(14, 97);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(112, 24);
this.Label1.TabIndex = 8;
this.Label1.Text = "Label1";
this.Label1.Click += new System.EventHandler(this.ctrlClick);
//
// TextBox1
//
this.TextBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TextBox1.Location = new System.Drawing.Point(12, 61);
this.TextBox1.Name = "TextBox1";
this.TextBox1.Size = new System.Drawing.Size(156, 21);
this.TextBox1.TabIndex = 7;
this.TextBox1.Text = "TextBox1";
this.TextBox1.Click += new System.EventHandler(this.ctrlClick);
//
// Button1
//
this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.Button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Button1.Location = new System.Drawing.Point(12, 21);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(96, 28);
this.Button1.TabIndex = 6;
this.Button1.Text = "Button1";
this.Button1.Click += new System.EventHandler(this.ctrlClick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(367, 281);
this.Controls.Add(this.Label1);
this.Controls.Add(this.TextBox1);
this.Controls.Add(this.Button1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.Text = "Control Medley";
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}