0

I have 2 winforms in my project. When i clicked on "Settings" button on Form1, it shows the Settings form, I'm making some changes on textboxes and when I click the Save button on second form, it saves these values to a text file and I wanna pass these values to first form, but I couldn't pass them.

Here is some parts of my codes;

This code is Settings button click (on Form1)

private void button3_Click(object sender, EventArgs e)
{
    Settings frm = new Settings();
    frm.Show();  
}

public void funData(TextBox txtForm1)
{
    label3.Text = txtForm1.Text;
}

and this code is Save button click (Second form)

private void button5_Click(object sender, EventArgs e)
{
    if (File.Exists(ConfigFile))
    {
        File.Delete(ConfigFile);
        using (StreamWriter writer = new StreamWriter(ConfigFile))
        {
          writer.WriteLine(txtTemsPath.Text);
          writer.WriteLine(txtVodafonePath.Text);
          writer.WriteLine(txtTurkcellPath.Text);
          writer.WriteLine(txtAveaPath.Text);
          writer.Close();
        }
    }
    else
    {
       using (StreamWriter writer = new StreamWriter(ConfigFile))
        {
            writer.WriteLine(txtTemsPath.Text);
            writer.WriteLine(txtVodafonePath.Text);
            writer.WriteLine(txtTurkcellPath.Text);
            writer.WriteLine(txtAveaPath.Text);
            writer.Close();
        }
    }
    Form1 frm = new Form1();
    delPassData del = new delPassData(frm.funData);
    del(this.txtTemsPath);
    frm.getSettings();
    frm.TemsPath = TemsPath;
    frm.Activate();
    frm.Refresh();
    this.Close();
}

Could you please help me for this issue?

Thanks

Joel
  • 7,401
  • 4
  • 52
  • 58
  • What do you mean by 'couldn't pass them'? Are you getting an error? – Paul Michaels Oct 10 '13 at 13:24
  • Few things: 1) You appear to just want to overwrite the configuration file. You do not need to check it exists, delete it, then rewrite it, by default the `StreamWriter` will overwrite it unless you explicitly tell it not too. 2) The two writing blocks appear to do the same thing regardless, 3) Don't call `.Close()`, having it in a `using` block does this for you and finally 4) How doesn't it work so far? What errors are you getting? – Arran Oct 10 '13 at 13:24

3 Answers3

0

define on your first Form:

 Settings obj = (Settings)Application.OpenForms["Settings"];

private void button3_Click(object sender, EventArgs e)
    {
        Settings obj = new Settings();
        obj.Show();  
    }

And replace in your code anywhere else frm with obj

The thing is that you must refer each time to the current instance of the other form and not open a new one

apomene
  • 14,282
  • 9
  • 46
  • 72
0

You need to create a public property accessor on form2 with the data you would like to store. After form2 closes you will still be able to access this data by using form2.MySpecialData as long as you havent nullified it. this question has been asked many times on stackoverflow and there are alot of good examples.

Communicate between two windows forms in C#

    public Form2()
    {
        InitializeComponent();
    }

    private string mySpecialData;

    public string MySpecialData
    {
        get { return mySpecialData; }
        set { mySpecialData = value; }
    }
Community
  • 1
  • 1
Jwit
  • 156
  • 6
0

Add a property to Settings to return the "TemsPath" value. Then, instead of Close(), set DialogResult to OK:

public partial class Settings : Form
{

    public string TemsPath
    {
        get { return txtTemsPath.Text; }
    }

    private void button5_Click(object sender, EventArgs e)
    {

        // ... your save code ...

        this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }

}

Now, back in Form1, use ShowDialog() instead of Show() and access the property when it returns:

public partial class Form1 : Form
{

    private void button3_Click(object sender, EventArgs e)
    {
        Settings frm = new Settings();
        if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            label3.Text = frm.TemsPath;
        }
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40