-2

All.

I'm newbie in C#. I know this is very popular question. But I didn't understand. I know there is a mistake, but where? For example - first part of code Form1 include private variable test, I need to get the value of this variable in the Form2. Where is the error?

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
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string test = "test this point";
            Form2 dlg = new Form2();
            dlg.test = test;
            dlg.Show();
        }
    }
}

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
    {
        public string test { get; set; }

        public Form2()
        {
            InitializeComponent();
            label1.Text = test;
        }
    }
}
annayak
  • 103
  • 2
  • 4
  • 10
  • Well you would have to assign it to `Form2 test` property what you have is a independent string which has nothing to do with the property of the the `Form2`. `dlg.test = test;` in `Form1` is needed. – V4Vendetta May 09 '12 at 10:17

4 Answers4

4

In your Form2 you are using a public property, because it is public you can assign it via the object in form1. For example:

 private void button1_Click(object sender, EventArgs e)
        {
            Form2 dlg = new Form2();
            dlg.test = "test this point";
            dlg.Show();
        }

There are a couple of ways to use this in form 2, if you just want it to set the text property of the label only, this would be the best:

public partial class Form2 : Form
    {
        public string test 
        { 
           get { return label1.Text; }
           set { label1.Text = value ;} 
        }

        public Form2()
        {
            InitializeComponent();
        }
    }

Within the setter of the property you could also call a function if required.

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
1

You are right, this type of question has been asked many times, slightly different versions... Here's some answers I've provided in the past

This might be closest for what you are looking for

One answer getting a value via a method calls

Another, with step-by-step to create two forms and getting values to/from the other with function or Getter(setter)

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142
0

You're not using the string test anywhere within your method. Try this:

private void button1_Click(object sender, EventArgs e)
{
    Form2 dlg = new Form2();

    dlg.Test = "test this point";

    dlg.Show();
}

See how you're assigning the value to the property Test on the Form2 object dlg.

Note: I used a captial for the property Test since that's the general consensus on style for property names.

Alexander R
  • 2,468
  • 24
  • 28
0

test is a property available for Form2 (better case it to Test) while the string test is just scoped for the click event of Form1. It has no relation to the property unless you assign it.

Form2 dlg = new Form2();
dlg.test = test; // this will assign it
dlg.Show();

Now Form2 property has got the value which will be used to display the same in the Label

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82