473,386 Members | 1,758 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,386 software developers and data experts.

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

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
6 2292


"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
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

"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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
4
by: BBFrost | last post by:
We have a data entry application written within Framework 1.0 that contains 20+ custom user controls. The controls overlay each other so that only one custom control is visible at a time. What...
11
by: John J. Hughes II | last post by:
I have a DataGridView displaying data from a DataSet. To the right of that I have a custom user control which displays one of the data set fields. The custom user control is bound to the data set...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
2
by: ahmed.maryam | last post by:
Hello Everyone, I designed a custom control that is entirely covered by a picture box. I then dragged this custom control onto a windows form application (called main) and I need to handle mouse...
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
4
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.