2011-09-15 38 views
1

我讓一個Supertooltip(來自DotNetBar)出現在NumericUpDown的每一個控制。但是我只需要在NumericUpDown的TextBox上使用supertooltip。這是我當前的代碼:做功能只有1控制NumericUpDown

foreach (Control c in NumericUpDown.Controls) 
{ 
    NumericUpDownToolTip.SetSuperTooltip(c, NumericUpDownSuperToolTip); 
} 

//Declarations: 
//NumericUpDownToolTip is a SuperToolTip from DotNetBar 
//NumericUpDownSuperToolTip is the configuration of the SuperToolTip (for example: the text of the tooltip) 

那麼如何設置工具提示只在文本框?

回答

2

修改您的foreach是這樣的:

foreach (Control c in NumericUpDown.Controls.OfType<TextBox>()) 
+0

謝謝!短代碼,簡單,像魅力一樣工作。 – HitomiKun

0

你能做到這一點的老式方法:

foreach (Control c in NumericUpDown.Controls) 
{ 
    if (!(c is TextBox)) continue; 
    NumericUpDownToolTip.SetSuperTooltip(c, NumericUpDownSuperToolTip); 
} 

或者使用LINQ來完成相同的

var controls = NumericUpDown.Controls.Where(c => c is TextBox); 

foreach (Control c in controls) 
    NumericUpDownToolTip.SetSuperTooltip(c, NumericUpDownSuperToolTip); 
+0

謝謝你你的快速答案和很好的解釋!但我更喜歡較短的代碼。 – HitomiKun