简体   繁体   English

输入一个值,它将以另一种形式显示另一个值

[英]Enter a value then it will show a different value in another form

So I need to have a feature where a number (ID) is typed in a textbox and as I click the button, I need it to have a display value corresponding to the value inputted that would show on a different window. 因此,我需要具有在文本框中键入数字(ID)的功能,并且在单击按钮时,我需要它具有与输入的值相对应的显示值,该值将在不同的窗口上显示。

在此处输入图片说明

I need to have the encircled one as a display value but I need to retain its value member which is an ID 我需要将圈出的一个作为显示值,但我需要保留其值成员(即ID)

This is my code at the first window: 这是我在第一个窗口中的代码:

private void btnApprove_Click(object sender, EventArgs e)
    {
        PassingText = txtEvent.Text;
        PackingApproval pa = new PackingApproval();
        pa.Show();
    }

This is my code for the other window: 这是我在另一个窗口中的代码:

private void PackingApproval_Load(object sender, EventArgs e)
    {
        txtBox4.Text = PackingList.PassingText;
    }

Note: the value I will show to the approval has a Corresponding value on the database 注意:我将显示给批准的值在数据库中具有对应的值

UPDATE: I have a code for calling the value from the database, but I cannot make it work since it needs to work between two windows 更新:我有一个用于从数据库调用值的代码,但是由于它需要在两个窗口之间工作,所以我无法使其工作

        private void PackingApproval_Load(object sender, EventArgs e)
    {
        txtBox4.SelectedText = "EventID";

        SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=Juan Carlo SCM;Persist Security Info=True;User ID=sa;Password=benilde");
        con.Open();
        SqlCommand sc = new SqlCommand("SELECT EventName FROM Event_Table WHERE EventID=@EventID", con);
        SqlDataReader reader;

        reader = sc.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("EventID", typeof(string));
        dt.Columns.Add("EventName", typeof(string));
        dt.Load(reader);

        txtBox4.SelectedText = "EventID";
        txtBox4.Text = txtBox4.Tag.ToString();

        con.Close();
    }

You can pass the value by these ways: 您可以通过以下方式传递值:

  • By constructor 按构造函数
public partial class PackingApproval : Form
{
    public PackingApproval(string enterValue)
    {
        InitializeComponent();
        txtBox4.Text = enterValue;
    }
}

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

    private void btnApprove_Click(object sender, EventArgs e)
    {
        var text = txtEvent.Text;
        PackingApproval pa = new PackingApproval(text);
        pa.Show();
    }
}
  • By Property of destination object 按目标对象的属性
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnApprove_Click(object sender, EventArgs e)
    {
        var text = txtEvent.Text;
        PackingApproval pa = new PackingApproval();
        pa.SetPackagingApprovalText = text;
        pa.Show();
    }
}

public partial class PackingApproval : Form
{
    private string _setPackagingApprovalText;
    public string SetPackagingApprovalText
    {
        get
        {
            return _setPackagingApprovalText;
        }
        set
        {
            txtBox4.Text = value;
            _setPackagingApprovalText = value;
        }
    }
    public PackingApproval()
    {
        InitializeComponent();
    }
}
  • By field in destination object + FormLoad 按目标对象中的字段+ FormLoad
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnApprove_Click(object sender, EventArgs e)
    {
        var text = txtEvent.Text;
        PackingApproval pa = new PackingApproval();
        pa.SetPackagingApprovalText = text;
        pa.Show();
    }
}
public partial class PackingApproval : Form
{
    public string SetPackagingApprovalText;
    public PackingApproval()
    {
        InitializeComponent();
    }

    private void PackingApproval_Load(object sender, EventArgs e)
    {
        txtBox4.Text = SetPackagingApprovalText;
    }
}

for your database problem: 对于您的数据库问题:

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

    private void btnApprove_Click(object sender, EventArgs e)
    {
        var text = txtEvent.Text;
        PackingApproval pa = new PackingApproval();
        pa.SetPackagingApprovalText = text;
        pa.Show();
    }
}

public partial class PackingApproval : Form
{
    public string SetPackagingApprovalText;
    public PackingApproval()
    {
        InitializeComponent();
    }

    private void PackingApproval_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=Juan Carlo SCM;Persist Security Info=True;User ID=sa;Password=benilde");
        con.Open();
        SqlCommand sc = new SqlCommand("SELECT EventName FROM Event_Table WHERE EventID=@EventID", con);

        sc.Parameters.Add(new SqlParameter("EventID", int.Parse(SetPackagingApprovalText)));
        SqlDataReader reader;
        reader = sc.ExecuteReader();
        if (reader.HasRows)
        {
            reader.Read();
            txtBox4.Text = reader["EventName"].ToString();
        }

        reader.Close();
        con.Close();
    }
}

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

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