C# exchanging values between forms

2019-08-31 08:05发布

I'm making a system for ticket sales for concerts and stuff like that. When I select the event from a comboBox, a new form is opened showing the available tickets for that show in a DataGridView; without closing the main form, which contains the billing data. When I double click the ticket chosen, the secondary form with the datagridview needs to be closed, sending certain values to the active window. I do not know how to do the last part.

I send the event from the comboBox to the new window to make the query:

Boleto b = new Boleto(comboBox8.Text);
                b.Show();

Then I open the connection and load the select into the DGV

    try
    {
        connStr = "Server=localhost;Database=quanax;Uid=root;Pwd=root;Port=3306";
        conn = new MySqlConnection(connStr);
        conn.Open();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: No se puede conectar con la base de datos");
        MessageBox.Show(ex.ToString());
    }

    try
    {
        string query = "select id_boleto as 'Id', fecha as 'Fecha', evento as 'Evento', seccion as 'Sección', fila as 'Fila', asiento as 'Asiento', bloque as 'Bloque', total as 'Precio' from boletos where evento = '" + evento + "';";
        mySqlDataAdapter = new MySqlDataAdapter(query, conn);
        mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter);
        dataTable = new System.Data.DataTable();
        mySqlDataAdapter.Fill(dataTable);
        bindingSource = new BindingSource();
        bindingSource.DataSource = dataTable;
        dataGridView1.DataSource = bindingSource;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error! Intenta de nuevo");
        MessageBox.Show(ex.ToString());
    }

And when double clicked I get the values that I need into variables

                 try
                {
                    MySqlCommand cmdc = new MySqlCommand("select seccion, fila, asiento, bloque, total from boletos where id_boleto ="+toolStripTextBox1.Text+";", conn);
                    MySqlDataAdapter dataadapc = new MySqlDataAdapter(cmdc);
                    System.Data.DataTable datatabc = new System.Data.DataTable();
                    dataadapc.Fill(datatabc);

                    aux1 = Convert.ToString(datatabc.Rows[0][0]);
                    aux2 = Convert.ToString(datatabc.Rows[0][1]);
                    aux3 = Convert.ToString(datatabc.Rows[0][2]);
                    aux4 = Convert.ToString(datatabc.Rows[0][3]);
                    aux5 = Convert.ToString(datatabc.Rows[0][4]);                    
                }

                catch (IndexOutOfRangeException ex)
                {
                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: No se puede conectar con la base de datos");
                    MessageBox.Show(ex.Message);
                }

If I needed to do this closing the main form, making an overloaded constructor whould be the solution:

try
            {
                this.Close();         
                Form1 f1 = new Form1(aux1, aux2, aux3, aux4, aux5);
                f1.Show();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.ToString());
            }

But, since I'm not closing the main window, how can I send the values aux1, aux2, aux3, aux4 and aux5 to the active form when closing the secondary form? I hope I made myself clear. Thanks.

3条回答
SAY GOODBYE
2楼-- · 2019-08-31 08:58

One of available way is using one data at both form and changing at second form shares changes at first form. Another way is be handling Closed event at first form and using second form properties. ...

You should decide what do you want to do! I hope this helps.

查看更多
仙女界的扛把子
3楼-- · 2019-08-31 09:00

I can suggest two ways: 1. Use "ShowDialog()". this way, you can set aux1, aux2... as properties and get the data easily from the main form:

Boleto b = new Boleto(comboBox8.Text);
b.ShowDialog();
this.aux1 = b.aux1;
.
.
  1. Pass "this" as a reference and have properties in the main form to hold these aux values. So you can call this up. Overload your constructor. Make sure you save the instance of Mainform somewhere.

    public Boleto(string text, MainForm form){//Code here}
    

then you can set it up before you close:

form.aux1 = aux1;
查看更多
冷血范
4楼-- · 2019-08-31 09:01

You can declare a static list of strings and then use it. For example in your main form use this:

public static List<string> Values = new List<string>();
// then add your strings into a list
Values.Add("String1");
Values.Add("String2");
Values.Add("String3");
Values.Add("String4");
Values.Add("String5");

Then in your secondary form use them:

foreach(string s in MainForm.Values)
{
    // and do what ever you want
}
查看更多
登录 后发表回答