Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all....please do help me...am stuck with this problem....

There are 2 forms.
In one form(form1),there are 3 textboxes(tb1,tb2,tb3) and corresponding to each there is a button..i.e: for tb1-btn1,tb2-btn2,and tb3-btn3
In second form(form2),there is one textbox(tb4) and one button(btn4)...
When i click on a button(say btn1)in form1,form2 should be displayed.and when some text is entered in textbox(tb4) and button(btn4) is clicked that text should be displayed in the textbox(tb1) in form1 which is corresponding to the button(btn1) clicked first.......like that for btn2 and btn3....The prob is what i enter in tb4 is displaying in all 3 textboxes(tb1,tb2,tb3)...
I dont want it like dat....What i want is....When btn1 is clicked value must be displayed only in tb1,similarly when btn2 is clicked value must be shown in tb2....and so for btn3...
This is in C#.net Windows Application
Here is the code i tried......


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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();

        }

        internal void valuefromform2(string p)
        {
            textBox1.Text = p;
        }
}
}



C#
public partial class Form2 : Form
    {
        Form1 f1;
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (f1 == null)
                f1 = new Form1();
            f1.valuefromform2(textBox1.Text);
            f1.Show();
        }


    }



pls do help me....am stuck here...without this i cant go further in my project...pls pls...
Posted

humm I would say you have done one of the complex way to doing a simplest task in the world.

Have you heard about Properties in C#. ??
If it was me. I would expose the TextBox from the Form2 as a Get only property and use that object to update in my parent form.
eg:

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

   private void button1_Click(object sender, EventArgs e)
   {
      Form2 f2 = new Form2();
      if( f2.ShowDialog() == DialogResult.OK)
      {
        textBox1.text = f2.TxtBox.Text;
      }
    }   
}

public partial class Form2 : Form
{
    public TextBox TxtBox
    {
      get {return textBox1;}
    }
    public Form2()
    {
       InitializeComponent();
    }
}
 
Share this answer
 
Hi,

Change the Form2 class into this:
C#
public partial class Form2 : Form
    {
        Form1 f1 = null;
        public Form2(Form1 _form1)
        {
            InitializeComponent();
            f1 = _form1;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (f1 == null)
                f1 = new Form1();
            f1.valuefromform2(textBox1.Text);
            f1.Show();
        }
 

    }

And change the button1_Click function into this:
C#
private void button1_Click(object sender, EventArgs e)
       {
           Form2 f2 = new Form2(this);
           f2.Show();

       }

Hope this helps.
 
Share this answer
 
v3
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }
       bool btn1click = false;
       bool btn2click = false;
       bool btn3click = false;
       public void button1_Click(object sender, EventArgs e)
       {
           Form2 f2 = new Form2(this);
           f2.Show();
           btn1click = true;

   }

       internal void valuefromform2(string p)
       {
          // throw new NotImplementedException();
           if (btn1click == true)
           {
               textBox1.Text = p;
               btn1click = false;
           }
           if (btn2click == true)
           {
               textBox2.Text = p;
               btn2click = false;
           }
           if (btn3click == true)
           {
               textBox3.Text = p;
               btn3click = false;
           }
       }

       private void button2_Click(object sender, EventArgs e)
       {
           Form2 f2 = new Form2(this);
           f2.Show();
           btn2click = true;

       }

       private void button3_Click(object sender, EventArgs e)
       {
           Form2 f2 = new Form2(this);
           f2.Show();
           btn3click = true;
       }
   }





C#
public partial class Form2 : Form
   {
        Form1 form1=null;


       public Form2()
       {
           InitializeComponent();
       }

       public Form2(Form1 form1)
       {
           // TODO: Complete member initialization
           InitializeComponent();
           this.form1 = form1;
       }

      

       private void button1_Click(object sender, EventArgs e)
       {
           if (form1 == null)
              form1 = new Form1();
           form1.valuefromform2(textBox1.Text);
           form1.Show();
           this.Close();
       }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900