将值从一种形式传递到另一种形式

时间:2015-04-16 23:07:46

标签: c# forms winforms

我有两个名为Form1和Form2的表单:

Form1是SQL中插入表中的一些值的列表 Form2是对表中插入的值的验证。

当我单击Form1按钮时,这将显示Form2并在表中插入值,同样,Form 1中textBox中的任何输入都应写回Form2 TextBoxes。

我有下面的代码,但它不起作用。

////Form1 Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 F2= new Form2();
        F2.Show();
        this.Hide();
        SqlConnection con = new SqlConnection("Data Source=MXPEDAMAP401;Initial Catalog=VentaCajas;User ID=sa;Password=1TservicesMX");
        con.Open();
        SqlCommand sc = new SqlCommand("insert into DatosGenerales values('" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','"+ textBox1.Text + "','" + listBox1.Text + "','" + textBox5.Text + "','" + listBox2.Text + "', getdate());",con);
        int o=sc.ExecuteNonQuery();
        MessageBox.Show("Verifica que los datos esten correctos");
        con.Close();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: esta línea de código carga datos en la tabla 'ventaCajasDataSet1.LugarEntrega' Puede moverla o quitarla según sea necesario.
        this.lugarEntregaTableAdapter.Fill(this.ventaCajasDataSet1.LugarEntrega);
        // TODO: esta línea de código carga datos en la tabla 'ventaCajasDataSet.TipoContrato' Puede moverla o quitarla según sea necesario.
        this.tipoContratoTableAdapter.Fill(this.ventaCajasDataSet.TipoContrato);

    }
}
}



////Form2 Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

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

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=MXPEDAMAP401;Initial Catalog=VentaCajas;User ID=sa;Password=1TservicesMX");
        con.Open();
        SqlCommand sc = new SqlCommand("delete DatosGenerales where No_Empleado+Tipo_Contrato = '';('" + textBox4.Text + "," + textBox5.Text + "');", con);
        int o = sc.ExecuteNonQuery();
        Form1 F1 = new Form1();
        F1.Show();
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("¡Gracias!");
        this.Close();
    }
}
}

一些截图:

enter image description here enter image description here

我怎么能意识到这一点?

1 个答案:

答案 0 :(得分:1)

您可以通过构造函数传递值,因为您总是使用C#中的任何类。

否则,反过来,Form2中有一个实例属性。在Form2代码中设置该属性。在销毁表单之前,请从Form1中阅读。

public partial class Form2 : Form
{
    public int MyValue { get; set; }

    public Form2()
    {

        // somwhere in this code:
        MyValue = 3;

然后:

Form2 F2= new Form2();
F2.Show();
// ...

int myValue = F2.MyValue; // form 1 can just read the value of form 2 (which is "3")