如何从C#中的Object-type参数中获取Text-property?

时间:2015-03-03 21:14:36

标签: c#

我有这样的按钮点击处理程序:

private void button2_Click(object sender, EventArgs e)
{
   MessageBox.Show(sender.Text);
}

我想知道发件人对象的文本值。如果发件人有对象类型,如何不包含文本属性,该怎么做?

1 个答案:

答案 0 :(得分:2)

你需要施展它:

var control = sender as Control;
if(control != null)
{
    MessageBox.Show(control.Text);
}