Alynson Torres

Alynson Torres

  • 1.6k
  • 149
  • 2.1k

Pass data between open C # Forms

Jan 3 2021 8:18 PM
Hi happy new year
My problem is the following, in Form1 I have 3 buttons, the 2 at the top open the FrmFruit and the FrmVegetable in the panel, the button next to the text boxes opens the FrmFruit outside of the panel.
If I open the form from the enter button, I enter the data in it and when I enter I send the data from the FrmFruit to Form1  without problem, but if I try it from the FrmFruit open in the panel, no data is passed to me, see if you can help me.
what I need is to be able to send the data from the FrmFruit open in the panel, without it closing or opening another form.
 
 
 
 Form1
  1. public partial class Form1 : Form, Clform  
  2. {  
  3.   
  4.     public Form1()  
  5.     {  
  6.         InitializeComponent();  
  7.     }  
  8.   
  9.     private void Form1_Load(object sender, EventArgs e)  
  10.     {  
  11.          
  12.     }  
  13.   
  14.     public void ChangeTextBoxText(string text)  
  15.     {  
  16.         tbxName.Text = text;  
  17.         tbxquantity.Text = text;  
  18.     }  
  19.   
  20.     //BUTTON OPEN THE FORM  
  21.     private void btnEnter_Click(object sender, EventArgs e)  
  22.     {  
  23.         FrmFruit form2 = new FrmFruit ();  
  24.         form2.ShowDialog(this);  
  25.     }  
  26.   
  27.     //METHOD TO OPEN THE FORM ON THE PANEL----------------------------------------------------------  
  28.     private Form formActived = null;  
  29.   
  30.     private void OpenFormPanel(Form FormSon)  
  31.     {  
  32.         if (formActived != null)  
  33.             formActived .Close();  
  34.         formActived = FormSon;  
  35.         FormSon.TopLevel = false;  
  36.         FormSon.Dock = DockStyle.Fill;  
  37.         pnlcontent.Controls.Add(FormSon);  
  38.         pnlcontent.Tag = FormSon;  
  39.         FormSon.BringToFront();  
  40.         FormSon.Show();  
  41.     }  
  42.   
  43.   // BUTTON OPEN THE FORM ON THE PANEL  
  44.     private void btnFruit_Click(object sender, EventArgs e)  
  45.     {  
  46.         OpenFormPanel(new FrmFruit ());  
  47.     }  
  48. }  
 
 FormFruit
 
  1. public partial class FrmFruit : Form  
  2. {  
  3.     public FrmFruit()  
  4.     {  
  5.         InitializeComponent();  
  6.     }  
  7.   
  8.    // ENTER BUTTON  
  9.     private void btnEnter_Click(object sender, EventArgs e)  
  10.     {  
  11.         Iform formInterface = this.Owner as Iform;  
  12.   
  13.         if (formInterface != null)  
  14.             formInterface.ChangeTextBoxText(tbxNameFruit.Text, );  
  15.            formInterface.ChangeTextBoxText(tbxquantityFruit.Text);  
  16.     }  
  17. }  
 

Answers (4)