简体   繁体   中英

Update variable in Form1 from value in Form2

I have 2 form, first name HomeForm and the second name cfgForm

I have variables, name strCNF in HomeForm, value of strCNF from variable CNF in cfgForm.

Here the code from HomeForm :

private void cnfRulesMenu_Click(object sender, EventArgs e)
{
    try
    {
        cfgForm cfgForm = new cfgForm(this);
        cfgForm.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString(), "Error");
    }
}

// other parts....
public cfgForm cfg;
string strCNF = "";
strCNF = ((cfgForm)this.cfg).CNF;

Then, in another form that is cfgForm, here the code :

public string CNF = "S -> NP VP" + System.Environment.NewLine +
                        "NP -> DT NN | QT NN | NP PP | NP RC | n | p" + System.Environment.NewLine +
                        "DT -> d" + System.Environment.NewLine +
                        "NN -> JJ NN | n" + System.Environment.NewLine +
                        "JJ -> JJ JJ | j" + System.Environment.NewLine +
                        "QT -> q" + System.Environment.NewLine +
                        "PP -> PT NP | GG PP" + System.Environment.NewLine +
                        "PT -> i" + System.Environment.NewLine +
                        "GG -> g" + System.Environment.NewLine +
                        "RC -> WH NP | WH VP" + System.Environment.NewLine +
                        "WH -> w" + System.Environment.NewLine;

    public cfgForm(HomeForm homeForm)
    {
        // TODO: Complete member initialization
        InitializeComponent();
        this.homeForm = homeForm;
    }

    private void btnDefaultCFG_Click(object sender, EventArgs e)
    {
        tbCNF.Text = CNF;
    }

    private void cfgForm_Load(object sender, EventArgs e)
    {
        tbCNF.Text = CNF;
        btnApplyCNF.Enabled = false;
    }

    private void btnApplyCNF_Click(object sender, EventArgs e)
    {
        //CNF = "";
        CNF = tbCNF.Text;
        HomeForm homeForm = new HomeForm();
        homeForm.cfg = this;
        //homeForm.ShowDialog(); 
        this.Hide();// doesn't work
    }

So far, those code running well, if I use homeForm.ShowDialog(). I mean that, after cnfRulesMenu_Click is active and show cfgForm stood by HomeForm , then i update CNF variable in cfgForm, then back to HomeForm, and cfgForm.Hide. But it give null value.

Any suggestion Sir..

Because you new another HomeForm in these code.

private void btnApplyCNF_Click(object sender, EventArgs e)
{
    //CNF = "";
    CNF = tbCNF.Text;
    HomeForm homeForm = new HomeForm();
    homeForm.cfg = this;
    //homeForm.ShowDialog(); 
    this.Hide();// doesn't work
}

So, these code didn't work as your wish.

public cfgForm cfg;
string strCNF = "";
strCNF = ((cfgForm)this.cfg).CNF;

Modify your code to these may solve your problem, but I must to say these architecture didn't make sense.

private void btnApplyCNF_Click(object sender, EventArgs e)
{
    //CNF = "";
    CNF = tbCNF.Text;
    this.homeForm.cfg = this;
    //homeForm.ShowDialog(); 
    this.Hide();// doesn't work
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM