PC Review


Reply
Thread Tools Rate Thread

Class/Form scope question

 
 
Kim
Guest
Posts: n/a
 
      22nd Jun 2006
Like
http://groups.google.dk/group/micros...280aaeed78de0/
and
http://groups.google.dk/group/micros...ea4d944b12bf0/
am I trying to access something in a form in one class from another
class. And without luck

In my case I tried to change the Text of a Label. Here is my problem:

I'm using Microsoft Visual Studio 2005 Professional Edition.
I have 2 files (Form1.cs and Stuff.cs) with 1 class each and they are
in the same namespace. Form1.cs has a form with some elements, among
these, a label (Label1) and a button (Button1). I made an event so that
when I click on Button1 a fuction in Stuff.cs is called/run.
When the function in Stuff.cs is done, I want to signal this on the
form window by changing the Label1.Text.

I have tried making Label1 public (by default its private). See below,
note that this is inside Form1.cs.
public System.Windows.Forms.Label Label1

When I tried to change the text, from Stuff.cs, like this
Form1.Label1.Text= "NewText";
I got a compile error, "An object reference is required for the
nonstatic field, method, or property".

My question is then: How can I change something on an element in a form
in another class but while in the same namespace ?

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
The compile error says it all:

Form1 is a type declaration, not an instance; as far as the compiler is
concerned you could have 400 instances of Form1 all side-by-side : so which
one should it update? To do this using direct access, you *must* have a
reference between the forms. It really comes down to how you are creating
the forms, and how they behave; if you create them together and they both
sit side by side, then you might have something like:

Form1 form1 = new Form1();
Form2 form2 = new Form2();
form2.SomeProperty = form1;
form1.Show();
form2.Show();

where SomeProperty is defined something like:
public class Form2 : Form {
// ...
private Form1 _someField;
public Form1 SomeProperty {get {return _someField;} set {_someField =
value;}}
}

This now allows any instance of Form2 to have a handle to the corresponding
Form1 instance.

In a simple scenario, you could proably get away with using some static
property, but I don't recommend it.

Also : it is bad practive to make the Label (or whatever) public; at a push,
Internal maybe, but it would be better to encapsulate it

public class Form1 : Form {
// ...
public string MessageCaption {
get {return Label1.Text;} set {Label1.Text = value;}
}
}

Now a Form2 instance can set SomeProperty.MessageCaption = "Frodo";

Finally, you may want to consider using an eventing mechanism to communicate
between the forms; a little bit more involved than direct property access,
but (for more complex scenarios) far more extensible.

Marc


 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      22nd Jun 2006
"Kim" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

| I'm using Microsoft Visual Studio 2005 Professional Edition.
| I have 2 files (Form1.cs and Stuff.cs) with 1 class each and they are
| in the same namespace. Form1.cs has a form with some elements, among
| these, a label (Label1) and a button (Button1). I made an event so that
| when I click on Button1 a fuction in Stuff.cs is called/run.
| When the function in Stuff.cs is done, I want to signal this on the
| form window by changing the Label1.Text.

Create a new public event on the form that can be caught in the outside
class.

Handle the button event in a handler on the form and call your public event
from that handler.

After the the public event returns, set the label in the end of the button
handler on the form.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Kim
Guest
Posts: n/a
 
      22nd Jun 2006
Thanks for the quick replies.

Maybe I wasn't clear about, but when I run the program file, my
constructor opens the form window and it's in this window Label1 and
Button1 are.
Creating a new instance of Form1 in Stuff.cs
Form1 somename = new Form1();
is not what I want, as this creates a new window and leaves the first
window unchanged.
The only way the function (lets call it FuncA) can be called is when
the Button1_Click event is trigger in the form window.
The whole idea is that the form window can call functions from Stuff.cs
and some of those functions should give back responses so it can be
noted that the function has been completed.
I can only see one other option to solve my problem, and thats to make
the functions return error_values, which then I must handle in the
event in Form1.cs.

As you might have noticed Im fairly new into C#, so please give
examples.

Marc: As you said, "It really comes down to how you are creating the
forms, and how they behave", I hope this is more clear now what I do
and want.

Joanna: Think I get the idea, but I under what you mean with "Create a
new public event on the form that can be caught in the outside class".
Could elaborate it ?

 
Reply With Quote
 
Kim
Guest
Posts: n/a
 
      22nd Jun 2006
Thanks for the quick replies.

Maybe I wasn't clear about, but when I run the program file, my
constructor opens the form window and it's in this window Label1 and
Button1 are.
Creating a new instance of Form1 in Stuff.cs
Form1 somename = new Form1();
is not what I want, as this creates a new window and leaves the first
window unchanged.
The only way the function (lets call it FuncA) can be called is when
the Button1_Click event is trigger in the form window.
The whole idea is that the form window can call functions from Stuff.cs
and some of those functions should give back responses so it can be
noted that the function has been completed.
I can only see one other option to solve my problem, and thats to make
the functions return error_values, which then I must handle in the
event in Form1.cs.

As you might have noticed Im fairly new into C#, so please give
examples.

Marc: As you said, "It really comes down to how you are creating the
forms, and how they behave", I hope this is more clear now what I do
and want.

Joanna: Think I get the idea, but I under what you mean with "Create a
new public event on the form that can be caught in the outside class".
Could elaborate it ?

 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      22nd Jun 2006
"Kim" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

| Joanna: Think I get the idea, but I under what you mean with "Create a
| new public event on the form that can be caught in the outside class".
| Could elaborate it ?

public partial class Form1 : Form
{
...

#region public event

private EventHandler button1Clicked;

public event EventHandler Button1Clicked
{
add { button1Clicked += value; }
remove { button1Clicked -= value; }
}

#endregion

private void button1_Click(object sender, EventArgs args)
{
if (button1Clicked != null)
{
button1Clicked(sender, args);
label1.Text = "some value";
}
}
}

public class Stuff
{
private void HandleForm1Button1Click(object sender, EventArgs args)
{
// do something
}

public void ShowForm()
{
Form1 test = new Form1();
test.Button1Clicked += HandleForm1Button1Click;
test.Show;
}
}

But better still would be to pass an instance of Stuff to the constructor of
the form, then your event handlers can work directly on the object.

public partial class Form1 : Form
{
...

private Stuff stuff;

public Form1(Stuff stuff)
{
this.stuff = stuff;
}

private void button1_Click(object sender, EventArgs args)
{
stuff.DoSomething();
label1.Text = "some value";
}
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class Scope JimS Microsoft Access VBA Modules 18 18th Jul 2009 08:59 PM
Class scope =?Utf-8?B?TWljaGFlbA==?= Microsoft C# .NET 2 19th Jul 2006 05:15 PM
nested class events and scope question Carmella Microsoft VB .NET 3 24th Mar 2005 11:26 PM
ADSI returning groups in Global scope and Domain local scope instead of Universal scope Maziar Aflatoun Microsoft C# .NET 1 21st Apr 2004 04:08 PM
Class's Scope JJ Microsoft C# .NET 2 2nd Feb 2004 07:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:17 AM.