473,769 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access Form Controls From Another Class?

How can controls on a Windows Form be accessed (or referenced) from another
Class? I know how to do it from another Form. The following doesn't work
even though the Control Modifiers property is set to "Public":

frmMain.myContr ol

Thanks.

Michael
Nov 15 '05 #1
3 7546
100
Hi Michael,
What does *doesn't work* mean? Is there some compiler error message? It has
to work as long as the myControl is public. I suppose frmMain is the member
variable of the *other* class and it is initilized with the reference to the
form.
Make sure as well that the frmMain's type is the type that has myControl
field.

for example:
class MyForm: Form
{
public Control myControl = .....
}

if you have

Form frmMain = <reference to MyForm object>
and you try

frmMain.myConrt rol.....

it won't work because Form class doesn't have myControl member variable.

However, if you have

MyForm frmMain = <reference to MyForm object>

frmMain.myContr ol....

has to work.

HTH
B\rgds
100
"Michael Ramey" <ra*****@hotmai l.com> wrote in message
news:eu******** *****@TK2MSFTNG P09.phx.gbl...
How can controls on a Windows Form be accessed (or referenced) from another Class? I know how to do it from another Form. The following doesn't work
even though the Control Modifiers property is set to "Public":

frmMain.myContr ol

Thanks.

Michael

Nov 15 '05 #2
The compiler error message is:

An object reference is required for the nonstatic field, method, or
property.

The following code allowed me to access the control:

frmMain frmActive = (frmMain)frmMai n.ActiveForm;
frmActive.myCon trol...

Is there a better way?

Thanks.

Michael

"100" <10*@100.com> wrote in message
news:O2******** ******@tk2msftn gp13.phx.gbl...
Hi Michael,
What does *doesn't work* mean? Is there some compiler error message? It has to work as long as the myControl is public. I suppose frmMain is the member variable of the *other* class and it is initilized with the reference to the form.
Make sure as well that the frmMain's type is the type that has myControl
field.

for example:
class MyForm: Form
{
public Control myControl = .....
}

if you have

Form frmMain = <reference to MyForm object>
and you try

frmMain.myConrt rol.....

it won't work because Form class doesn't have myControl member variable.

However, if you have

MyForm frmMain = <reference to MyForm object>

frmMain.myContr ol....

has to work.

HTH
B\rgds
100
"Michael Ramey" <ra*****@hotmai l.com> wrote in message
news:eu******** *****@TK2MSFTNG P09.phx.gbl...
How can controls on a Windows Form be accessed (or referenced) from

another
Class? I know how to do it from another Form. The following doesn't work even though the Control Modifiers property is set to "Public":

frmMain.myContr ol

Thanks.

Michael


Nov 15 '05 #3
The confusion lies in your naming convention.

Since frmMain uses camel notation, a C# developer using MS's conventions
will naturally assume that it's a member/local variable.

Anyways, you need to pass a reference to the form to the object that you
want to access its control.

public class MyControlAccess or
{
public MyControlAccess or(frmMain mainForm)
{
mainForm.myCont rol.....
}
}

"Michael Ramey" <ra*****@hotmai l.com> wrote in message
news:uG******** ******@tk2msftn gp13.phx.gbl...
The compiler error message is:

An object reference is required for the nonstatic field, method, or
property.

The following code allowed me to access the control:

frmMain frmActive = (frmMain)frmMai n.ActiveForm;
frmActive.myCon trol...

Is there a better way?

Thanks.

Michael

"100" <10*@100.com> wrote in message
news:O2******** ******@tk2msftn gp13.phx.gbl...
Hi Michael,
What does *doesn't work* mean? Is there some compiler error message? It

has
to work as long as the myControl is public. I suppose frmMain is the

member
variable of the *other* class and it is initilized with the reference to

the
form.
Make sure as well that the frmMain's type is the type that has myControl
field.

for example:
class MyForm: Form
{
public Control myControl = .....
}

if you have

Form frmMain = <reference to MyForm object>
and you try

frmMain.myConrt rol.....

it won't work because Form class doesn't have myControl member variable.

However, if you have

MyForm frmMain = <reference to MyForm object>

frmMain.myContr ol....

has to work.

HTH
B\rgds
100
"Michael Ramey" <ra*****@hotmai l.com> wrote in message
news:eu******** *****@TK2MSFTNG P09.phx.gbl...
How can controls on a Windows Form be accessed (or referenced) from

another
Class? I know how to do it from another Form. The following doesn't

work even though the Control Modifiers property is set to "Public":

frmMain.myContr ol

Thanks.

Michael



Nov 15 '05 #4

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

Similar topics

6
4753
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
49
14354
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 application is relatively big: around 200 tables, 200 forms and sub-forms, 150 queries and 150 repports, 5GB of data (SQL Server 2000), 40 users. I'm wondering what are the disadvantages of using Access as front-end? Other that it's not...
2
3024
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2 or more user controls dynamically by adding to a placeholder defined in page_load. I've included the sample code for how we are accessing one. The user controls are not rocket science - just a few text boxes with public accessor properties. We've...
18
2987
by: Colin McGuire | last post by:
Hi - this was posted last weekend and unfortunately not resolved. The solutions that were posted almost worked but after another 5 days of working on the code everynight, I am not further ahead. If you do have any ideas I would really like to hear them. Thanks Colin - 0 - 0 - 0 - I want a glorified popup/context menu on a button that shows only when
17
5534
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables too much - that it's bad programming practice. Is there an easy way to deal with this? I would like to do things in the "Best Practices" way but at the same time I don't want to make a federal case out of it. This comes up over and over...
2
1998
by: Pj | last post by:
hi, i am new to vb.net and i am stuck at a particular point in my project. i have two nested classes (because vb.net does NOT allow multiple inheritance) ----------------------------------------- Class DrawingCanvas Inherit System.Windows.Forms.Form
5
2689
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all that's come before. To take an extreme example, suppose you have been writing Visual Basic applications for years now. If you're like many developers, you will have built up a substantial inventory of code in that time. And if you've been following...
4
4128
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a user control that is designed as below. I am creating these User Controls Dynamically in another form. They are multiple types of User Controls all with a common Interface so I can use a Factory to create them all in a generic way. The problem is that if I create a IMyInterface - I cannot access the derived UserControl Methods. I need to access the Methods derived from UserControl and also the Interface
10
3373
by: Les Desser | last post by:
In article <fcebdacd-2bd8-4d07-93a8-8b69d3452f3e@s50g2000hsb.googlegroups.com>, The Frog <Mr.Frog.to.you@googlemail.comMon, 14 Apr 2008 00:45:10 writes Not sure if I quite follow that. 1. Data encrypted by AES key 2. AES key encrypted with Asymmetric public key (?)
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.