By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

How do you access a control property of a main form from a custom user control?

P: n/a
i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.

i may be making this harder than it needs to be, but seem to be
buffaloed at this point.

thanks to all....

-
john

Aug 22 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a


"forest demon" <me********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
>i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.

i may be making this harder than it needs to be, but seem to be
buffaloed at this point.

thanks to all....

-
john
1.) The control you want to access from the user control needs to be visible
to the usercontrol by making the control public or internal.
2.) In the user control's code, you would use the FindForm method (instance
member of Control) and cast the result to the main form's type. (FindForm
returns a reference to the form the control is a child of).
3.) You access the control by name/id on the main form's reference (since
it's a visible member of the main form).

HTH,
Mythran
Aug 22 '07 #2

P: n/a
John,

If the control on the main form is exposed as a public member (or
internal, if they are in the same assembly), then you can just call Parent
on the child control, and cast it to an instance of the parent form and
access the member.

If not, then you have to expose a method/property/field on the parent
form to expose the control you want.

Either that, or you have to pass this information to the control
somehow, either by setting a property, field, or calling a method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"forest demon" <me********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
>i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.

i may be making this harder than it needs to be, but seem to be
buffaloed at this point.

thanks to all....

-
john

Aug 22 '07 #3

P: n/a

"forest demon" <me********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
>i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.

i may be making this harder than it needs to be, but seem to be
buffaloed at this point.
It sounds to me as if you have inversion of control issues and overcoupling.

A custom control is concerned with its own drawing, properties, etc. A
custom control is not concerned with making the parent form function
correctly.

Declare a public event on the custom control, write a handler in the parent
form. That's the appropriate place to handle interaction between controls.
Aug 22 '07 #4

P: n/a
You can implement an interface, such as IServiceProvider in the form class.
Later in the user control you can query the interface from the parent form.

--
Sheng Jiang
Microsoft MVP in VC++
"forest demon" <me********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.

i may be making this harder than it needs to be, but seem to be
buffaloed at this point.

thanks to all....

-
john

Aug 22 '07 #5

P: n/a
"forest demon" <me********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
>i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.

i may be making this harder than it needs to be, but seem to be
buffaloed at this point.

thanks to all....

-
john

Use System.Management and read the "WindowsDirectory" property of WMI's
class Win32_OperatingSystem.
Next sample reads the "windowsdirectory" from a remote server (BOBSMachine)
....
ConnectionOptions co = new ConnectionOptions();;
co.Username = "administrator"; // user with sufficient privileges to
connect to the cimv2 namespace
co.Password = "adminPwd"; // his password
ManagementScope scope = new
ManagementScope(@"\\BOBSMachine\root\cimv2", co);
SelectQuery query =
new SelectQuery("Select windowsdirectory from
Win32_OperatingSystem");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
foreach (ManagementObject windir in searcher.Get())
Console.WriteLine("Value = {0}", windir["windowsdirectory"]);
....

Willy.

Aug 22 '07 #6

P: n/a
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:eo**************@TK2MSFTNGP03.phx.gbl...
"forest demon" <me********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
>>i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.

i may be making this harder than it needs to be, but seem to be
buffaloed at this point.

thanks to all....

-
john


Use System.Management and read the "WindowsDirectory" property of WMI's
class Win32_OperatingSystem.
Next sample reads the "windowsdirectory" from a remote server
(BOBSMachine)
...
ConnectionOptions co = new ConnectionOptions();;
co.Username = "administrator"; // user with sufficient privileges to
connect to the cimv2 namespace
co.Password = "adminPwd"; // his password
ManagementScope scope = new
ManagementScope(@"\\BOBSMachine\root\cimv2", co);
SelectQuery query =
new SelectQuery("Select windowsdirectory from
Win32_OperatingSystem");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
foreach (ManagementObject windir in searcher.Get())
Console.WriteLine("Value = {0}", windir["windowsdirectory"]);
...

Willy.

Sorry, wrong thread, please ignore previous reply .

Willy.

Aug 22 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.