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

  • Thread starter Thread starter forest demon
  • Start date Start date
F

forest demon

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
 
forest demon said:
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
 
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.
 
forest demon said:
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.
 
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.
 
forest demon said:
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.
 
Willy Denoyette said:
forest demon said:
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.
 

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