我已经尝试了下面的方法
Getting values from two or more forms C# Sharing a variable between two winforms Get value from parentform和passing username to form but username returns null c#
但我不能让我的工作,因为我会得到一些随机的参数错误。

从图片中,我只想把登录表单上的用户名放到用户表单上的label1中。
但也能够在以后使用其他形式。
有人能帮上忙吗?
登录表单代码:
public partial class Login : Form
{
UserForm _userform = new UserForm();
Admin _Adminform = new Admin();
public Login()
{
InitializeComponent();
}
private void loginscs_Click(object sender, EventArgs e)
{
try
{
string userNameText = txtUser.Text;
string passwordText = txtPass.Text;
string isAdmin = "yes";
string isNotAdmin = "no";
if (!(string.IsNullOrEmpty(txtUser.Text)) && !(string.IsNullOrEmpty(txtPass.Text)))
{
SqlConnection SCScon = new SqlConnection();
SCScon.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
SqlCommand cmd = new SqlCommand("SELECT ISNULL(SCSID, '') AS SCSID, ISNULL(SCSPass,'') AS SCSPass, ISNULL(isAdmin,'') AS isAdmin FROM SCSID WHERE SCSID='" + txtUser.Text + "' and SCSPass='" + txtPass.Text + "'", SCScon);
SCScon.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
this.CompareStrings(dr["isAdmin"].ToString(), isAdmin))
{
MessageBox.Show("Hello " + txtUser.Text, "Admin", MessageBoxButtons.OK, MessageBoxIcon.Information);
_Adminform.Show();
this.Hide();
}
else if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
this.CompareStrings(dr["isAdmin"].ToString(), isNotAdmin))
{
MessageBox.Show("Welcome " + txtUser.Text, "User");
_userform.Show();
this.Hide();
}
}
else
{
MessageBox.Show("Wrong ID/Pass");
}
SCScon.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("error2" + ex);
}
}
private void Login_Load(object sender, EventArgs e)
{
}
private bool CompareStrings(string string1, string string2)
{
return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
}
}===========================
在这里,我试过这个,但是这个,不起作用..

发布于 2013-12-29 14:56:11
为第二个表单创建一个构造器,并在其创建过程中(调用new Form2(...)) -将用户名和/或密码作为参数发送。
https://stackoverflow.com/questions/20822857
复制相似问题