Start New Discussion within our Software Development Community

//I have this code

//In my Form1 I have textbox and a button
namespace Passing
{
    public partial class Form1 : Form
    {
        Class1 class1 = new Class1();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            class1.Name = textBox1.Text;
            Form2 form2 = new Form2();
            form2.Show();
        }
    }
}
///I'm having a problem here --> Form2 form2 = new Form2();
///In my Form 2 I only have label1
namespace Passing
{
    public partial class Form2 : Form
    {
        Class1 class1;
        public Form2(Class1 class1)
        {
            class1.Name = label1.Text;

            InitializeComponent();
            this.class1 = class1;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}
//And in my class
namespace Passing
{
    class Class1
    {
        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}

I'm having a hard time in here, since i;m new in c#. I want to input a text in my form1 and after clicking the button the text will be outputed on form2, label1.

Any help is appreciated. Thank you in advance :)))

Well, the line:

Form2 form2 = new Form2();

is not calling the constructor you define above. It is calling the default constructor. You would have to change it to something like this:

Form2 form2 = new Form2(class1);

Also, in the Form2 constructor, you are assigning label1.Text to class1.Name, I think you want the opposite. I.e.:

label1.Text = class1.Name;
This article has been dead for over six months. Start a new discussion instead.