CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005
    Posts
    21

    help with field validation in windows forms

    I really need halp on this one.

    I am trying to display a message box for mandatory fields only. However the message box still appears for the optional fields such as phone, comments and email when I click Add Student. Can any one give me any advice on excluding the optinal fields. I have entered the piece of code that am working on for this for this section.

    private void btnAddNewStud_Click(object sender, System.EventArgs e)
    {
    foreach(System.Windows.Forms.Control aControl in this.Controls)
    {
    if(aControl is System.Windows.Forms.TextBox & aControl.Text
    == "")
    {
    MessageBox.Show("Please ensure that the First Name, Last Name, Address and Course details are entered");
    aControl.Focus();
    return;

    if(aControl is System.Windows.Forms.TextBox & aControl.Name == "txtEmail")
    {
    return;
    }

    }
    }

    END: I have tried using a 2nd 'If' statement to test when the email text box is reached and to return straight away but this doesnt seem to work.

    Any help would be great

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: help with field validation in windows forms

    I presume that each control has its own name - i.e. a name which appears as a member of your form class.

    All you need to do is to validate each one individually e.g.

    Code:
    class MyForm : Form
    {
        private Edit editUserName;
        private Edit editNotToBeValidated;
    
        private void btnAddNewStud_Click(object sender, System.EventArgs e)
        {
            bool validated = false;
            validated = ValidateUserName();
            // don't include editNotToBeValidated here
    
            // if validated is false here do whatever
        }
    
        bool ValidateUserName()
        {
            if (edtiUserName.Text.Length == 0)
            {
                return false;
            }
    
            return true;
        }
    }
    Get the idea ?

    Iterating through all the controls is a bad idea unless you have a large number of them : in which case I would strongly suggest a re-design of your form.

    Why ? Because your form will become not just difficult for users to understand but will also make it unmaintainable. In other words, in future it will become very hard to change it.

    Forms should be simple and easy to use : the fewer controls per form the better. This is from a user's point of view of course but holds very true.

    Bear in mind you're writing UI so that users can use it easily - not so that you can just code it.

    Always consider the user first with UI.

    Darwen.
    Last edited by darwen; April 23rd, 2006 at 06:40 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: help with field validation in windows forms

    You can use the Tag property of the controls to indicate whether they should be validated. For example, for control that you need validated set the Tag property to be the string "ToValidate" (you can do this in the designer).
    then, when you iterate through the controls you can check the Tag:
    Code:
    private void btnAddNewStud_Click(object sender, System.EventArgs e)
    {
        foreach(System.Windows.Forms.Control aControl in this.Controls)
        {
            if(aControl .Tag == "ToValidate") 
            {
                  //validate here
            }
    
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured