Solved

Accessing Fields of a form from within a class ?

Posted on 2003-11-12
5
347 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 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