Windows窗体 - 从类型的按钮对象获取的文本值 [英] Windows Forms - get Text value from object of type button
问题描述
我有这种形式命名为内一个名为Windows窗体 Form1中
和面板 PANEL1
。我用的面板只有将其置于按钮,让我可以将它们分组,并与他们分别来自其他按钮在我的 Form1中
工作。对于我的计划的目的,我需要处理来自按钮所做的每一个按钮,点击里面的 PANEL1
。为此,我使用相同的代码片段:
公共Form1中()
{
的InitializeComponent() ;
//设置点击事件处理程序,在面板
的foreach按钮(VAR按钮panel1.Controls.OfType<按钮和GT;())
{
button.Click + = HandleClick;
}
}
我需要做的是有办法准确识别被点击了哪个按钮。为此,我打一点点我的处理方法:
私人无效HandleClick(对象o,EventArgs五)
{
MessageBox.Show(HI+ o.ToString());
}
这给了我一些希望,因为我得到这样的:
这是第二部分 - 正文:将Button4
这实际上是足够的信息来继续我的工作。但我不能找到一个办法让这一块的信息,而一些复杂的字符串操作。那么,有没有办法让有关按钮本或其他唯一信息被点击给我写我的代码的方式?
私人无效HandleClick(对象发件人,EventArgs五)
{
VAR BTN =发件人的按钮;
如果(BTN!= NULL)
{
MessageBox.Show(btn.Text);
}
}
I have a Windows form named Form1
and panel within this form named panel1
. I use the panel only to place buttons there so that I can group them and work with them separately from the other buttons in my Form1
. For the purpose of my program I need to handle every button click made from the buttons inside panel1
. For this purpose I use the same code snippet:
public Form1()
{
InitializeComponent();
// Set a click event handler for the button in the panel
foreach (var button in panel1.Controls.OfType<Button>())
{
button.Click += HandleClick;
}
}
What I need to do is to have a way to identify which button exactly has been clicked. For this purpose I played a little bit with my handler method:
private void HandleClick(object o, EventArgs e)
{
MessageBox.Show("HI" + o.ToString());
}
which gave me some hope because I get this:
It's the second part - Text: button4
which is actually enough information to continue with my work. But I can't find a way to get this piece of information without some complicated string manipulations. So is there a way to get this or other unique information about the button been clicked given the way I have written my code?
private void HandleClick(object sender, EventArgs e)
{
var btn = sender as Button;
if (btn != null)
{
MessageBox.Show(btn.Text);
}
}
这篇关于Windows窗体 - 从类型的按钮对象获取的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!