473,786 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Variables of another form

Hello, I have created a form which I intend to use a a dialog box. It has
a few controls on that form and I changed the properties of those controls
to "public" with the idea that I could access those controls from the
"calling form", but I am unable to access/see those controls. Why?

Here are portions of my code

Here I am substantiating the new form and trying to set values for the
controls on that form...

Form frm = new frmGenericInput Dialog();
frm.txtSkaterNa me.Text = item.SubItems[0].Text;
frm.txtCurrentP lacement.Text = cboFinalsFinish .SelectedIndex+ 1;
DialogResult rslt = frm.ShowDialog( );
Here are the declarations of those controls in the

public System.Windows. Forms.TextBox txtSkaterName;
public System.Windows. Forms.Label lblNewPlacement ;
public System.Windows. Forms.ComboBox cboCurrentPlace ment;
public System.Windows. Forms.TextBox txtCurrentPlace ment;

So why can't I access these controls?

Nov 15 '05 #1
4 12107
Your declaring a Form type not your actual Form derived type so the compiler
will have no knowledge of your *extra* fields:
Form frm = new frmGenericInput Dialog();
Also a couple of extra points. It would be better not to make the controls
public as you are exposing much more than is necessary and the lesser the
interface area to the class the better. You should instead create public
assessor type properties that in-turn update the controls. Also if you make
something public then you should really adhere to a Pascal naming convention
for the public members.

Regards
Lee

"Jim Heavey" <Ji*******@nosp am.com> wrote in message
news:Xn******** *************** **********@207. 46.248.16... Hello, I have created a form which I intend to use a a dialog box. It has
a few controls on that form and I changed the properties of those controls
to "public" with the idea that I could access those controls from the
"calling form", but I am unable to access/see those controls. Why?

Here are portions of my code

Here I am substantiating the new form and trying to set values for the
controls on that form...

Form frm = new frmGenericInput Dialog();
frm.txtSkaterNa me.Text = item.SubItems[0].Text;
frm.txtCurrentP lacement.Text = cboFinalsFinish .SelectedIndex+ 1;
DialogResult rslt = frm.ShowDialog( );
Here are the declarations of those controls in the

public System.Windows. Forms.TextBox txtSkaterName;
public System.Windows. Forms.Label lblNewPlacement ;
public System.Windows. Forms.ComboBox cboCurrentPlace ment;
public System.Windows. Forms.TextBox txtCurrentPlace ment;

So why can't I access these controls?

Nov 15 '05 #2
Thanks Lee for your comments....but
My form is a Class and I am creating an instance of that class, so what are
not all my public objects, contained within the form not available to me if
the form is in the same namespace and in the same project?

You indicated that I and creating a "Form Type" - but am I not creating an
instance of the object which is of that type? And should not all Public
variables/object be available to me?

Don't mean to be dense... but I don't get it...

I have passed variable values as part of the constructor, but I will still
need to access the results of the dialog box, And I am guessing that if I
declare a Public method(s) such return(s) the values I need, then all will
be fine....but why would public methods work and not public variables???
Nov 15 '05 #3
Hi Jim,

"Jim Heavey" <Ji*******@nosp am.com> wrote in message
news:Xn******** *************** **********@207. 46.248.16...
Thanks Lee for your comments....but
My form is a Class and I am creating an instance of that class, so what are not all my public objects, contained within the form not available to me if the form is in the same namespace and in the same project?

