I have this button in Form2 that when is clicked, a string value is passed back to Form1 and it is displayed in a textbox.

public partial class Form2 : Form
    {
        double a, b, ave;
        string stype;
        Form1 frm = new Form1();

        public Form2(double x, double y)
        {
            InitializeComponent();
            a = x; b = y;
            ave = (a + b) / 2;
        }

        ...

        private void button2_Click(object sender, EventArgs e)
        {
            frm.textBox6.Text = stype;
        }

textBox6 Modifiers is already Public.

Recommended Answers

All 8 Replies

Was there a question in there?

I believe the question is "Why isnt the text showing when i call 'frm.textbox6.Text = stype' ??"
The answer is, frm holds a reference to a new instance of Form1, an instance that has not been displayed on the screen. Instead of Form1 frm = new Form1() you need to store a reference to your current Form1.
Also, making a control public is terrible OO practice. You should always access internal members through methods and properties.
I have a tutorial here that shows how to pass values back to a control on the calling form.

I would also suggest you look into modal forms; if you want to prevent the user from doing anything in Form1 before they have finished in Form2 then you can use Form2.ShowDialog() instead of Form2.Show(). ShowDialog opens the form modally, the code execution on the calling method pauses until Form2 is closed, because of this, you can place code to process the result of Form2 in the same method that calls it:

private void GetValueFromForm2()
{
    Form2 frm = new Form2();
    frm.ShowDialog(); //code in this method pauses here
  
    //the next line will be executed [B]only[/B] when frm is closed.
    string result = frm.Value;
    
    //process result here....
}

public partial class Form2 : Form
{
    private string _value;
    
    //read only property to return value
    public string Value
    {
        get { return _value; }
    }
}

Was there a question in there?

Oh, right! Forgive me if I have forgotten. Anyway, the question had been defined by another user and he was able to give me the right one. Thank you. :D

I believe the question is "Why isnt the text showing when i call 'frm.textbox6.Text = stype' ??"
The answer is, frm holds a reference to a new instance of Form1, an instance that has not been displayed on the screen. Instead of Form1 frm = new Form1() you need to store a reference to your current Form1.
Also, making a control public is terrible OO practice. You should always access internal members through methods and properties.
I have a tutorial here that shows how to pass values back to a control on the calling form.

Oh, thank you for replying. Really, it helped me. I've learned from your tutorial. Forgive me if you still have to guess for the question. I really appreciate your help. Thank you, thank you! :)

No problem, remember to mark the thread as solved if your question has been answered :)

its really simple.

** Create a project......>windows application.
** form1 will be like that.
** add a textbox.
** add a button
** form2 will be like that.
** add a texbox.

when you press the button of form1 you will get the text you input in the form1 textbox at form2.

simple code will like that.
[c]


for form1 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 ob;
        public Form1()
        {
            InitializeComponent();           
        }

        private void button1_Click(object sender, EventArgs e)
        {           
            string s;
            s = textBox1.Text;
            ob = new Form2(s);
            ob.Show();
        }
    }
}

for form2 : 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        string ss;
        public Form2(string s)
        {
            InitializeComponent();
            ss = s;
            textBox1.Text = ss;
        }
   }
}

it will work fine in visual stdio 2008 express edition.

This should do it:

//on form1:

Form2 f2 = new Form2(this);
form2.Show();

//create a method and a label on Form1
//to pass data from form2 to form1;
public void MyMethod(string value)
{
  Label lablel1 = new Label1;
  label1.Location = new Point(10,10);
  this.Controls.Add(label1);
  label1.Text = value;
}

//on form2:
//constructor
public Form2(Form1 _f1)
{
form1 = _f1;
}
Form1 form1;

private void Button_Click(object sender, EventArgs e)
{
 form1.MyMethod(textBox1.Text);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.