How to retrieve a property for a custom control.

G

Guest

How can I loop through all the controls on the form, and for certain types of
controls, access the custom properties. For example, I've created a custom
textbox for name fields. It has a property called InvalidData that I turn on
during the validating event if the data is not valid.

Here's my code that loops through all the records:
private bool AnyFieldErrors()
{
// Clear the error message fields
bool _anyErrors = false;
// Perform the validate event for each field on the screen
foreach (Control control in GetAllControls() )
{
if (control.CausesValidation == true)
{
control.Focus();

if (!this.Validate() )
{
string CName = control.Name;
string CType = control.GetType().ToString();
if (CType == "WS.Windows.Forms.NameTextBox")
{
if (control.InvalidData) // How do I refer to a custom type?
{
_anyErrors = true;
}
}

}
}
}
return _anyErrors;
}
 
T

Tim Wilson

Recursively loop through the controls on the Form and check if the type "is"
your custom control. Then cast the control to that type and access the
property.

if (ctrl is NameTextBox)
{
((NameTextBox)ctrl).InvalidData = true;
}
 
G

Guest

Thanks, I knew it must be simple.

Now for another question. If I set the focus to a control and run
this.Validate(), does it finish the logic in the validating routines before
returning control to the method that called this.Validate? It appears, then
when I try to access an error property for the control immediatly after the
call to this.Validate, it is not set on. But if I do a seperate loop through
all the control, then it is set on. Or does this.Validate just run the
validation for the previous control that just lost focus?
 
G

Guest

Or maybe I should run application.DoEvents after the this.Validate to be sure
the validating event is finished before I do the test on the next line?
 
T

Tim Wilson

Have you checked to make sure that your validation event handlers are being
called when you call Validate()?
 
G

Guest

Yes, because when the screen is displayed, it has the little red dots next to
the fields with the description that is set by the OnValidating Event handler.
 
T

Tim Wilson

It could be something simple here but since I'm not all that familiar with
the validation process in windows forms I would encourage you to post this
question in a new thread and maybe someone else can answer it more
precisely.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top