-1

I've been trying to make multiform programs for quite some time now, but I run into the same 2 problems again and again:

  1. Form2 (Or however you call it) treats already set variables as null
  2. Form1 doesn't accept, when Form2 changes a variable from Form1

It's been so annoying lately, that I've decided to ask here, what I may (or may not) be doing wrong. Here's my quick reproductuion - A program, where you can set a given value using another form:

valueForm

public partial class valueForm : Form
{
    public int value;
    private changeValueForm change;
    public valueForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        value = 2;
        valueLabel.Text = value.ToString();
        change = new changeValueForm();
    }

    private void change_Click(object sender, EventArgs e)
    {
        change.Show();
    }

    private void updatin_Tick(object sender, EventArgs e)
    {
        valueLabel.Text = change.thisValue.ToString();
    }
}

changeValueForm

public partial class changeValueForm : Form
{
    private valueForm valueForm;
    public int thisValue;
    private string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    public changeValueForm()
    {
        InitializeComponent();
    }

    private void changeValueForm_Load(object sender, EventArgs e)
    {
        valueForm = new valueForm();
        thisValue = valueForm.value;
        valueBox.Text = thisValue.ToString();
    }

    private void closeButton_Click(object sender, EventArgs e)
    {
        if (digits.Contains(valueBox.Text.ToString()))
        {
            thisValue = Int32.Parse(valueBox.Text);
            valueForm.value = thisValue;
        }
    }
}

So, summa summarum, what is wrong with this code?

P.S. How to pass values between forms in c# windows application? Might have hepled with the 1. problem, but valueForm still doesn't accept changes made in changeValueForm - It just stays as it was.

Community
  • 1
  • 1
Atom
  • 231
  • 1
  • 2
  • 6
  • 1
    Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Apr 03 '15 at 11:22
  • 2
    possible duplicate of [How to pass values between forms in c# windows application?](http://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application) – Orace Apr 03 '15 at 11:24
  • Yes yes, I know. I just couldn't come up with any better tags! – Atom Apr 03 '15 at 11:24

2 Answers2

0

Instead of creating new instance of valueForm here:

private void changeValueForm_Load(object sender, EventArgs e)
{
    valueForm = new valueForm();

You should pass to your changeValueForm instance reference to your existed valueForm and use it.

For example this can be done when you're creating your changeValueForm, like change = new changeValueForm(this);.

In this case you also need to and changeValueForm constructor to

public changeValueForm(valueForm f)
{
    InitializeComponent();

    valueForm = f;
}

Also it it unclear from the code you've posted - when it it suposed to call updatin_Tick method and who should call it? I can guess from the method name - it should be some timer - but there is not any timer declared in your code.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • Put a parameter in a control constructor is not trivial: http://stackoverflow.com/questions/1784303/usercontrol-constructor-with-parameters-in-c-sharp especially the Visual Studio Windows Forms Designer will not handle it. – Orace Apr 03 '15 at 11:30
  • @Orace You have posted reference to post dated more than 5 years. Modern versions of Visual Studio (for example, VS2012) perfectly handles situation when you have parameterized constructor and have not parameterless one. – Andrey Korneyev Apr 03 '15 at 11:36
  • good to know. Also I work with VS2010. And it's not dead: https://support.microsoft.com/fr-fr/lifecycle/search/default.aspx?alpha=Visual%20Studio%202010&Filter=FilterNO – Orace Apr 03 '15 at 12:13
0

It is a common situation. In your changeValueForm you create a new instance of the valueForm. This new instance has nothing in common (a part from being the same class) with the initial form. It is a totally different form. So, when you set properties to the instance created inside the changeValueForm you are setting the properties of a form that is not visible. (Just try to call Show() on that instance).

The easiest way to fix the problem is passing the instance of the initial valueForm to the changeValueForm created by your code. Usually this is done in the constructor, but could also passed using a public custom property of the valueChangeForm class.

USING THE CONSTRUCTOR

public partial class changeValueForm : Form
{
    private valueForm valueForm;
    ....

    public changeValueForm(valueForm currentInstance)
    {
        InitializeComponent();
        valueForm = currentInstance;
    }

Of course when you build your changeValueForm you call

public partial class valueForm : Form
{
    public int value;
    private changeValueForm change;
    public valueForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        value = 2;
        valueLabel.Text = value.ToString();

        // pass this instance to the changeValueForm
        change = new changeValueForm(this);
    }

USING A PROPERTY

public partial class changeValueForm : Form
{
    private valueForm valueForm;

    public changeValueForm ClientCaller 
    { get { return valueForm}; 
      set { valueForm = value};
    }....


public partial class valueForm : Form
{
    ...
    private void Form1_Load(object sender, EventArgs e)
    {
        ....
        change = new changeValueForm();
        change.ClientCaller = this;
    }
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Put a parameter in a control constructor is not trivial: http://stackoverflow.com/questions/1784303/usercontrol-constructor-with-parameters-in-c-sharp especially the Visual Studio Windows Forms Designer will not handle it. – Orace Apr 03 '15 at 11:28
  • There is no problem in passing a parameter to a form constructor. The article linked is relative to UserControls that requires a parameterless constructor to be used/dropped in a designer surface – Steve Apr 03 '15 at 12:38