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)