Class/Form scope question

K

Kim

Like
http://groups.google.dk/group/micro...csharp/browse_thread/thread/1ac280aaeed78de0/
and
http://groups.google.dk/group/micro...csharp/browse_thread/thread/f7fea4d944b12bf0/
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 ?
 
M

Marc Gravell

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
 
J

Joanna Carter [TeamB]

"Kim" <[email protected]> a écrit dans le message de (e-mail address 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
 
K

Kim

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 ?
 
K

Kim

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 ?
 
J

Joanna Carter [TeamB]

"Kim" <[email protected]> a écrit dans le message de (e-mail address 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top