动态生成一批btton 控件和时间Timer组件取名称问题
C#2.0 winform中我动态生成一批btton 控件和时间Timer组件生成事件如下:
btnArray[n].Click += new System.EventHandler(ClickButton);
timArray[n].Tick += new System.EventHandler(timerTick);
请问大家怎能在事件public void ClickButton(object sender, System.EventArgs e)和 private void timerTick(Object sender, System.EventArgs e)取得他们的名称??谢谢 --------------------编程问答-------------------- Button btn=(Button)sender
btn.Name
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- 你在动态生成时最好也给它们赋个名称,这样feiyun0112代码才有意义 --------------------编程问答-------------------- 有名称的啦,现在是Button单击的时候要知道是那个按钮
还是时间组件 --------------------编程问答-------------------- 用一楼的方法
在buttton的onclick事件中。
Button btn=(Button)sender ;
string a=btn.Name;
--------------------编程问答-------------------- ((Button)sender).Name.ToString() --------------------编程问答-------------------- 取sender, --------------------编程问答-------------------- 好奇怪我取和Name值为空,怎解决 --------------------编程问答-------------------- 代码我发一下,以免大家误解
private int timerNum;
private int btnNum; // number of wanted Button
private int lblNum;
private System.Windows.Forms.Timer[] timArray;
private System.Windows.Forms.Button[] btnArray; // Declaring array of Button
private System.Windows.Forms.Label[] lblArray; // Declaring array of Label
private System.Windows.Forms.TextBox[] txtArray; // Declaring array of TextBox
private void button1_Click(object sender, EventArgs e)
{
ShowTimer();
}
private void ShowTimer()
{
timerNum = 6;
AddControls("tim", timerNum); // Create six Timer
int n = 1;
while (n < timerNum + 1)
{
timArray[n].Interval = 10000;
timArray[n].Enabled = true;
timArray[n].Tick += new System.EventHandler(timerTick);
n++;
}
}
private void AddControls( string anyControl, int cNumber)
{
switch (anyControl)
{
case "btn": // anyControl = btn to Add Button
{
// assign number of controls
btnArray = new System.Windows.Forms.Button[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
btnArray[i] = new System.Windows.Forms.Button();
}
break;
}
case "lbl": // anyControl = lbl to Add Label
{
// assign number of controls
lblArray = new System.Windows.Forms.Label[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
lblArray[i] = new System.Windows.Forms.Label();
}
break;
}
case "txt": // anyControl = txt to Add TextBox
{
// assign number of controls
txtArray = new System.Windows.Forms.TextBox[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
txtArray[i] = new System.Windows.Forms.TextBox();
}
break;
}
case "tim":
{
timArray=new System.Windows.Forms.Timer[cNumber + 1];
for(int i=0;i<cNumber + 1;i++)
{
timArray[i] = new System.Windows.Forms.Timer(this.components);
}
break;
}
}
}
private void timerTick(Object sender, System.EventArgs e)
{
string n = ((System.Windows.Forms.Timer)sender).Site.Name.ToString();
MessageBox.Show("You have clicked button ");
}
private void button2_Click(object sender, EventArgs e)
{
ShowButton();
}
private void ShowButton()
{
int xPos = 8;
int yPos = 8;
btnNum = 6;
AddControls("btn", btnNum); // Create six Buttons
int n = 1;
while (n < btnNum + 1)
{
btnArray[n].Tag = n;
btnArray[n].Width = 72;
btnArray[n].Height = 24;
btnArray[n].Text = "Button" + btnArray[n].Tag.ToString();
if (yPos > 80) // Three buttons in one column
{
yPos = 8;
xPos = xPos + btnArray[n].Width + 8;
}
btnArray[n].Left = xPos;
btnArray[n].Top = yPos;
yPos = yPos + btnArray[n].Height + 8;
this.Controls.Add(btnArray[n]); // Let panel hold the Buttons
// the Event of click Button
btnArray[n].Click += new System.EventHandler(ClickButton);
n++;
}
}
//===================== Functions for Button Array ======================
// Result of the event click Button
public void ClickButton(object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("You have clicked button " +
((System.Windows.Forms.Button)sender).Tag.ToString());
string ddd = ((System.Windows.Forms.Button)sender).Text.ToString();
//Button btn=(Button)sender;
string btnname = ((Button)sender).Name.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
//string n = ((System.Windows.Forms.Timer)sender).Site.Component;
string tt = sender.ToString();
}
} --------------------编程问答-------------------- 取名称的目的是什么?
你可以使用((Button)sender).Text等属性来判断
也可以用Tag来记录下标
btnArray[n].Tag = n;
btnArray[n].Click += new System.EventHandler(ClickButton);
public void ClickButton(object sender, System.EventArgs e)--------------------编程问答-------------------- 你生成Button的时候,没有赋名称呀.应该如下:
{
//btnArray[(int)((Button)sender).Tag];
}
case "btn ": // anyControl = btn to Add Button
{
// assign number of controls
btnArray = new System.Windows.Forms.Button[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
btnArray[i] = new System.Windows.Forms.Button();
btnArray[i].Name = "btn"+i.ToString() ;
}
break;
}
--------------------编程问答-------------------- 你生成Button的时候,没有赋名称呀.应该如下:
case "btn ": // anyControl = btn to Add Button
{
// assign number of controls
btnArray = new System.Windows.Forms.Button[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
btnArray[i] = new System.Windows.Forms.Button();
btnArray[i].Name = "btn "+i.ToString();
}
break;
}
--------------------编程问答-------------------- 楼上是对的,但是Timer是没有Name这个属性的
其他我最想要就是知道那个Timer
private void timerTick(Object sender, System.EventArgs e)
{
string n = ((System.Windows.Forms.Timer)sender).Site.Name.ToString();
MessageBox.Show( "You have clicked button ");
}
--------------------编程问答-------------------- 我要取得是那个时间Timer控件的Tick然后再去执行另一个传入参数到另一个函数执行如下
private void timerTick(Object sender, System.EventArgs e)
{
int yy=取得Timer控件;
CheckID(yy);
}
--------------------编程问答-------------------- Timer也是有Name属性的呀。
--------------------编程问答-------------------- 1、你在创建Timer的时候,把Name赋上;
2、取法跟Button是一样的;
--------------------编程问答-------------------- wisest(无影无忠)你试过没有,我试过了是没有的 --------------------编程问答-------------------- 不信你在代码里写 --------------------编程问答-------------------- 哦,我没看清楚。
那你这样子做吧
创建Timer:
Timer t = new Timer(100);
t.tag="12345";
接下去,你会做了吧? --------------------编程问答-------------------- 谢谢,搞定了,怎么结贴给分
补充:.NET技术 , C#