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
Mythran - 22 Aug 2007 17:37 GMT
>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,
[quoted text clipped - 7 lines]
> -
> 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
Nicholas Paldino [.NET/C# MVP] - 22 Aug 2007 17:43 GMT
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.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.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,
[quoted text clipped - 7 lines]
> -
> john
Ben Voigt [C++ MVP] - 22 Aug 2007 18:43 GMT
>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.
Sheng Jiang[MVP] - 22 Aug 2007 20:58 GMT
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.

Signature
Sheng Jiang
Microsoft MVP in VC++
> 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,
[quoted text clipped - 7 lines]
> -
> john
Willy Denoyette [MVP] - 22 Aug 2007 23:20 GMT
>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,
[quoted text clipped - 7 lines]
> -
> 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.
Willy Denoyette [MVP] - 22 Aug 2007 23:36 GMT
>>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,
[quoted text clipped - 29 lines]
>
> Willy.
Sorry, wrong thread, please ignore previous reply .
Willy.