How to access the values of form1 control to form2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ezhar
    New Member
    • Aug 2010
    • 13

    How to access the values of form1 control to form2

    Hi,
    I have two form i my project. Form1 contains a text box with public access modifiers and a button.
    and Form 2 contains a label and a button.
    the following task i am trying to do.
    when some one enter some text in text box and click the button it will show the form two and its bit easy task.
    But the problem is that I want to access the values of text box(Form1) to Form2 when i will click the button on form 2 but getting error the code is
    Code:
    //Form1 Button Code
    private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text != "")
                {
                    frm.Show();
                   
                }
            }
    //form 2 code
    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                label1.Text = Form1.ActiveForm.Controls["textBox1"].Text.ToString();
            }
        }
  • fastestindian
    New Member
    • Aug 2009
    • 74

    #2
    You need to instance of the of the Form2.
    When u create you have to create the object of Form2 which you can store globaly.

    Create a property in that class in Form2.
    Code:
    public string PropNearest        {            get            {                return Nearest.Text;            }            set            {                Nearest.Text = value;            }        }        public string PropNearest
            {
                get
                {
                    return Nearest.Text;
                }
                set
                {
                    Nearest.Text = value;
                }
            }
    Whenever you needed the value of the Form2 text box then just type Form2.PropNeare st;

    Best of luck....

    Comment

    • ezhar
      New Member
      • Aug 2010
      • 13

      #3
      i want value from form1

      Comment

      • mcfly1204
        New Member
        • Jul 2007
        • 233

        #4
        So you have form1 with a textbox that contains a value and you want to create an instance of form2 from within form1, and then reference that value of the text box in form1?

        Comment

        • ezhar
          New Member
          • Aug 2010
          • 13

          #5
          yes i want to do the same

          Comment

          • mcfly1204
            New Member
            • Jul 2007
            • 233

            #6
            I think this is correct, and textbox1 should be public.

            Code:
            Form1 form = (Form1)Application.OpenForms["Form1"];
            
            label1.Text = form.textbox1.Text.ToString();

            Comment

            Working...