2010-07-30 36 views
5

假設我們在控件的值字段中顯示0,我希望如果值爲0 - display string.Empty(我知道值的類型是十進制的,並且不能插入字符串而不是小數點,但是仍然...也許有一些可能的格式?)。我可以在NumericUpDown控件中隱藏值嗎?

回答

4

注:這是依賴於目前執行的NumericUpDown

你需要做的是創建一個新的控制,從NumericUpDown繼承這樣的:

public partial class SpecialNumericUpDown : NumericUpDown 
{ 
    public SpecialNumericUpDown() 
    { 
     InitializeComponent(); 
    } 

    protected override void UpdateEditText() 
    { 
     if (this.Value != 0) 
     { 
      base.UpdateEditText(); 
     } 
     else 
     { 
      base.Controls[1].Text = ""; 
     } 
    } 
} 
1

似乎只有非常有限的支持更改格式。

我沒有嘗試過這個我自己。但是你可以創建一個子類並覆蓋UpdateEditText方法來支持你的自定義格式。事情是這樣的:

protected override void UpdateEditText() 
{ 
    this.Text = Value.ToString(); // Insert your formatting here 
} 
1
public partial class MyNumericUpDown : NumericUpDown 
{ 
    public override string Text 
    { 
     get 
     { 
      if (base.Text.Length == 0) 
      { 
       return "0"; 
      } 
      else 
      { 
       return base.Text; 
      } 
     } 
     set 
     { 
      if (value.Equals("0")) 
      { 
       base.Text = ""; 
      } 
      else 
      { 
       base.Text = value; 
      } 
     } 
    } 
} 
-1

如果你只想隱藏用戶的價值,可以使ForeColor一樣BackColor所以NumericUpDown內的值對用戶是不可見的。

相關問題