1

Hello everyone I'm new to c#, using visual c# 2010 and I'm trying to learn the basics.

I've created two forms (Form1 and Form2)

each form has a text box and a button

my experiment( form1 to form2) is as follows:

I've declared a string

public string deneme; I made both text boxes public, and in form 1 button I write the following:

deneme= textbox1.text;
Form2 frm2 = new Form2();
form2.show();

form2.textbox1.text= deneme;

when I do this it works and I see my input on form2 textbox. What I want to do is; to press the button on form1 and open form2, then write something on the text box and display that input on form1 text box, I use the same method but it returns nothing.. what an I doing wrong?

and I'm sorry for my bad english

AsIm Gunduz
  • 25
  • 2
  • 7

5 Answers5

1

The simple way is a property of the Form2

You can add a property to the form2

public string Result{get;set;}

and check it in external code

form1.textbox1.text= form2.Result;
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
0
deneme= textbox1.text;
Form2 frm2 = new Form2(dename);
form2.show();

In form2, do this in the Initialize method:

Initialize(string name)    
{
        form2.textbox1.text= name;
}
0

Check these out for more than just a single value passed to another form, but to also handle passing values back/forth and also hooking into events... Not advanced stuff, but does have some step-by-step samples too

Prior posting here

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

Hello all I have solved my issue, here is what I have done !

In the second form, I have write the following codes

Form1 mymainform;

public Form2(Form1 m)
{
    InitializeComponent();
    afrm1 = m;
}

and in the button click I write the following

mymainform.Textboxnameinform1.text = textboxnameinform2.text;    
this.close();

and in Form1, where I have the button to call form2 I write the following codes

Form2 frm2 = new Form2(this);    
frm2.Show();

if anyone is having the same difficulty and couldn't figure it out, please don't hesitate to contact me I'll be happy if I can help you

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
AsIm Gunduz
  • 25
  • 2
  • 7
  • After I have followed by your coding, I get this problem. [code]An object reference is required for the non-static field, method, or property[/code] – user3423149 Dec 01 '14 at 04:33
0
 //this code worked for me
 //in form2 put following code prevent form from opening multiple times  
 public partial class Form2 : Form
    {
        public Form2()
        {
             InitializeComponent();
        }
        private static Form2 Instance;
        public static Form2 GetInstance()
            {
               if (Instance ==null || Instance.IsDisposed)
                {  
                    Instance = new Form2();
            }
            else
             {
                Instance.BringToFront();
            }
                 return Instance;
         }

 // in form1

 public partial class Form1 : Form
     {
         public Form1()
        {
             InitializeComponent();
        }


         private void Button2_Click(object sender, EventArgs e)
         {
            Form2 form2 = Form2.GetInstance();
            form2.textBox1.Text = textBox1.Text;
            form2.Show();
        }
    }
lee
  • 1
  • 2