2

I have two forms, form 1 have textboxes, radioboxes, save button to save the data required and transaction list button to open the second form. Form 2 have listbox. I want help with save button to save data of textboxes and radioboxes and then clicking transaction list button to display them on listbox in form 2.

Form1

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

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void trans_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.ShowDialog();
    }

    private void save_Click(object sender, EventArgs e)
    {
        

    }
 }
}

Form2

namespace form1
{
  public partial class Form2 : Form
  {
    
    public Form2()
    {
        InitializeComponent();
    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void back_Click(object sender, EventArgs e)
    {
        this.Close();
        Form1 frm = new Form1();
        frm.ShowDialog();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
  }
}
  • On the second form in the `back_Click` event… remove the lines… `Form1 frm = new Form1();` … and … `frm.ShowDialog();` … if the first form is already open… there is no need to create a new one. In addition the code in form1 that opens form 2 appears to use a `ShowDialog`, so code execution will return to that point when form 2 is closed. – JohnG Dec 09 '21 at 23:25
  • Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – JohnG Dec 09 '21 at 23:27
  • Sorry, thats not what i need. – ÇAĞKAN ERGÜDEN Dec 10 '21 at 09:56
  • Well… then can you be more specific on what _“you need.”_ It appears you are asking multiple questions… one in reference to saving data from text boxes etc.… and another about… _“clicking transaction list button to display them on listbox in form 2.”_ … ? … is vaguer yet my duplicate link was a reference to passing data between two forms. In addition, in the posted code… I do not see anything related to either task…? Also, I am confident you do not want to create a “new” `Form1` from `Form2`… I apologize if I am missing something obvious. – JohnG Dec 10 '21 at 10:16
  • I suggest you peruse the SO [tour] section as it shows how SO works. The [ask] section may help. In addition, you may find the SO [Asking](https://stackoverflow.com/help/asking) section useful. – JohnG Dec 10 '21 at 10:28
  • In your example there is only one button to pass data between two forms, what i need is one "save button" to only save the data and another button "transaction list" to display saved data in another forms Listbox. Listbox contains "Date" textbox, "Credit/Debit" radioboxes, and "Balance" textbox. – ÇAĞKAN ERGÜDEN Dec 10 '21 at 10:40
  • OK… you want two buttons… one to save data to a file and a second button to open a different form and read some data from a file into the second form. What is preventing you from doing either of these things? – JohnG Dec 10 '21 at 22:13

1 Answers1

0

I usually add an Initialize in the second form that has parameters for each of the items you want to pass to the second form. In the trans_Click method, add a call to initialize(a,b,c,d). I would move the frm.ShowDialog(); also.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Bill
  • 1
  • 2