Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Custom Validation with Binding Navigator

$
0
0

Hello all,

I've been trying to implement the binding navigator custom validation as shown in this thread and given by Yoni:

http://social.msdn.microsoft.com/Forums/windows/en-US/ce2a837c-35b7-4d6c-b1c9-b109390830b9/best-way-to-do-row-validation-with-bindingsourcebindingnavigator?forum=winformsdatacontrols

But I've come across a problem. It works for validating the code and preventing the user from going on to the next record if e.Cancel is set to true. But if the record is valid and e.Cancel is set to false, the program will not continue the action. Instead it goes into a loop of reading the overridden WndProc() code where m.Msg != 0x201.

Below is the related code:

//class created by Yoni.
public class ValidatingBindingNavigator : BindingNavigator
    {
        public ValidatingBindingNavigator(IContainer container)
            : base(container)
        {
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x201) //WM_LBUTTONDOWN  
            {
                CancelEventArgs args = new CancelEventArgs();
                OnValidating(args);
                if (args.Cancel)
                {
                    return;
                }
            }
            base.WndProc(ref m);
        }
//OnValidating mod suggested later in thread by BonnieB
//I saw no noticeable difference in the outcome when included
        protected override void OnValidating(CancelEventArgs e)
        {
            if (this.Parent is Form)
                e.Cancel = !((Form)this.Parent).ValidateChildren();
            base.OnValidating(e);
        } 
    }
//My code for validation
private void bindingNavigatorStudentGI_Validating(object sender, CancelEventArgs e)
        {
            if (IsStudentDirty() == true)//if changes, validate
            {
                if (ValidateStudentForm() == false)//if not valid cancel action
                    e.Cancel = true;
                else e.Cancel = false;//else continue action
            }
            else
                e.Cancel = false;//if no changes, don't validate, continue action
        }

private bool IsStudentDirty()
        {
            bool dirty = false;

            ...//if changes have been made to the record
                dirty = true;
				
            return dirty;
        }

private bool ValidateStudentForm()
        {
            bool empty = false;
            bool invalid = false;

            ...//if there is an empty required field
				empty = true;
	...//if there is a field in an incorrect format
				invalid = true;

            if (empty == true && invalid == true)
            {
                MessageBox.Show("...inform user of empty and invalid fields");
                return false;
            }
            else
            {
                if (empty == true)
                {
                    MessageBox.Show("...inform user of empty field");
                    return false;
                }
                else if (invalid == true)
                {
                    MessageBox.Show("...inform user of invalid field");
                    return false;
                }
                else return true;
            }
        }

I have tried the code both with e.Cancel = false and with those lines omitted. It doesn't seem to make a difference either way. The button click actions are not 'canceled' if valid but they are never performed with the current code. Help would be greatly appreciated.

I am hoping that I will not have to change the way it validates because I like the way it currently cancels. I just need it to perform the action if it's not canceled. Thanks in advance for your help.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>