繁体   English   中英

如何将 windows 表单页面中的所有用户输入和标签复制到另一个以表格样式显示的 windows 表单页面?

[英]How can I copy all of the users input and labels in a windows form page to another windows form page that is displayed in a table style?

我正在使用 windows 表单应用程序 C# 并创建了一个带有标签和文本框以及复选框的表单(清单)。 在我的第二个表单 (test_results) 页面中,我创建了一个表格布局面板,其中包含第一个表单中的所有数据。 我的第一个表单上的“提交”按钮有。 我选择了 tablelayoutpanel,因为我要复制的表单是 excel 表单,并且用户希望它看起来与 excel 中的完全一样。 当我运行它时,结果会显示出来,但正如您所见,这仅适用于 tablelayoutpanel 上的 3 个文本框,实际上大约有 25 个。我只是不确定这是最佳实践和最简单的方法。 请参阅下面的 forms 代码

表格1

namespace IppInspectSheet
{
    public partial class checkList : Form
    {
        public checkList()
        {
            InitializeComponent();
        }

        private void checkList_Load(object sender, EventArgs e)
        {

        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            SetValueForText1 = textBox1.Text;
            SetValueForText2 = textBox2.Text;
            SetValueForText3 = textBox3.Text;
            test_results_Label t = new test_results_Label();
            t.Show();
            //Form3 f = new Form3();
            //f.Show();


        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        public static string SetValueForText1 = "";
        public static string SetValueForText2 = "";
        public static string SetValueForText3 = "";

表格2

namespace IppInspectSheet
{
    public partial class test_results_Label : Form
    {
        public test_results_Label()
        {
            InitializeComponent();
        }

        private void test_results_Label_Load(object sender, EventArgs e)
        {
            textBox1.Text = checkList.SetValueForText1;
            textBox2.Text = checkList.SetValueForText2;
            textBox3.Text = checkList.SetValueForText3;
        }
    }
}

Winforms 是 UI 框架,负责显示给定的数据结构并向该数据结构提供用户输入值。

您可以创建一个类型,它将在 Form1 中表示您的数据

public class MyData
{
    public string One { get; set; }
    public string Two { get; set; }
}

然后在 Form1 中将这些数据“绑定”到表单控件。 数据绑定和 Windows Forms

public class Form1
{
    private readonly MyData c;
    public Form1()
    {
        InitializeComponent();

        _data = new MyData();

        textbox1.Bindings.Add("Text", data, nameof(data.One), true, DataSourceUpdateMode .OnPropertyChanged);
        textbox2.Bindings.Add("Text", data, nameof(data.Two), true, DataSourceUpdateMode .OnPropertyChanged);
    }

    private void nextButton_Click(object sender, EventArgs e)
    {
        var form2 = new Form2(_data);
        form2.Show();
    }
}

在 Form2 的构造函数中添加参数,这样我们就可以向它传递数据了。

public class Form2
{
    private readonly MyData _data;
    public Form2(MyData data)
    {
        InitializeComponent();

        _data = data;

        label1.Bindings.Add("Text", data, nameof(data.One));
        label1.Bindings.Add("Text", data, nameof(data.Two));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM