473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to pass "this" as a parameter?

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).InvalidDa ta)
{
_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

Nov 17 '05 #1
6 2138
Vern <Ve**@discussions.microsoft.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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);
}
}
"Jon Skeet [C# MVP]" wrote:
Vern <Ve**@discussions.microsoft.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
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.

Nov 17 '05 #4
Vern <Ve**@discussions.microsoft.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Vern wrote:
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.

Nov 17 '05 #6
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.

"Bruce Wood" wrote:
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.

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike Kamermans | last post by:
I'm trying to determine where inside an XHTML's DOM a javascript function is triggered, but when I use the construction of calling a function with "this" as parameter and then checking the event...
1
by: Christoph | last post by:
newFieldElement = document.createElement( 'INPUT' ); newFieldElement.onblur = new Function( "calculatePremiumOptionTotal( this );" ); In my (dynamically generated) javascript, I've never had a...
1
by: Chris | last post by:
Morning, This is my problem, I try to pass in url parameter yop.php the PID value, computed in "select", but when I execute this script, I obtain "this.PID" in all letters in URL instead of the...
3
by: Oliver Saunders | last post by:
can someone please tell me why everytime i try and pass this to a js function i can't seem to use it for anything: <html> <head> <script language="javascript"> function a(fromlink) {...
7
by: dhnriverside | last post by:
Hi peeps I'm just following this HOW-TO from MSDN.. http://support.microsoft.com/default.aspx?scid=kb;en-us;306355 But I've got a problem. I've adding the #using System.Diagnostics; line to...
8
by: solarin | last post by:
Hi all. I'm writting a logger class to write all the debug/info/warning/error messages in a file. Every time a class needs to send any message, should send a code (int) and a message (string)....
3
by: Jason S | last post by:
I have a rough understanding of what the "this" keyword refers to in Javascript but am not very clear on it. Is there a good reference that explains it well?
3
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
3
by: arnuld | last post by:
this is from C++ Primer 4/e page 260: there is a class named Sales_item and same_isbn is a member function of that class. bool same_isbn(const sales_item &rhs) const { return isbn ==...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.