Solved

Accessing Fields of a form from within a class ?

Posted on 2003-11-12
5
340 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
Comment Utility
You must verify that the property "modifiers" of your contros isn't "private"
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now