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?

7
  • 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 Commented Aug 18, 2016 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 Commented Aug 18, 2016 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.. Commented Aug 18, 2016 at 20:11
  • 2
    @MethodMan is right. With a bit of Google searches you will find a lot of examples. For example codeproject.com/Articles/14122/Passing-Data-Between-Forms (10sec to find, first line) Commented Aug 18, 2016 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 Commented Aug 18, 2016 at 20:30

4 Answers 4

5

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();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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();
}

Comments

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

1 Comment

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?
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

2 Comments

Can you foreach(Textbox text in Controls) ?
@khargoosh you cannot because you need to define the base class as Control and Textbox is not a base class here for all controls :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.