C# 将值从一个窗体传递到另一个窗体

C# 将值从一个窗体传递到另一个窗体,c#,forms,winforms,C#,Forms,Winforms,我有两个表格,分别是Form1和Form2: Form1是SQL表中插入的一些值的列表 Form2是表中插入值的验证 当我点击Form1按钮时,这将显示Form2并在表中插入值,而且Form1中textBox中的任何输入都应写回Form2 textBox 我有下面的代码,但它不工作 ////Form1 Code using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat

我有两个表格,分别是Form1和Form2:

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

当我点击Form1按钮时,这将显示Form2并在表中插入值,而且Form1中textBox中的任何输入都应写回Form2 textBox

我有下面的代码,但它不工作

////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();
    }
}
}
一些截图:


我怎么能意识到这一点呢?

您可以像对待C#中的任何类一样,通过构造函数传递值

否则,对于相反的方式,在
Form2
中有一个实例属性。在
表单2中设置该属性。销毁表单之前,请从
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")

可能的重复听起来是一样的,但从一个页面移动到另一个页面的值的数量不同。“值的数量”本身并不会使问题成为不同的问题。您需要更详细地解释为什么这个问题没有被那个问题解决,也没有解释已经存在的堆栈溢出。将数据从一个对象移动到另一个对象是一个基本的C#概念,以前已经多次提到过。请确保正确关闭SQL连接和SQL命令。使用
命令研究