Can I hide Value in NumericUpDown control?
-
04-10-2020 - |
Question
Lets say we have 0
displayed in value field of the control and I want that if the value is 0
- display string.Empty
(I know that the type of value is decimal and there can be no string inserted instead of decimals in it, but still... Maybe there is some formatting possible there?).
Solution
Note: This is dependent on the current implementation of NumericUpDown
.
What you need to do is create a new control that inherits from NumericUpDown
such that:
public partial class SpecialNumericUpDown : NumericUpDown
{
public SpecialNumericUpDown()
{
InitializeComponent();
}
protected override void UpdateEditText()
{
if (this.Value != 0)
{
base.UpdateEditText();
}
else
{
base.Controls[1].Text = "";
}
}
}
OTHER TIPS
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;
}
}
}
}
It seems that there is only very limited support for changing the formatting.
I have not tried this myself. But you could create a subclass and override the UpdateEditText
method to support your custom format. Something like this:
protected override void UpdateEditText()
{
this.Text = Value.ToString(); // Insert your formatting here
}
If you only want to hide the value from the user, you can make ForeColor
the same as BackColor
so the value inside NumericUpDown
will be invisible to the user.