我已经尝试了下面的方法
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(...)) -将用户名和/或密码作为参数发送。
发布于 2013-12-29 14:59:58
您可以向UserForm添加获取参数的构造函数,并在构造函数中提供一些具有该对象的对象。
您还可以在Userform中添加getter/setter并更新要传递的信息。
发布于 2013-12-29 16:25:37
例如,您可以简单地使用属性:
Form2:
public string UserText { get; set;}
...
private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "")
    {
        MessageBox.Show("Please enter keyword to search");
    }
    else 
    {
        UserText = textBox1.Text; // set the Text
    }Form1:
private void button5_Click(object sender, EventArgs e)
{            
    Form2 form2 = new Form2();
    form2.ShowDialog();   //open form2-search form  
    string text = from2.UserText; get the Text
    ....https://stackoverflow.com/questions/20822857
复制相似问题