473,386 Members | 1,706 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.

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 frmGenericInputDialog();
frm.txtSkaterName.Text = item.SubItems[0].Text;
frm.txtCurrentPlacement.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 cboCurrentPlacement;
public System.Windows.Forms.TextBox txtCurrentPlacement;

So why can't I access these controls?

Nov 15 '05 #1
4 12069
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 frmGenericInputDialog();
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*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.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 frmGenericInputDialog();
frm.txtSkaterName.Text = item.SubItems[0].Text;
frm.txtCurrentPlacement.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 cboCurrentPlacement;
public System.Windows.Forms.TextBox txtCurrentPlacement;

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*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.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 frmGenericInputDialog();

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 frmGenericInputDialog). Now, if your variable "frm" points to anything
other than an instance of frmGenericInputDialog, you know that this line:

frm.txtSkaterName.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:

frmGenericInputDialog frm = new frmGenericInputDialog();

Now "frm" can only reference instances of frmGenericInputDialog or
instances that inherit frmGenericInputDialog. Thus any operation that is
valid for frmGenericInputDialog 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
frmGenericInputDialog. You do this with casting. You can read about casting
in the docs, but it can look like this:

Form frm = new frmGenericInputDialog();
...
(frmGenericInputDialog)frm.txtSkaterName.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
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...
2
by: authorking | last post by:
How to access a control in another form and how to make a form become a MDI container.
1
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...
1
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...
0
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...
2
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...
7
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...
2
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...
5
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...
7
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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...

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.