Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, December 31, 2011 2:08 PM
hi,
i am using C#.net winforms. i have a form which contain a lot of controls including save button. i have added validate event for all control in my form. now i want when user click in save button then validate event should fire of all controls. i can do this by calling all validate method one by one on save click method but i don't want to do this. so how can i execute all validate method of form dynamically . please help me on this.
Prem Shah
All replies (6)
Saturday, December 31, 2011 4:00 PM ✅Answered | 2 votes
Hi Sir,
There is a way to solve this, please follow the following steps :
1. Write whatever validation you require for each textbox or controls in their validating event you need to get validated and on submit click button just call the ValidateChildren() method.
2. Set the CausesValidation property to true for each control or textbox you need to get validated on the click of the submit button.
3. Set the forms AutoValidate property to disable that will prevent the controls (textboxes) to be validated on the losing of focus and it will be validated on the click of submit button.
4. For the full code to understand better visit http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.autovalidate.aspx#Y579 and see the examples section in the link which is very easy to understand.
Thanks
Rehan Bharucha - The Tech Robot
MCTS, MCITP, MCPD, MCT
Monday, January 2, 2012 7:59 AM ✅Answered | 2 votes
Hi Prem,
Its easy,
Suppose you have two textboxes say textbox1 and textbox2 and a button
on click event of the button you validated all controls as i said in my previous post and now textbox2 is null so you want the focus to shift to textbox1 so for each control you must have writtent the validation in their validating event right!
refer my post above in the MSDN link
in the validating event, if the textbox1.text fails validation then we do e.cancel=true along with that, write textbox1.focus() use the focus() method to bring the cursor to the invalid control.
Dont forget to mark as answers if they both solve your purpose....
Editing : Just follow the entire steps laid down in first post and this second post it will hundred percent work. Tested on a dummy project by me.
The interface i designed is as below
The code is given below
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
private void Form1_Load(System.Object sender, System.EventArgs e)
{
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
if (ValidateChildren()) {
MessageBox.Show("Validation succeeded!");
} else {
MessageBox.Show("Validation failed.");
}
}
private void TextBox1_Validating(System.Object sender, System.ComponentModel.CancelEventArgs e)
{
if (TextBox1.Text.Length() == 0) {
TextBox1.Focus();
e.Cancel = true;
} else {
e.Cancel = false;
}
}
private void TextBox2_Validating(System.Object sender, System.ComponentModel.CancelEventArgs e)
{
if (TextBox2.Text.Length() == 0) {
TextBox2.Focus();
e.Cancel = true;
} else {
e.Cancel = false;
}
}
private void ComboBox1_Validating(System.Object sender, System.ComponentModel.CancelEventArgs e)
{
if (ComboBox1.Text.Length() == 0) {
ComboBox1.Focus();
e.Cancel = true;
} else {
e.Cancel = false;
}
}
public Form1()
{
Load += Form1_Load;
}
}
Textbox1, Textbox2, Combobox1 CausesValidation property = True
Form1 AutoValidate Property = Disable
I hope now its clear to you how to code on submit button for all controls on form
Thanks
Rehan Bharucha - The Tech Robot
MCTS, MCITP, MCPD, MCT
Monday, January 2, 2012 7:39 AM
Hi Rehan,
thanks for Reply. how can i set focus on Invalidate control , when i am clicking on save button. if multiple controls is invalid then cursor should focus on first control.
Prem Shah
Wednesday, January 4, 2012 4:51 AM
Hi Rehan,
Thanks for Reply. it help me. how can i use this behaviour in silverlight projects. did you have any idea on this?
Prem Shah
Wednesday, January 4, 2012 7:48 AM
Hi Prem,
For silverlight issue, you can post here: http://forums.silverlight.net/
Bob Shen [MSFT]
MSDN Community Support | Feedback to us
Wednesday, January 4, 2012 2:09 PM
Hi,
Prem, please post the issue to the link said by Bob Sir. Copy the url and paste the link in this thread. I will get you the code 100% when you post the question in that forum and paste the link here for my easy access to that link.
Thanks
Rehan Bharucha - The Tech Robot
MCTS, MCITP, MCPD, MCT