2

I have this MainForm class:

namespace homework_001{

public partial class MainForm : Form
{
    public MainForm()
    {InitializeComponent();}

    public string Change
    {
        get{ return label.Text;}
        set{ label.Text = value;}
    }

     void ButtonClick(object sender, EventArgs e)
    {
        Test a = new Test();
        a.changer();


    }
}}

And I have this class:

namespace homework_001{

public class Test
{       
    private MainForm form = new MainForm ();
    public void changer(){
    form.Change = "qqqqq!";
    }
}}

Desired workflow is to change the label/text on button press. It compiles, but nothing happens after I press the button.

What might be the issue?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
prehisto
  • 33
  • 1
  • 3
  • Because you created new `MainForm`, what you need is the very same `MainForm` where you clicked the button. So pass the instance to `Test` class through constructor parameter, and use the same. – Sriram Sakthivel Nov 11 '14 at 10:08
  • Though this is a homework, i really don't see any benefit it provides rising developers. This doesn't even comply with Single Responsibility concept – DevEstacion Nov 11 '14 at 10:11
  • @RonaldEstacion no, this namespace just left from another project, I just changed the form for testing purposes instead of creating a new solution. – prehisto Nov 11 '14 at 10:24
  • Possible duplicate of [Access form component from another class](http://stackoverflow.com/questions/6803970/access-form-component-from-another-class) – Ash Jul 21 '16 at 02:57

4 Answers4

4

What is happening is that the form you are showing is not the same as the one inside the class Test.

To make things work you should pass the form to the class Test in this way:

public class Test
{       
    private MainForm form;
    public Test(MainForm f)
    {
        this.form=f;
    }
    public void changer(){
    form.Change = "qqqqq!";
    }
}}

and in your main form you do this:

public partial class MainForm : Form
{
    public MainForm()
    {InitializeComponent();}

    public string Change
    {
        get{ return label.Text;}
        set{ label.Text = value;}
    }

     void ButtonClick(object sender, EventArgs e)
    {
        Test a = new Test(this);
        a.changer();


    }
}}
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Monah
  • 6,714
  • 6
  • 22
  • 52
1

You're creating another MainForm object, different from the one that's actually displayed on the screen. To work on the same object you need to pass it to the Test class, like this:

Test a = new Test(this);   // "this" is the MainForm object you want to work with
a.changer();

This will give Test class a reference to your MainForm object.

Now, you need to create a constructor in Test with one argument, and store the received reference to your form object in Test's private MainForm form field. You shouldn't have any trouble doing that.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
1

done create a new object of the main form.. Instead. make the string Change public and static and change it from test class like Mainform.Change="some text";

-1

change you code below:

 void ButtonClick(object sender, EventArgs e)
    {
        Test a = new Test();
        a.changer(ref this);


    }

you pass the form object to function by refrence and changed that

 public class Test
    {       
       public void changer(ref MainForm form){
        form.Change = "qqqqq!";
        }
    }

refer this link Passing Reference-Type Parameters (C# Programming Guide)

Asieh hojatoleslami
  • 3,240
  • 7
  • 31
  • 45