[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Accessing Fields of a form from within a class ?

Posted on 2003-11-12
5
Medium Priority
?
356 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 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 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