Button.Click EventHandler问题 [英] Button.Click EventHandler problem
问题描述
大家好.
我正在制作一个小程序,每次按下按钮时都可以添加一个分组框(参见图片) .我的问题是,当我按下组框中的添加"按钮时,我需要使用事件处理程序在文本框中获取文本.
Hello all.
I am making a small program where I can add an groupbox every time a button is pushed ( See picture ). The problem I have is that, when I push the Add button in the groupbox, I need to get the text in the textbox with the eventhandler
private void button1_Click(object sender, EventArgs e)
{
Button button1 = new Button();
Label lbText = new Label();
GroupBox groupBox1 = new GroupBox();
groupBox1.Text = "Pipe pice number " + pipe;
groupBox1.Size = new System.Drawing.Size(300, 139);
button1.Location = new Point(220, 100);
button1.Text = "Add";
lbText.Text = "Pipe Size: ";
lbText.Location = new Point(100, 100);
lbText.Size = new System.Drawing.Size(90, 13);
lbText.Visible = true;
TextBox text = new TextBox();
text.Location = new Point(100, 100);
text.Size = new System.Drawing.Size(100, 20);
// Set the FlatStyle of the GroupBox.
groupBox1.FlatStyle = FlatStyle.Flat;
// Add the Button to the GroupBox.
groupBox1.Controls.Add(button1);
// Add the textbox to the GroupBox
groupBox1.Controls.Add(text);
// Add the label to the Groupbox
groupBox1.Controls.Add(lbText);
// Add the GroupBox to the Form.
flowLayoutPanel1.Controls.Add(groupBox1);
i += 10;
pipe += 1;
label1.Text = Convert.ToString(i);
button1.Click += new EventHandler(b_Click);
sender = text.Text;
}
private void b_Click(object sender, EventArgs e)
{
string btn = ((Button)sender).Name;
Console.WriteLine(btn);
textbox1.Text("Display the text from groupbox(X)");
}
这可能吗?
最好的问候,
Morten Starck
Is this possible?
Best Regards,
Morten Starck
推荐答案
如果您正在使用v.3.5,并且已经能够在运行时编写代码安排控件,那么该成长为不再使用无助的new EventHandler(...)
的成年状态.
现在,您遇到了将TextBox
实例传递给按钮单击处理程序的问题.坦率地说,最好的明显方法是将按钮子类化并添加所需的属性:
If you''re using v.3.5 and already able to write code arranging control during run-time, it''s time to grow up to the adult state who no longer use helplessnew EventHandler(...)
.
Now, you problem to pass theTextBox
instance to the handler of button click. Frankly, the best obvious way is to subclass the button and add the required property:
class TextAwareButton : Button {
protected override void OnClick(EventArgs e) {
if (TextSource != null || TextHandler != null)
TextHandler(TextSource.Text); //whatever is that
}
public Control TextSource { get; set; }
public Action<string> TextHandler { get; set; }
} //class TextAwareButton
对于给定的TextAwareButton
实例,设置其关联的TextBox
或其他Control
实例和TextHandler
动作,就可以完成.
如果您不想继承子类(例如,懒惰:),或者代码变得垃圾,您很快就需要),则可以使用Control.Tag
并将所需的数据放入此Tag
中;它需要转换回您的类型:
For a given instance of you TextAwareButton
, set its associated TextBox
or other Control
instance and TextHandler
action and you''re done.
If you don''t want to subclass (lazy, for example :) , or the code goes to junk, you need it fast), you can use Control.Tag
and put data you want in this Tag
; it will require casting back to your type:
TextBox text = new TextBox();
Button myButton = new Button();
//...
myButton.Tag = text;
myButton.Click += (sender, eventArgs) {
Button btn = (Button)sender;
TextBox text = (TextBox)btn.Tag;
ProcessText(text.Text);
};
这篇关于Button.Click EventHandler问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!