2

These are my 2 Forms. enter image description here

These are the codes for Form 1-->

namespace Passing_Values
{
    public partial class Form1 : Form
    {
        string a="preset value";
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpenF2_Click(object sender, EventArgs e)
        {
            new Form2().Show();
        }

        public void set(string p)
        {
            MessageBox.Show("This is Entered text in Form 2 " + p);
            a = p;
            MessageBox.Show("a=p done! and P is: " + p + "---and a is: " + a);
            textBox1.Text = "Test 1";
            textBox2.Text = a;
            textBox3.Text = p;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(a);
        }
    }
}

These are the codes for Form 2-->

namespace Passing_Values
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string g;
            g = textBox1.Text;

            Form1 j = new Form1();
            j.set(g);
        }
    }
}

See the picture.You can understand the design.

This is what I want to do. 1st I open Form2 using button in Form1. Then I enter a text and click the button("Display in Form1 Textbox"). When it's clicked that value should be seen in the 3 Textboxes in Form1.I used Message Boxes to see if the values are passing or not. Values get passed from Form2 to Form1. But those values are not displays in those 3 Textboxes but the passed values are displayed in Message Boxes. Reason for the 3 Text Boxes can be understood by looking at the code. So what's the error?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
sajithneyo
  • 641
  • 3
  • 7
  • 17
  • you should create variables in Form1 that you want to hold when closing Form2 there are a couple of ways to do this .. but just calling `new Form2().Show();` is not the approach I would take.. I would create a var frm2 = new Form2(), then pass and or create variables to hold the values when you close form2, then when you show form 2 I would call the ShowDialog()` method so that when you close it comes back to the calling method.. then I would call frm2.Dispose()` hope this makes sense – MethodMan Aug 18 '16 at 20:01
  • Sorry I'm new to VS or C# it's better if you provide the code. So I can understand it! Thank you! @MethodMan – sajithneyo Aug 18 '16 at 20:08
  • it's better that you do some googling on how to do this .. this is not a `Code Factory Site` and this is not that difficult.. so no I will not do the work for you.. sorry this isn't how this site works. I gave you an explanation on how to do this.. – MethodMan Aug 18 '16 at 20:11
  • 2
    @MethodMan is right. With a bit of Google searches you will find a lot of examples. For example http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms (10sec to find, first line) – Stef Geysels Aug 18 '16 at 20:15
  • I googled. But didn't get a correct answer. That's why I came here :/ BTW tnx for the link @DDD Soft – sajithneyo Aug 18 '16 at 20:30
  • @KumarathungaMunidasa please check my answer and see if you are able to understand it well. Any queries related to that, just kindly comment on it :) – Raktim Biswas Aug 18 '16 at 20:34
  • Take a look at [this post](http://stackoverflow.com/questions/17836398/passing-values-between-windows-forms-c-sharp) or [this one](http://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application). Also [this one](http://stackoverflow.com/a/38769212/3110834). – Reza Aghaei Aug 18 '16 at 20:36

4 Answers4

4

Actually I have an object to pass. So I did this

in form1-->

private void btnOpenF2_Click(object sender, EventArgs e)
        {
            new Form2(this).Show();
        }

in form2-->

public partial class Form2 : Form
    {
        Form1 a;
        public Form2(Form1 b)
        {
            a = b;
            InitializeComponent();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            string g;
            g = textBox1.Text;


            a.set(g);
            this.Close();
        }
    }
sajithneyo
  • 641
  • 3
  • 7
  • 17
1

I would simply pass it in the constructor. So, the code for form2, will be:

public partial class Form2 : Form
{
    string _input;

    public Form2()
    {
        InitializeComponent();
    }

    public Form2(string input)
    {
        _input = input;
        InitializeComponent();

        this.label1.Text = _input;
    }
}

And the call in Form1 will be:

private void button1_Click(object sender, EventArgs e)
{
    fm2 = new Form2(this.textBox1.Text.ToString());
    fm2.Show();
}
0

Place a static string in your Form2

public static string s = string.Empty;

and, in your Display in Form1 Textbox button click event, get the value from the textbox in your string s:

s = textBox1.Text;
Form1 f1 = new Form1();
f1.Show();

once, the Form1 is showed up again, then in the Form1_Load event, just pass your Form2's text value to your Form1's textboxes, the value of which was gotten by the variable s:

foreach (Control text in Controls)
{
    if (text is TextBox)
    {
        ((TextBox)text).Text = Form2.s;
    }
}

Display

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
0

enter image description here

public partial class Form1 : Form
{
    Form2 fm2;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        fm2 = new Form2();
        fm2.Show();
        fm2.button1.Click += new EventHandler(fm2button1_Click);
    }
    private void fm2button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = fm2.textBox1.Text;
    }


}

And code in form2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
}

set modifier property of textbox1 and button1 to public

Manu Varghese
  • 791
  • 8
  • 25
  • Even I set the button modifier property to public I could still get this error `'Form' does not contain a definition for 'searchButton' and no accessible extension method 'searchButton' accepting a first argument of type 'Form' could be found (are you missing a using directive or an assembly reference?) ` why? – Travis Su May 03 '21 at 11:44