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
Related
I made a textbox that only accepts numbers and a "-" for negative numbers. I would like there to be an option to disable negative numbers.
In the constructor method I want to reference the allowNegatives bool that is defined properties editor and do different things depending on if it allows negatives values. I'm running into the problem that the 'allowNegatives' bool is always its default value in the constructor. If I reference it elsewhere it is the correct value.
Is there an way to get the assigned property value rather than the default value in the constructor?
public partial class ControlIntEntry : TextBox
{
private bool allowNegatives = false;
[Description("Allow negative values"), Category("Behavior")]
public bool AllowNegatives
{
get { return allowNegatives; }
set { allowNegatives = value; }
}
public ControlIntEntry()
{
// user sets AllowNegatives to true using properties editor
InitializeComponent();
Console.WriteLine(allowNegatives); // returns false
if (allowNegatives)
{
//do one thing
}
else
{
// do something else.
}
Task.Run(() => AfterConstructor()); // use for testing
}
private async Task AfterConstructor()
{
await Task.Delay(1000);
Console.WriteLine(allowNegatives); //returns true
}
}
Before you can assign a value to an instance property, the class should be instantiated, so first constructor will run and then you can assign property values.
That said, to have a better understanding of what is happening here, when you drop an instance of a control on your form at design time and set some of its properties, designer will generate a code like this:
private void InitializeComponent()
{
...
this.myControl1 = new MyControl();
...
//
// myControl1
//
this.myControl1.Location = new System.Drawing.Point(0, 0);
this.myControl1.Name = "myControl1";
this.myControl1.Size = new System.Drawing.Size(100, 22);
this.myControl1.MyProperty = true;
...
}
I believe it's now clear that what is happening here. You see first the constructor of your control will run, then later property values will be set.
To use property values to configure your object can put the logic inside the setter of the property:
private bool myProperty = false;
public bool MyProperty
{
get { return myProperty;}
set
{
myProperty = value;
// some logic here.
}
}
It's the most common scenario.
Another option is delaying the initializations to some time later, for example when the control handle is created by overriding OnHandleCreated or another suitable time.
// This is just an example, the event may not be a good one for your requirement
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// some logic here
}
Another option for complex initialization scenarios which may involve multiple properties, you can implement ISupportInitialize and put the logic inside EndInit:
public class MyControl : TextBox, ISupportInitialize
{
public void BeginInit()
{
}
public void EndInit()
{
// some logic here
}
}
Then when you drop an instance of the control on the form, this code will be generated in addition to the common code that I showed at beginning of this answer:
...
((System.ComponentModel.ISupportInitialize)(this.myControl1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
...
(I expect it's obvious now, that) All above options will run after running the constructor.
Putting that code in the setter worked
public partial class ControlIntEntry : TextBox
{
private bool allowNegatives = false;
[Description("Allow negative values"), Category("Behavior")]
public bool AllowNegatives
{
get { return allowNegatives; }
set
{
allowNegatives = value;
if (allowNegatives)
this.KeyPress += KeyPress_AllowNegatives;
else
this.KeyPress += KeyPress_PositiveOnly;
}
}
public ControlIntEntry()
{
InitializeComponent();
}
private void KeyPress_PositiveOnly(object sender, KeyPressEventArgs e)
{
Char newChar = e.KeyChar;
if (!Char.IsDigit(newChar) && newChar != 8)
{
e.Handled = true;
}
}
private void KeyPress_AllowNegatives(object sender, KeyPressEventArgs e)
{
Char newChar = e.KeyChar;
int cursorIndex = this.SelectionStart;
if (cursorIndex == 0)
{
if (!Char.IsDigit(newChar) && newChar != 8 && newChar != 45)
{
e.Handled = true;
}
}
else
{
if (!Char.IsDigit(newChar) && newChar != 8)
{
e.Handled = true;
}
}
}
}
Today I got a problem in my development.
I have a Windows Form like this :
I need to enable the button "Appliquer" when the content of one of my textbox change.
I know that I can put the KeyPress event on each textbox and enable my button with that. In this window it can be easy to do that because there is only 10 textbox but I have an other window with more of 100 textbox and I think there is a better solution.
I tried to put the Keydown event directly in my windows form but it doesn't work.
So my question is, how can I do this. If someone have an idea ?
Thank you in advance !
Thomas
Since you already have 100+ textboxes in your form. I am assuming performance is not an issue for you.
In your form constructor, call this method. It will attach the event to all the textbox controls present in your form & inside sub controls such as groupbox, panel etc. (if you require)
There could be better ways of iteration..
public Form1()//your constructor
{
InitializeComponent();
AttachEvent(this);
}
void AttachEvent(Control CTrl)
{
foreach (Control c in CTrl.Controls)
{
if (c is TextBox)
{
c.TextChanged += new EventHandler(c_TextChanged);
continue;
}
if (c.HasChildren)
{
AttachEvent(c);
}
}
}
void c_TextChanged(object sender, EventArgs e)
{
//Your Code here btnGo.Enabled = !btnGo.Enabled;
}
What you can do is to extend TextBox make a field ( accessible from the designer ) to bind that TextBox into some other control.
public class MeTextBox
: TextBox
{
public override string Text
{
get { return base.Text; }
set
{
if ( m_DependantControl != null )
{
m_DependantControl.Enabled = !string.IsNullOrWhiteSpace(value);
}
base.Text = value;
}
}
Control m_DependantControl;
[Browsable(true)]
public Control DependantControl
{
get { return m_DependantControl; }
set { m_DependantControl = value; }
}
}
Now you can use MeTextBox as a regular TextBox. And if you want to make it control Enabled flag of some other Control you can just specify DependantControl property which will be accessible in the designer.
Fitting this into your example (code):
// assume you have a Button named btnConfirm
// and want to enable this button only when your `TextBox` has some text
MeTextBox mtb = new MeTextBox();
mtb.DependantControl = btnConfirm;
And if you do not want to make it in the code you can use designer directly.
To make it other way around ( one button dependant on many text boxes ) you can extend Button object :
public class MeButton
: Button
{
List<TextBox> m_DependantOn = new List<Control>();
[Browsable(true)]
public List<TextBox> DependantOn
{
get { return m_DependantOn; }
set { RemoveEvents(); m_DependantOn = value; AssignEvents(); }
}
void RemoveEvents()
{
foreach(TextBox ctrl in m_DependantOn)
ctrl.TextChanged -= WhenTextChanged;
}
void AssignEvents()
{
foreach(TextBox.ctrl in m_DependantOn)
ctrl.TextChanged += WhenTextChanged;
}
void WhenTextChanged(object sender, TextChangedEventArgs e)
{
this.Enabled = true;
}
}
Ok so I am trying to pass a boolean from my Login form to my Home form, normally this would be fine for me and I would just use a property. However I thought I could use a similar method this time but I am implementing the singleton factory on the forms.
Here is the Login code relevant to this:
The AdminAccess property gets set fine and I have checked the value is correct.
private bool adminAccess;
public bool AdminAccess
{
get { return adminAccess; }
private set { adminAccess = value; }
}
private void btnLogin_Click(object sender, EventArgs e)
{
//Some Code Does Stuff
OpenHome();
}
private void OpenHome()
{
HomeForm CreateHomeForm = HomeForm.HomeUI;
CreateHomeForm.StartupHome = this;
//Trying to set the property.
CreateHomeForm.AdminPermissions= this.AdminAccess;
CreateHomeForm.Show();
this.Hide();
}
Here is the relevant code from the Home form:
public HomeForm()
{
InitializeComponent();
//just to check what is in the property quickly
textBox1.Text = AdminPermissions.ToString();
}
private bool adminPermissions;
public bool AdminPermissions
{
private get { return adminPermissions; }
set { adminPermissions = value; }
}
public Form StartupHome
{
set;
get;
}
private static HomeForm homeUI;
public static HomeForm HomeUI
{
get
{
if (homeUI == null || homeUI.IsDisposed)
{
homeUI = new HomeForm();
}
return homeUI;
}
}
The value gets reset when the HomeUI if loop runs as a new instance of the form is created. I can't seem to think how to modify this to get a working solution. As you can tell I am fairly amateur so I'm just looking for a quick and clean solution to this :)
Thank you very much for your time in advance!
You assign the value in the constructor, BEFORE the AdminPermissions property is actually set. Change your code like this
public class HomeForm
{
public HomeForm()
{
InitializeComponent();
}
private bool adminPermissions;
public bool AdminPermissions
{
get { return adminPermissions; }
set {
adminPermissions = value;
textBox1.Text = value.ToString();
}
}
...
}
Try setting the textBox1.Text value in one of the Form events. Try Loaded first, then Activated. You're resetting it to false every time in your constructor!
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.
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.