1

How can i make a Panel.Validate() and Panel.ValidateChildren() ?

I need this, because i have a toolstrip on my panel(s). I contains 2 buttons (Save and Cancel).

Save should call Panel.Validate() and Panel.ValidateChilden().

Cancel should not call anything.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class MyPanel : Panel
    {

        public bool Validate()
        {
            //What to write in here ?
            return true;
        }

        public bool ValidateChildren()
        {
            foreach (Control c in this.Controls)
            {
                //What to write in here ?
            }
            return true;
        }
    }
}

EDIT: A little more explaining is necessary. The textboxes on the panel are validating when the user leaves the textbox. But when i click the Save-button, the user does not leave the active textbox. Therefore it is not validated, allowing him to save corrupt data. I don't want to force him to leave, (by setting the focus to another control), because he might want to continue typing in the textbox, after pressing save.

I am handling it right now by calling Form.ValidateChildren() when i click the save-button. It works, but validates ALL controls on the form. Not just the ones in my panel.

private void button1_Click(object sender, EventArgs e)
{
    if (ParentForm.ValidateChildren())
        this.Save();
    else
        MessageBox.Show("Error in validating");
}

EDIT2:

SOLVED. I just use a containercontrol instead of a Panel. It gives me what i need. (actually i didn't know this control before now)

  • Where ? I don't se any Panel.ValidateChildren() in System.Windows.Forms.Panel. [link]http://msdn.microsoft.com/en-us/library/System.Windows.Forms.Panel(v=vs.110).aspx – Ole Haahr Andersen Nov 05 '13 at 06:24
  • ValidateChildren is a method of the ContainerControl, but Panel is not derived from ContainerControl, so it cannot call this method. – Ole Haahr Andersen Nov 05 '13 at 06:31

2 Answers2

0

you will need to assign the event handler for on Save to Validate. Your looking at delegates and event handlers at this point. C# In Depth is a good in depth look at this. But to get started look on the web for delegates and eventhandler. As far as what you do when you get to the validate function point, you will have to determine what "Validate" means in your case.

garaber
  • 232
  • 3
  • 13
0
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class MyPanel : Panel
    {

        public bool Validate()
        {
            //What to write in here ?
            return true;
        }

        public bool ValidateChildren()
        {
            foreach (Control c in this.Controls)
            {
                //What to write in here ?
            }
            return true;
        }

        public void Save()
        {
            if (Validate() && ValidateChildren())
            {
                //Do something
            }
        }

        private void Save_Click(object sender, System.EventArgs e)
        {
            Save();
        }
        //something else
    }
}

Here you can see how can one attach an event to a control in WinForms. Here you can see an example click event.

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175