Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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......
 

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;
        }
}
}
 

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 13-Dec-12 8:40am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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();
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Change the Form2 class into this:
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:
 private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Show();
 
        }
Hope this helps.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 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;
        }
    }
 

 

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();
       }
   }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100