我的项目中有两种形式(Form1 & Form2)。将Form1文本框值传递给Form2按钮
在Form1上,我有两个文本框,如“Login”表单,其中用户需要输入用户名(textboxUsername)&密码(textboxPassword)。当用户登录时,Form2弹出。
现在在Form2中我有一个button1和dataGridView1。当我按下按钮1我想显示所选用户,我在Form1中输入(textboxUsername)
我想根据他的用户名获取该用户,并保存在SQL Server中。
窗体2 Button1的代码:
private void button1_Click(object sender, EventArgs e)
{
string constring = @"Data Source=V-K\;Initial Catalog=ATMKlientet;Integrated Security=True";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(" select * from Clients ;", conDataBase);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmdDataBase;
dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
在这种情况下,在这里我想打电话给Form1中textBoxUsername的一部分,在Form2的是:
(" select * from Clients where Username='" + this.textBoxUsername + "';", conDataBase);
此问题的基本答案是不要。有一个用户类并通过事件链接起来(一个接口也会很好),那么你最终将得到两个表单“知道”登录界面而彼此没有关系。 –