Can I hide Value in NumericUpDown control? - c#

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?).

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 = "";
}
}
}

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
}

An easier solution is calling the ResetText() method. You can restore the text changing the Value property.
Example code to hide text when NumericUpDown control is disabled, and restore it on enabled
private void NumericUpDown_EnabledChanged(object sender, EventArgs e)
{
if (numericUpDown.Enabled)
{
if (numericUpDown.Tag != null)
{
// Restore last value
numericUpDown.Value = (decimal)numericUpDown.Tag;
}
}
else
{
// Save last value
numericUpDown.Tag = numericUpDown.Value;
// Just to force value change
numericUpDown.Value = (numericUpDown.Value > numericUpDown.Minimum ? numericUpDown.Minimum : numericUpDown.Maximum);
// Clear text
numericUpDown.ResetText();
}
}

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.

Related

Reseting label back to design-time .Text value

I have a bunch of labels that I set their value in the designer and later during runtime update them, but after using them, I want to set them back to their default value. My intent with this is to reduce the amount of large code to help make it easier to read.
random example like, setting in the designer of lbl_fruit Text = no fruits available currently then
*code*
lbl_fruits.Text = "banana";
*code*
lbl_fruits.ResetText(); // I want something like this
lbl_fruits.Text = "no fruits available currently"; // Instead of this
The .ResetText(); doesn't work for this as the label text gets cleaned instead of returning to "no fruits available currently"
My current solution is making a custom label control.
public class ExLabel : Label
{
private string defaultValue = "";
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; this.Invalidate(); }
}
protected override void OnControlAdded(ControlEventArgs e)
{
defaultValue = this.Text;
MessageBox.Show("This code is being run");
base.OnControlAdded(e);
}
public void ResetValue()
{
this.Text = defaultValue;
}
}
This code currently solves my problem if I use the custom propriety I made, but for me the ideal solution would be to have the design-time text value as the default value and not an extra propriety I made. OnControlAdded() does not get executed, OnPaint() runs again when lbl_fruits.Text = "banana"; happens.
So the question is: Which event I can override so the code gets executed as soon as the label is loaded but doesn't run twice. And also, is there a simpler way of approaching this?
In the end the solution I used was this:
public class ExLabel : Label
{
private string defaultValue = "";
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; this.Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
if(defaultValue == "" && !this.Text.Contains("exLabel"))
{
defaultValue = this.Text;
}
base.OnPaint(e);
}
public void ResetValue()
{
this.Text = defaultValue;
}
}
public class ExLabel : Label
{
private string defaultValue = "";
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; this.Invalidate(); }
}
protected override void OnControlAdded(ControlEventArgs e)
{
defaultValue = this.Text;
MessageBox.Show("This code is being run");
base.OnControlAdded(e);
}
public void ResetValue()
{
this.Text = defaultValue;
}
}

Edit my control from designer

I created control which contains a label and a textbox next to it:
The hierarchy look like that:
Panel
->Panel
->TextBox
->Label
I want to be able to custom the it on designer like change the textbox and label size and the text of the textbox.
Is there a easier way to do it without the needs to add property for each datamember of each control and calculate the sizes?
I have this code right now:
public override string Text
{
set { this.PhoneLabel.Text = value; }
get { return this.PhoneLabel.Text; }
}
public Size SizePhone
{
set { PhoneTextBox.Size = value; }
get { return PhoneTextBox.Size; }
}
public Size SizeLabel
{
set { PhoneLabel.Size = value; }
get { return PhoneLabel.Size; }
}
public Point LocationLabel
{
set { PhoneLabel.Location = value; }
get { return PhoneLabel.Location; }
}
I want to change the size with the mouse like I design a form
Thank you

UserControl TextBox Validation in C#

I have a TextBox UserControl. I create a Dynamic Property for the Textbox for MaximumLength.
public int MaximumLength { get; set; }
private void txtLocl_KeyPress(object sender, KeyPressEventArgs e)
{
txtLocl.MaxLength = MaximumLength;//txtLocl is a Usercontrol Textbox..,
//txtLocl maxLength should be given by the user in WindowsForm
//that should be come to here...,
}
I show you the Image of the UserControl Properties in Windows Form
Now i want to verify when user change the value in that property...,
Implement a custom setter that checks if the value is valid.
public int MaximumLength
{
get
{
return this.maximumLength;
}
set
{
if(value <= 4)
{
MessageBox.Show("Value is too small.");
}
else this.maximumLength = value;
}
}
Edit: So implement a getter.

How to correctly override the TextBox.Text property

