因此,我需要具有一个在文本框中键入数字(ID)的功能,并且当我单击按钮时,我需要它具有与输入的值相对应的显示值,该值将在不同的窗口中显示。 / p>
我需要将圈出的一个作为显示值,但我需要保留其值成员(即ID)
这是我在第一个窗口中的代码:
private void btnApprove_Click(object sender, EventArgs e)
{
PassingText = txtEvent.Text;
PackingApproval pa = new PackingApproval();
pa.Show();
}
这是我在其他窗口中的代码:
private void PackingApproval_Load(object sender, EventArgs e)
{
txtBox4.Text = PackingList.PassingText;
}
注意:我将显示给批准的值在数据库中具有相应的值
更新:我有一个用于从数据库中调用值的代码,但是由于它需要在两个窗口之间工作,所以我无法使其工作
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();
}
答案 0 :(得分:3)
您可以通过以下方式传递值:
- 通过构造函数
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();
}
}
- 按目标对象的属性
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();
}
}
- 通过目标对象+ 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;
}
}
针对您的数据库问题:
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();
}
}