<snip>
A lengthy explanation (you've been warned). A line from your code:

Form frm = new frmGenericInput Dialog();

But you could also have coded:

Form frm = new Form();

or:

Form frm = new SomeOtherForm() ; // SomeOtherForm inherits Form

The point is, it is possible to point a variable of type Form (such as
frm, above) to any object instance that either is an instance of the Form
class or is an instance of a class that inherits from the Form class (as
does frmGenericInput Dialog). Now, if your variable "frm" points to anything
other than an instance of frmGenericInput Dialog, you know that this line:

frm.txtSkaterNa me.Text = item.SubItems[0].Text;

is just not going to work. Furthermore, there is no way for the compiler
to know at compile-time what specific type of object "frm" is going to
reference, but compiler *does* know that "frm" can only reference an object
that is a Form or inherits from Form. Therefore it makes sense that the
compiler will limit you to the operations (method calls, properties, ect.)
that are valid for a Form object only.

There are two ways around this. First, you could declare frm like this:

frmGenericInput Dialog frm = new frmGenericInput Dialog();

Now "frm" can only reference instances of frmGenericInput Dialog or
instances that inherit frmGenericInput Dialog. Thus any operation that is
valid for frmGenericInput Dialog will be valid for "frm".

The second way is to tell the compiler that even though "frm" is of type
Form, you know the object "frm" references is specifically of type
frmGenericInput Dialog. You do this with casting. You can read about casting
in the docs, but it can look like this:

Form frm = new frmGenericInput Dialog();
...
(frmGenericInpu tDialog)frm.txt SkaterName.Text =
item.SubItems[0].Text;

HTH.

Regards,
Dan
Nov 15 '05 #4
I got it...Thanks A bunch!!!!!!!!!! !!!!!
Nov 15 '05 #5

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

Similar topics

3
3651
by: TonyM | last post by:
Hi all, I have an application with a few different Windows forms. I am trying to update a statusbar panel's text that is in the main form, from another form. When I set the statusbar and the statusbar panels to public and shared in VS.net's property box it will work. However, whenever I make any modifications to the main form it will remove the shared property and I will get build errors about accessing the panels with "this.".
2
11131
by: authorking | last post by:
How to access a control in another form and how to make a form become a MDI container.
1
1836
by: tmaster | last post by:
Within a class, can I create a property that is a listview? Here's what I tried, but it doesn't seem to work: '------------ create property to give the second form access to the first form's listview: Public Class frmToDoDetail ' (this is the second form)
1
1220
by: Nathan | last post by:
Hi, I have created a class library creating a number of forms and a few public variables. I have a project that references the .dll for this class library, and in that project I need to access those public variables. For instance, the main app calls a form in the referenced library, that form changes the value of PublicVar1 and then closes. The main app now needs to know the value of PublicVar1.
0
1424
by: Geraldine Hobley | last post by:
Hello I have a problem whereby I have a treeview control on one form called projecttree and I wish to clea the nodes from this control in another form The form that contains the treeview is called projecttree and the form that I wish to clear the node from the tree, (because I'm connecting to a different database) is called frmConnections The forms are on the screen at the same time, and they are of type crownwood docking control ...
2
4881
by: baret bonden | last post by:
Trying to return a selected listbox item to another form .tried lots of ways; defining public variables and passing those as well as textboxes ..I' m able to display the chosen item on it's form in a textbox and I'm able to pass other variables to the 2nd form, but not the data I want . I should add this is a smartdeviceapplication(if that matters; not sure) Here's most of the test code .
7
2011
by: David | last post by:
Hi, I am having trouble with a form that I have loaded trying to access a procedure on the main form. The trouble seems to be that a Global Array that is declare in a Module is producing a Nullreference error when I try to call the procedure on the main form. ie. Module 1 contains
2
2358
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class CCommandMeshToSrf, eg: BOOL m_bHaveAnswer; I want to set this variable from within a dialog class which is defined in separate .h and .cpp files of course. How would one access this varibale from a method defined in my CTestModelessDialog class ?
5
2406
by: Boki | last post by:
Hi All, There are two forms, when some click happen, it fires to set form2's viable as enable. The form2 is for user to input some text and then the data need to be collected into form1's textbox. I have created a function call as public, but I still can't use it in form2.
7
1884
by: rb0135 | last post by:
I have one form that initializes a class, called Players. But, I need to access these initialized values in another form. Obiously, if I do Players player = new Players(); it creates a new instance of the Players class, but has all variables set to defaults, not the previous values when the first form was run. I have tried just Players player; the compile compiles OK, but then I get an NULL REFERENCE error when trying to access the...
0
9647
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
9492
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,...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
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
5397
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
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
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.