[英]Get name of control calling method
我的表單有幾個數字向上控件。 所有這些控件在更改時調用相同的方法:
private void SetColors(object sender, EventArgs e)
如何確定哪種控件稱為方法?
這就是sender
參數的用途。
如果您知道時間,可以適當地進行投射:
NumericUpDownControl control = (NumericUpDownControl) sender;
如果它可以是幾種類型的,你可以使用as
和空測試,或者is
隨后鑄造。
當然,您只需要轉換為包含所需成員的類型 - 例如,您可能只是轉換為Control
。
編輯:假設您只想要名稱,並且您知道發件人將始終是某種控件。 您可以使用:
private void SetColors(object sender, EventArgs e)
{
Control control = (Control) sender;
String name = control.Name;
// Use the name here
}
發生事件的控制存儲在變量sender
。 您只需將其轉換回原始類型即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.