Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Visual-Studio
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..
Posted 1-Apr-13 18:53pm
Comments
ThePhantomUpvoter at 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 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Maciej Los at 2-Apr-13 3:10am
   
+5
Please, see my answer.
Sergey Alexandrovich Kryukov at 2-Apr-13 8:49am
   
Thank you, Maciej.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
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. Wink | ;)
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 Maciej Los 270
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 202
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100