如何从以前的表单中检索数据?

时间:2017-07-10 07:06:20

标签: c# winforms

我想在用户以“操作员”身份登录时禁用表单中的按钮,并在他是“admin”时启用它。我有3种形式:第一种=> 登录页面我有“用户”和“密码”参数,第二个=> 菜单我顶部有标签,说明当前连接的人(操作员或管理员),第三个=> 修复 页面

问题是,当我点击第三张表格上的“返回”按钮,然后我回到第二张表格时,按钮启用。因此,即使“操作员”也可以通过这个故障/错误访问它。

我的登录页面的代码:

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    public void button2_Click(object sender, EventArgs e)
    {
        Main form2 = new Main(this);
        try
        {
            SqlConnection con = new SqlConnection("Server=(local); Database= Seica_Takaya;Integrated Security = SSPI; ");
            SqlDataAdapter sda = new SqlDataAdapter("SELECT count(*) FROM Loginmdp WHERE util='" + textBox1.Text + "' AND mdp='" + textBox2.Text + "'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            if (dt.Rows[0][0].ToString() == "1")
            {
                MessageBox.Show("Connexion réussie ! ");
                this.Hide();
                form2.Show();

                con.Close();
            }
            else
            {
                MessageBox.Show("Identifiant / Mot de passe faux. Veuillez resaisir votre identifiant et mot de passe.");

                con.Close();
            }
        }
        catch(SqlException ex)
        {
            MessageBox.Show("SQL EXCEPTION : " + ex.Message);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

以下是我的菜单代码:

public partial class Main : Form
{
    private Login login;

    public Main()
    {
        InitializeComponent();
    }

    public Main(Login login)
    {
        InitializeComponent();
        label3.Text = login.textBox1.Text;

        this.login = login;

        if (login.textBox1.Text == "admin" && login.textBox2.Text == "root")
        {
            button2.Enabled = true;
        }
        else
        {
            button2.Enabled = false;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Repair form3 = new Repair();
        this.Hide();

        form3.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Admin form4 = new Admin();
        this.Hide();
        form4.Show();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        new Login().Show();
        this.Hide();
        MessageBox.Show("Vous êtes déconnecté");
    }
}

维修页面的代码:

public partial class Repair : Form
{
    public Repair()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");

        maConnexion.Open();

         string Var1 = textBox1.Text;
         SqlCommand command = maConnexion.CreateCommand();
         command.Parameters.AddWithValue("@BoardName", Var1);
         command.Parameters.AddWithValue("@Machine", Var1);
         command.Parameters.AddWithValue("@SerialNum", Var1);
         command.Parameters.AddWithValue("@FComponent", Var1);
        command.CommandText = "SELECT * FROM FailOnly WHERE BoardName=@BoardName OR Machine=@Machine OR SerialNum=@SerialNum OR FComponent=@FComponent AND ReportingOperator != NULL";
         SqlDataAdapter sda = new SqlDataAdapter(command);
         DataSet ds = new DataSet();
         sda.Fill(ds);
         dataGridView1.DataSource = ds.Tables[0];
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Hide();
        Main ff = new Main();
        ff.Show();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
       /**
        BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView1.DataSource;
        bs.Filter = string.Format("BoardName LIKE '%{0}%'",textBox1.Text.Replace("'","''"));
        dataGridView1.DataSource = bs;**/
    }
}

现在一步一步地拍照:

First Second Third

1 个答案:

答案 0 :(得分:0)

最简单的方法是定义'视图模型'类:

public class ViewModel
{
    private static ViewModel viewModel;

    public static ViewModel Instance
    {
        get { return viewModel ?? (viewModel = new ViewModel()); }
    }

    public bool IsAdmin { get; set; }
}

将其初始化为全局变量(或您最喜欢的任何方式),并在主窗体中使用它:

public Main()
{
    InitializeComponent();
    label3.Text = login.textBox1.Text;

    button2.Enabled = ViewModel.Instance.IsAdmin;        
}

应在登录表单中设置IsAdmin属性:

....
    if (textBox1.Text == "admin" && textBox2.Text == "root")
    {
        ViewModel.Instance.IsAdmin = true;
    }