C#交换表单之间的值 [英] C# exchanging values between forms

查看:153
本文介绍了C#交换表单之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作音乐会门票销售系统和类似的东西。
当我从组合框中选择事件时,会打开一个新窗体,在DataGridView中显示该节目的可用门票;而不关闭包含账单数据的主表单。当我双击选中的票证时,需要关闭具有datagridview的二级表格,并将某些值发送到活动窗口。我不知道如何做最后一部分。



我从comboBox发送事件到新窗口进行查询:

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

然后打开连接并将选择加载到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());
}

尝试
{
字符串查询=选择id_boleto作为'Id',作为'Fecha',事件为'Evento',seccion为'Sección ',细丝为'Fila',asiento为'Asiento',bloque为'Bloque',总共为来自boletos的'Precio',其中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());
}

双击时,我将所需的值存入变量

  try 
{
MySqlCommand cmdc = new MySqlCommand(从boletos中选择seccion,fila,asiento,bloque,total 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);



$ b $ p
$ b

如果我需要这样做,关闭主窗体,重载构造函数解决方案:

  try 
{
this.Close();
Form1 f1 =新的Form1(辅助1,辅助2,辅助3,辅助4,辅助5);
f1.Show();
}

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

但是,由于我没有关闭主窗口,我怎样才能发送在关闭辅助表单时将aux1,aux2,aux3,aux4和aux5的值设置为激活表单?我希望我明确自己。谢谢。

解决方案

我可以建议两种方法:
1.使用ShowDialog()。这样,您可以将aux1,aux2 ...设置为属性,并从主窗体轻松获取数据:

  Boleto b = new Boleto(comboBox8.Text); 
b.ShowDialog();
this.aux1 = b.aux1;





  1. 传递this作为参考,保存这些辅助值的主要形式。所以你可以打电话了。重载你的构造函数。确保将Mainform的实例保存到某处。

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


然后您可以在关闭前设置它:

  form.aux1 = aux1; 


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.

解决方案

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;

这篇关于C#交换表单之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