Solved

Accessing Fields of a form from within a class ?

Posted on 2003-11-12
5
348 Views
Last Modified: 2010-04-16
Hi,

   How do you change the properties of a form from a class ?

   I tried form1.fieldnname.Text = "Message"; but I get an error. Also when I type
   form1 and the "." I cannot see the fields (labels and textbox) that are on the form.



Thanks


Robert
0
Comment
Question by:rogaut1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:DaniPro
ID: 9732009
You must verify that the property "modifiers" of your contros isn't "private"
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9732210
As DaniPro said............

In your form class you should have something similar to...

public class MyWindow : System.Windows.Forms.Form
{
    // Code
    private System.Windows.Forms.Label lblTitleDefaults;
}

if you use private, then you won't be able to call it from another class......

You must use internal (if you want to access it from all classes in the same assembly) or public( if you want to access it from any class)

Then if you have
public class MyWindow : System.Windows.Forms.Form
{
    // Code
    private System.Windows.Forms.Label lblTitleDefaults1;
    internal System.Windows.Forms.Label lblTitleDefaults2;
    public System.Windows.Forms.Label lblTitleDefaults3;
}

If you are calling from another class in the same project

You won't be able to access lblTitleDefaults1.
You will be able to access lblTitleDefaults2.
You will be able to access lblTitleDefaults3.

If you are calling from another class outside the project where the form is placed

You won't be able to access lblTitleDefaults1.
You won't be able to access lblTitleDefaults2.
You will be able to access lblTitleDefaults3.

Hope I've been clear enough

Tincho

0
 
LVL 2

Author Comment

by:rogaut1
ID: 9732265
Hi,

I tried internal and I still cannot see the field. The only way I see it is if I declare it
"static public".



Thanks



Robert
0
 
LVL 6

Expert Comment

by:DaniPro
ID: 9732316
If you declare a component like internal it will be visible from the component of the same assembly but not from the componet of other assembly.
If you declare a component like static you can access it directly, otherwise you must instantiate the class for access it.
0
 
LVL 9

Accepted Solution

by:
tinchos earned 125 total points
ID: 9732341
You'll do like this

regarding internal and public I guess that I explained that in the previous post

regarding static the difference is the following

if you have class

public class MyWindow : System.Windows.Forms.Form
{
    // Code
    private System.Windows.Forms.Label lblTitleDefaults1;
    internal System.Windows.Forms.Label lblTitleDefaults2;
    public System.Windows.Forms.Label lblTitleDefaults3;
}


if you declare them the static you would call them

MyWindow.lblTitleDefaults1

if not, you would create an objetc of that type and call them from there

MyWindow myObj;

myObj.lblTitleDefaults1

But keep in mind the difference between static and non static methods and members.

Tincho
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question