【问题标题】:Passing values from one form to another form将值从一种形式传递到另一种形式
【发布时间】:2015-06-23 14:41:40
【问题描述】:

我有两个名为 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();
    }
}
}

一些截图:

我怎么能意识到这一点?

【问题讨论】:

  • 听起来一样,但是从一个页面移动到另一个页面的值的数量不同。
  • “价值的数量”并没有使问题本身成为一个不同的问题。您需要更详细地解释为什么那个问题没有解决这个问题,也没有 literally hundreds if not thousands of other similar questions 已经在 Stack Overflow 上。将数据从一个对象移动到另一个对象是一个基本的 C# 概念,之前已经讨论过很多次。
  • 请确保您正确关闭 SQL 连接和 SQL 命令。研究using 命令。

标签: c# forms winforms


【解决方案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")

【讨论】:

    猜你喜欢
    • 2015-06-06
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多