In Windows Forms and C#, I am inheriting from the TextBox class. I override the Text property from TextBox. Everything goes well until I try to use the TextChanged event. The OnTextChanged event does not work properly here, as the Text.set property is not invoked.
Initial field content 123, txpText.Text = 123
Field content changed to a , txpText.Text still 123
Field content changed to aa , txpText.Text still 123
Field content changed to aaa , txpText.Text still 123
Here is my custom TextBox code
public class ShowPartialTextBox : System.Windows.Forms.TextBox
{
private string _realText;
public override string Text
{
get { return _realText; }
set // <--- Not invoked when TextChanged
{
if (value != _realText)
{
_realText = value;
base.Text = _maskPartial(_realText);
//I want to make this _maskPartial irrelevant
}
}
}
protected override void OnTextChanged(EventArgs e)
{
//Always called. Manually invoke Text.set here? How?
base.OnTextChanged(e);
}
private string _maskPartial(string txt)
{
if (txt == null)
return string.Empty;
if (_passwordChar == default(char))
return txt;
if (txt.Length <= _lengthShownLast)
return txt;
int idxlast = txt.Length - _lengthShownLast;
string result = _lpad(_passwordChar, idxlast) + txt.Substring(idxlast);
return result;
}
}
Here is the Form class
public partial class Form1 : Form
{
private ShowPartialTextBox txpText;
private void InitializeComponent()
{
txpText = new ShowPartialTextBox();
txpText.Text "123";
txpText.TextChanged += new System.EventHandler(this.txpText_TextChanged);
}
private void txpText_TextChanged(object sender, EventArgs e)
{
label1.Text = txpText.Text; //always shows 123
}
}
I use _maskPartial. It is altering the displayed Text, while still preserving its real content. I want this custom TextBox to "almost" simulate PasswordChar property, with showing the last x characters.
Easy to see when you set a breakpoint on the Text property setter. You assume that typing in the text box will call the setter. It doesn't. One fix is this:
protected override void OnTextChanged(EventArgs e) {
_realText = base.Text;
base.OnTextChanged(e);
}
But you'll have to make that work with _maskPartial(), it surely isn't irrelevant.

C# validating data in multiple text boxes?

I have a C# form with multiple text boxes. Before proceeding I need to validate the inputs in the each text box. If my validation rule for each text box is same, Do I have any way to apply the same rule for all the fields at once. And my desired output is same. (I want to change the backcolour of the relevant textbox into pink) I mean I don't want to use anything like
validate_txtName();
validate_txtAddress();
validate_txtCity();
There should be some standard and easy way to do this.. I am seeking of that way ;)
First, put all the textboxes in a list. Then apply the ForEach function on the list, passing as argument the lambda expression that represents you're validation rule.
Edit:
I've found this example in my own code:
Core.Model.Settings.Labels.ToList()
.ForEach(x => schedulerStorage1.Appointments.Labels.Add(Color.FromArgb(x.ARGB), x.LabelName));
maybe foreach loop? :)
Write you own control which accepts a regular expression string for validation check during design time. At execution time handle the Validating event with one common handler. Following code does this. You can remove the errorprovider and just use the backcolor logic.
public class ValidatedTextBox : TextBox
{
private IContainer components;
private Color m_OldBackColor;
[Description("Color to be set when validation fails.")]
public Color BackColorOnFailedValidation
{
get
{
return m_BackColorOnFailedValidation;
}
set
{
m_BackColorOnFailedValidation = value;
}
}
private Color m_BackColorOnFailedValidation = Color.Yellow;
[Description("Message displayed by the error provider.")]
public string ErrorMessage
{
get
{
return m_ErrorMessage;
}
set
{
m_ErrorMessage = value;
}
}
private string m_ErrorMessage = "";
[Description("Regular expression string to validate the text.")]
public string RegularExpressionString
{
get
{
return m_RegularExpressionString;
}
set
{
m_RegularExpressionString = value;
}
}
private string m_RegularExpressionString = "";
private ErrorProvider errorProvider1;
[Browsable(false)]
public bool Valid
{
get
{
return m_Valid;
}
}
private bool m_Valid = true;
public ValidatedTextBox()
: base()
{
InitializeComponent();
m_OldBackColor = this.BackColor;
this.Validating += new System.ComponentModel.CancelEventHandler(ValidatedTextBox_Validating);
errorProvider1.Clear();
}
void ValidatedTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (RegularExpressionString != string.Empty)
{
Regex regex = new Regex(RegularExpressionString);
m_Valid = regex.IsMatch(Text);
SetBackColor();
if (!Valid)
{
errorProvider1.SetError(this, this.ErrorMessage);
this.Focus();
}
else
{
errorProvider1.Clear();
}
}
}
private void SetBackColor()
{
if (!Valid)
BackColor = BackColorOnFailedValidation;
else
BackColor = m_OldBackColor;
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
this.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
this.ResumeLayout(false);
}
}
you can try this i suppose.. Put all the controls you want to validate in a grouper control and call validate on all the controls inside the grouper using a foreach loop

Categories

Resources