Solved

Accessing Fields of a form from within a class ?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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