首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将数据从表单1移至其他表单

将数据从表单1移至其他表单
EN

Stack Overflow用户
提问于 2013-12-29 14:50:06
回答 3查看 306关注 0票数 0

我已经尝试了下面的方法

Getting values from two or more forms C# Sharing a variable between two winforms Get value from parentformpassing username to form but username returns null c#

但我不能让我的工作,因为我会得到一些随机的参数错误。

从图片中,我只想把登录表单上的用户名放到用户表单上的label1中。

但也能够在以后使用其他形式。

有人能帮上忙吗?

登录表单代码:

代码语言:javascript
运行
复制
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;
        }
    }

===========================

在这里,我试过这个,但是这个,不起作用..

EN

Stack Overflow用户

发布于 2013-12-29 14:56:11

为第二个表单创建一个构造器,并在其创建过程中(调用new Form2(...)) -将用户名和/或密码作为参数发送。

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20822857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档