Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

C#
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 :

C#
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..
Posted
Comments
[no name] 1-Apr-13 23:57pm    
This is because in your btnApplyCNF_Click method, you are creating a brand new instance of your HomeForm when you should be using the already existing instance that you passed through the form's constructor but then overwriting with a new instance.
Berry Harahap 2-Apr-13 0:21am    
CNF = tbCNF.Text;
homeForm.cfg = this;
That's working. But, how i get value string CNF from cfgForm to be used in HomeForm without opening cfgForm before?

There cannot be such things as "form variables", but they can be instance fields or instance properties of a form class.

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
Maciej Los 2-Apr-13 3:10am    
+5Please, see my answer.
Sergey Alexandrovich Kryukov 2-Apr-13 8:49am    
Thank you, Maciej.
—SA
Notes to your code:
1)
You want to show/hide forms between main form and other forms. Please, read this:
How to: Display Modal and Modeless Windows Forms[^]
How to: Choose the Startup Form in Windows Application[^]
How to: Change the Startup Object for an Application[^]
How to: Make a Startup Windows Form Invisible[^]

2)
If you use long string as a variable, please use StringBuilderClass[^] to build this long string.Using StringBuilder Class[^] is simplest than do that:
c3
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;

You can simply Append[^] text and than use ToString() method.
C#
StringBuilder MyStringBuilder = new StringBuilder("Hello Kitty!");
MyStringBuilder.Append(" What a beautiful day.");
MyStringBuilder.Append(" What a wonderful world.");
MyStringBuilder.Append(" What a beautiful women.");
string s = MyStringBuilder.ToString();


3)
If you want to pass data between forms, please see these:
Passing Data between Windows Forms[^]
Using a delegate to pass data between two forms[^]
There is a lot of examples on this site. Please use "Search" box on the right-top corner. ;)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Apr-13 8:54am    
It took some patience :-), my 5.

My note about long string: a long string itself is a sign or bad approach...
In this case, it should rather be some rule engine with its data stored somewhere, at least in a resource...
If this is just an experimental code for now, it's quite acceptable though.

—SA
Maciej Los 2-Apr-13 8:57am    
Thank you, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900