Windows Develop Bookmark and Share   
 index > Windows Forms General > How do you initiate textbox validation from code?
 

How do you initiate textbox validation from code?

How do I programatically validate the Textbox?Sourcecode is:

thetextbox.Text = "Something...";

What is the next command to validate the assignment?

If "Something..." were input by the user via keyboard, the validation would occur when focus is lost.

But how do I make it occur in code?

Thanks!

name_Derek  Tuesday, March 25, 2008 10:34 PM

Hi,

I think I'm getting closer to your question.

1. in your program you set the property Text of your TextBox to some string "Something..."

2. you have validation logic in your DataTable

3. this logic is only triggered during the validation event (textbox.DataBindings.Add(..., DataSourceUpdateMode.OnValidation...)Wink

4. since you set the Text property in your program the Validating event isn't fired

Your question: How can you force the textbox to fire the Validating event?

Hope I'm right.

1. your link is correct.

doing something like

private void button1_Click(object sender, EventArgs e)

{

textBox3.Text = "BlaBla";

textBox3.Focus();

button1.Focus();

}

is the best way to trigger the validating event on textbox3.

2. using TextChanged event

This will work too and it will be correct if you only change the Text property by your program. If the user types in the textbox the TextChanged is fired after each character typed in (for example 1. "S" 2. "So" 3. "Som" 4. "Some" ....). I'm not sure if the validation logic can deal with that.

greetings

Philipp

Philipp Merz  Wednesday, March 26, 2008 8:45 PM
name=Derek wrote:

How do I programatically validate the Textbox?Sourcecode is:

thetextbox.Text = "Something...";

What is the next command to validate the assignment?

If "Something..." were input by the user via keyboard, the validation would occur when focus is lost.

But how do I make it occur in code?

Thanks!

You have to use MaskedTextBox class instead of TextBox. It will do all the validation for you automatically. You will have to set up the formatting properly. Nobody can help you since you did not say what data you are going to input.

AlexBB - Vista Ult64 SqlSer64 WinSer64  Wednesday, March 26, 2008 2:41 AM

Thank you, but perhaps you did not understand. I need this to occur on a textbox.

My intent is to validate the textbox so the data goes through to the bound table, which I have set to occur using

textbox.DataBindings.Add(..., DataSourceUpdateMode.OnValidation...);

I cannot use MaskedTextBox, my question was specific to TextBox. I need to keep the UpdateMode to be OnValidation, but at times I want to set textbox.Text to something and programmatically validate - understand?

How do you programmatically validate a textbox?

Thanks in advance.

name_Derek  Wednesday, March 26, 2008 2:55 AM
name=Derek wrote:

Thank you, but perhaps you did not understand. I need this to occur on a textbox.

My intent is to validate the textbox so the data goes through to the bound table, which I have set to occur using

textbox.DataBindings.Add(..., DataSourceUpdateMode.OnValidation...);

I cannot use MaskedTextBox, my question was specific to TextBox. I need to keep the UpdateMode to be OnValidation, but at times I want to set textbox.Text to something and programmatically validate - understand?

How do you programmatically validate a textbox?

Thanks in advance.

A simple question.

Set up textBox1_TextChanged event delegate (you can do it in the designer) and use Regex Class all along. You will have to write a regular expression suitable to you particular input. There are thousands of them. You will have to research, perhaps you can find one already written that you can use or you will gain understanding and write your own.

There is a forum devoted to regular expressions. It is very active. The pros there are hungry for meat to swallow. Offer them a problem and you will get an answer sometimes in less than an hour. I don't know if you need to go that far. Yoy may figure it out yourself if the task is simple.

AlexBB - Vista Ult64 SqlSer64 WinSer64  Wednesday, March 26, 2008 1:07 PM

No, overriding OnTextChanged() is not validation.

I do not think you understood what I asked, but I do appreciate your trying. Here is an answer I got from someone else:


Programmatically invoking the Validating event on Windows Forms controls

It gives me two solutions I can work with. Be well.

AlexBB wrote:

name=Derek wrote:

Thank you, but perhaps you did not understand. I need this to occur on a textbox.

My intent is to validate the textbox so the data goes through to the bound table, which I have set to occur using

textbox.DataBindings.Add(..., DataSourceUpdateMode.OnValidation...);

I cannot use MaskedTextBox, my question was specific to TextBox. I need to keep the UpdateMode to be OnValidation, but at times I want to set textbox.Text to something and programmatically validate - understand?

How do you programmatically validate a textbox?

Thanks in advance.

A simple question.

Set up textBox1_TextChanged event delegate (you can do it in the designer) and use Regex Class all along. You will have to write a regular expression suitable to you particular input. There are thousands of them. You will have to research, perhaps you can find one already written that you can use or you will gain understanding and write your own.

There is a forum devoted to regular expressions. It is very active. The pros there are hungry for meat to swallow. Offer them a problem and you will get an answer sometimes in less than an hour. I don't know if you need to go that far. Yoy may figure it out yourself if the task is simple.

name_Derek  Wednesday, March 26, 2008 2:29 PM

Hi,

It looks like you are using data binding.

Do you bind to a DataSet?

Or are you using Linq To Sql?

Or binding to another source?

I'm asking this becauseyoudo validation in this data sources.

The parameter DataSourceUpdateMode.OnValidation only defines that validation takes place if you leave the control.

regards

Philipp

Philipp Merz  Wednesday, March 26, 2008 3:48 PM
name=Derek wrote:

No, overriding OnTextChanged() is not validation.

I do not think you understood what I asked, but I do appreciate your trying. Here is an answer I got from someone else:


Programmatically invoking the Validating event on Windows Forms controls

It gives me two solutions I can work with. Be well.

AlexBB wrote:

name=Derek wrote:

Thank you, but perhaps you did not understand. I need this to occur on a textbox.

My intent is to validate the textbox so the data goes through to the bound table, which I have set to occur using

thetextbox.Text = "Something...";

What is the next command to validate the assignment?

That was your original statement. You wanted to validate INPUT!!!! not the control or datafields or any datasource. The assignment is not a TextBox. It is a string. You clearly stated that you wanted to make sure that this string confirms to a certain format. You never specified which one so people are left guessing. Perhaps you want to make sure that only alphanumerics are allowed, or only numeric charracters or the input may have to be a US ZIP code or a phone number, who knows.

The only way to validate the input in a textbox is to do it thru TextCanged event. There is no other way. From your original statement it does not follow that your textbox is bound to any datasource. It can be a part of a DataGridView control who knows. If this is the case than that link is tha way to go, if not you are still stuck with the TextChanged event.

AlexBB - Vista Ult64 SqlSer64 WinSer64  Wednesday, March 26, 2008 4:22 PM

Hi,

if you want to validate all controls on your form, you can use the ValidateChildren() method of your form. This starts valdation on every control even if the control is contained within a panel or groupbox as long as the CausesValidation is set to true on this control.

You can restrict this validation by passing a parameter to ValidateChildren(ValidationConstraints...). ValidationConstraints is enum and you find the description at

http://msdn2.microsoft.com/en-us/library/system.windows.forms.validationconstraints.aspx

regards

Philipp

Philipp Merz  Wednesday, March 26, 2008 4:50 PM

Philipp,

Yes, it's with binding so I can pusha programmaticassignment to textbox.Text through to the bound table field/column. Microsoft docs state thisoccurs based on either validation or propertychange depending on my selection when I set the binding. This works fine, I use OnValidation, but once in a whileI need to assign something to textbox.Text programmatically and I want it to work just as if a user inputted the data.

I read about ValidateChildren() - looks like I cannot specifyone control to Validate (only those that match the enumeration options (i.e. visible, selectable, enabled, etc...).

From what I have read, I either have to set focus and then lose focus programmatically, or use the other method posted in the link I posted above. Any other ideas? Thanks and be well.

Kind regards,

Derek

Philipp Merz wrote:

Hi,

if you want to validate all controls on your form, you can use the ValidateChildren() method of your form. This starts valdation on every control even if the control is contained within a panel or groupbox as long as the CausesValidation is set to true on this control.

You can restrict this validation by passing a parameter to ValidateChildren(ValidationConstraints...). ValidationConstraints is enum and you find the description at

http://msdn2.microsoft.com/en-us/library/system.windows.forms.validationconstraints.aspx

regards

Philipp

name_Derek  Wednesday, March 26, 2008 6:30 PM

Hi,

I think I'm getting closer to your question.

1. in your program you set the property Text of your TextBox to some string "Something..."

2. you have validation logic in your DataTable

3. this logic is only triggered during the validation event (textbox.DataBindings.Add(..., DataSourceUpdateMode.OnValidation...)Wink

4. since you set the Text property in your program the Validating event isn't fired

Your question: How can you force the textbox to fire the Validating event?

Hope I'm right.

1. your link is correct.

doing something like

private void button1_Click(object sender, EventArgs e)

{

textBox3.Text = "BlaBla";

textBox3.Focus();

button1.Focus();

}

is the best way to trigger the validating event on textbox3.

2. using TextChanged event

This will work too and it will be correct if you only change the Text property by your program. If the user types in the textbox the TextChanged is fired after each character typed in (for example 1. "S" 2. "So" 3. "Som" 4. "Some" ....). I'm not sure if the validation logic can deal with that.

greetings

Philipp

Philipp Merz  Wednesday, March 26, 2008 8:45 PM

Yes, spot on, (except validation logic is not in the DataTable; I just need a value to get there when I update the textbox.Text property programmatically).

Your reasons are correct as to why I cannot use OnTextChanged() event too! I wonder if

textBox3.focus();

textBox3.focus();

Would do the trick? Probably not, but I will have to try that. Thanks and be well.

Philipp Merz wrote:

Hi,

I think I'm getting closer to your question.

1. in your program you set the property Text of your TextBox to some string "Something..."

2. you have validation logic in your DataTable

3. this logic is only triggered during the validation event (textbox.DataBindings.Add(..., DataSourceUpdateMode.OnValidation...)

4. since you set the Text property in your program the Validating event isn't fired

Your question: How can you force the textbox to fire the Validating event?

Hope I'm right.

1. your link is correct.

doing something like

private void button1_Click(object sender, EventArgs e)

{

textBox3.Text = "BlaBla";

textBox3.Focus();

button1.Focus();

}

is the best way to trigger the validating event on textbox3.

2. using TextChanged event

This will work too and it will be correct if you only change the Text property by your program. If the user types in the textbox the TextChanged is fired after each character typed in (for example 1. "S" 2. "So" 3. "Som" 4. "Some" ....). I'm not sure if the validation logic can deal with that.

greetings

Philipp

name_Derek  Wednesday, March 26, 2008 9:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Treeview expansion/collapse problem on vista
• reading a setting from another application .config file
• DateTimePicker initial date
• VS 2005 Databound Grid and email hyperlink
• scroll bar steals focus
• WebBrowser freezing whole app - deadlock on load stress testing
• OpenFileDialog.RestoreDirectory no longer works after installing VS 2008
• Challenge - Key and mouse click generator
• web service overriding the url property
• Gridview Datasource