我是C#的新手,我正面临这个问题。我有2个表单,一个是登录表单,另一个是登录连接后的表单。我想从登录表单的用户文本框中获取用户名,并在用户连接时将其设置在第二个表单中的Label上。喜欢"欢迎 User1 "并设置一个计时器来启动。提前感谢任何建议。 以下是我的登录代码
namespace KPI_TRANSPORT
{
public partial class LOGIN : Form
{
public LOGIN()
{
InitializeComponent();
}
private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar==(char)13)
textBox2.Focus();
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
btnLogin.PerformClick();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(txtUsername.Text))
{
MessageBox.Show("SVP Entrée votre nom d'utilisateur","Message",MessageBoxButtons.OK,MessageBoxIcon.Warning);
txtUsername.Focus();
return;
}
try {
KPI_DATABASEDataSet5TableAdapters.LoginTableAdapter login = new KPI_DATABASEDataSet5TableAdapters.LoginTableAdapter();
KPI_DATABASEDataSet5.LoginDataTable dt = login.GetDataByUsernamePassword(txtUsername.Text,textBox2.Text);
if (dt.Rows.Count > 0)
{
//MessageBox.Show("Connecté avec succès", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
PagePrin f1 = new PagePrin();
LOGIN lg = new LOGIN();
lg.Closed += (s, args) => lg.Close();
f1.Show();
}
else
{
MessageBox.Show("Nom d'utilisateur ou Mot de pass incorrect", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUsername.Text = "";
textBox2.Text = "";
txtUsername.Focus();
}
}
catch (Exception ex)
{ MessageBox.Show("Error"+ex); }
}
private void ExitBtn_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
this.Close();
}
private void LOGIN_Load(object sender, EventArgs e)
{
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
//UsernameText
txtUsername.AutoSize = false;
txtUsername.Height = 40;
txtUsername.Width = 200;
//password Text
textBox2.AutoSize = false;
textBox2.Height = 40;
textBox2.Width = 200;
}
private void check_Text_content()
{
if (txtUsername.Text !=string.Empty && textBox2.Text !=string.Empty)
{ btnLogin.Enabled=true; }
else if (txtUsername.Text==string.Empty)
{ btnLogin.Enabled=false; }
else if (textBox2.Text==string.Empty)
{ btnLogin.Enabled = false; }
}
}
}
答案 0 :(得分:0)
只需将其传递给另一种形式:
public partial class LOGIN : Form
{
public Login()
{
InitializeComponent();
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
string someString = "TestingString";
int someInt = 123;
SecondFrom sf = new SecondForm(someString, someInt);
sf.Show();
}
另一种形式:
public partial class SecondForm : Form
{
private string myString;
private int myInt;
public SecondForm(string passingString, int passingInt)
{
myString = passingString;
myInt = passingInt;
}
}
像这样,您已将someString
和someInt
的值从LOGIN
表单传递到SecondForm
的{{1}}和myString