How to pass "this" as a parameter?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to make the following a generic method that all my forms can call to
validate all the fields on the form. So how do I pass the form object that
is represented as "this" in the following code, and how do I retrieve the
list of all controls and child controls on that form (since I don't think I
can just refer to Controls).

#region AnyFieldErrors

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();
this.Validate();
}
}

// Check the properties for the custom fields to see if has errors.
foreach (Control control in GetAllControls() )
{
if (control.CausesValidation == true)
{
control.Focus();

string CName = control.Name;
string CType = control.GetType().ToString();
switch (CType)
{
case "WS.Windows.Forms.NameTextBox":
if (((WS.Windows.Forms.NameTextBox)control).InvalidData)
{
_anyErrors = true;
}
break;

} // end switch Ctype
} // endif control.CausesValidation


} // end for each
return _anyErrors;

} // end Method


// Retrieve all controls and all child controls and etc.
// Make sure to send controls back at lowest depth first
// so that most child controls are checked for things before
// container controls, e.g. a TextBox is checked before a
// GroupBox control
Control[] GetAllControls()
{
ArrayList list = new ArrayList();
GetAllControls(Controls, list);
return (Control[])list.ToArray(typeof(Control));
}

void GetAllControls(Control.ControlCollection controls, ArrayList list)
{
foreach(Control control in controls)
{
if(control.HasChildren)
GetAllControls(control.Controls, list);
list.Add(control);
}
}

#endregion
 
Vern said:
I'd like to make the following a generic method that all my forms can call to
validate all the fields on the form. So how do I pass the form object that
is represented as "this" in the following code, and how do I retrieve the
list of all controls and child controls on that form (since I don't think I
can just refer to Controls).

You can just pass "this" with no problems, and you can indeed refer to
the Controls property of any control. (So if you passed "this" as a
parameter called form, you'd use form.Controls.)
 
What would the type of the parameter be? Would it look like the following?
private bool AnyFieldErrors(?object? form)

Then down further, would I use:
form.Validate();


And further down, would I use:

foreach (Control control in GetAllControls(form) )

And still further down, how would I change the following to populate a list
of controls:
Control[] GetAllControls()
{
ArrayList list = new ArrayList();
GetAllControls(Controls, list);
return (Control[])list.ToArray(typeof(Control));
}

void GetAllControls(Control.ControlCollection controls, ArrayList list)
{
foreach(Control control in controls)
{
if(control.HasChildren)
GetAllControls(control.Controls, list);
list.Add(control);
}
}
 
How about this:

public static bool AnyFieldErrors(Form formToCheck)
{
if (!formToCheck.Validate())
{
}
foreach (Control control in formToCheck.Controls)
{
}
}

etc.

Then you just call:

if (AnyFieldErrors(this)) ...

from within a Form class. I would make the method AnyFieldErrors method
static, as it probably doesn't need to use any fields from its
enclosing class.
 
Vern said:
What would the type of the parameter be? Would it look like the following?
private bool AnyFieldErrors(?object? form)

If you're always going to pass in a form, then make it type Form.
And further down, would I use:

foreach (Control control in GetAllControls(form) )
And still further down, how would I change the following to populate a list
of controls:
Control[] GetAllControls()
{
ArrayList list = new ArrayList();
GetAllControls(Controls, list);
return (Control[])list.ToArray(typeof(Control));
}

void GetAllControls(Control.ControlCollection controls, ArrayList list)
{
foreach(Control control in controls)
{
if(control.HasChildren)
GetAllControls(control.Controls, list);
list.Add(control);
}
}

Make the first one take a parameter of type Control called control, and
change the middle line of it to:

GetAllControls(control.Controls, list);
 
Vern said:
I'd like to make the following a generic method that all my forms can call to
validate all the fields on the form. So how do I pass the form object that
is represented as "this" in the following code

Why don't you define that method in an abstact subclass of Form and derive
all your forms from this class ? So you wouldn't need to pass a parameter at
all...
, and how do I retrieve the
list of all controls and child controls on that form (since I don't think I
can just refer to Controls).
[...snip...]

Should be pretty straightforward, then.
 
This worked great! Thanks.

I now have a custom button, whos onclick event will always perform the
validations on all the fields on the form, and will not allow it to continue
to the actual onclick event if any errors were found.
 

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

Back
Top