C#Windows窗体,Combobox MouseClick事件多次被触发

时间:2013-05-09 12:18:43

标签: c# winforms mouseclick-event

我有一个带有许多控件的窗体。其中很小一部分是登录到SQL服务器并获取数据库名称列表并将该集合分配给组合框。

    private void InitializeComponent()
    {

        //...
        //...
        this.ServerTB = new System.Windows.Forms.TextBox();
        this.UserNameTB = new System.Windows.Forms.TextBox();
        this.PasswordTB = new System.Windows.Forms.TextBox();
        //...
        //...

        this.ServerTB.TextChanged += new System.EventHandler(this.OnSQLServerChanged); 
        this.UserNameTB.TextChanged += new System.EventHandler(this.OnSQLServerChanged);     
        this.PasswordTB.TextChanged += new System.EventHandler(this.OnSQLServerChanged);     


        this.DatabaseCmbBox = new System.Windows.Forms.ComboBox();
        //...
        //...
        this.DatabaseCmbBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.DatabaseCmbBox_Click);

        //....

    }

    private void DatabaseCmbBox_Click(object sender, MouseEventArgs e)
    {
        MessageBox.Show(sender.GetType().ToString());

        this.Cursor = Cursors.IBeam;

        List<string> dbList = new List<string>();

        dbList = GetDatabaseList();

        DatabaseCmbBox.DataSource = dbList;

        DatabaseCmbBox.SelectedIndex = -1;


        if (dbList.Count > 0)
        {
            DatabaseCmbBox.MouseClick -= DatabaseCmbBox_Click;
        }


        DatabaseCmbBox.Focus();

        this.Cursor = Cursors.Default;
    }

    protected List<string> GetDatabaseList()
    {
        List<string> list = new List<string>();

        string conString = "server=" + ServerTB.Text + ";uid=" + UserNameTB.Text + ";pwd=" + PasswordTB.Text + "; database=master";

        try
        {

            using (SqlConnection con = new SqlConnection(conString))
            {
                con.Open();

                using (SqlCommand cmd = new SqlCommand("select name from sys.databases where name not in ('master', 'model', 'tempdb', 'msdb') ", con))
                {
                    using (IDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            list.Add(dr[0].ToString());
                        }

                        dr.Close();
                    }

                    cmd.Dispose();
                }                    

                con.Close();
            }
        }
        catch(SqlException ex)
        {
            DatabaseCmbBox.MouseClick -= DatabaseCmbBox_Click;
            MessageBox.Show(ex.Message);
            ServerTB.Focus();
        }

        return list;

    }

    private void OnSQLServerChanged(object sender, EventArgs e)
    {
        DatabaseCmbBox.MouseClick += DatabaseCmbBox_Click;
    }

我在获取数据库列表时没有问题。这需要大约10秒钟。通过事件处理程序中的消息框,我发现MouseClick事件,没有明显的原因,被解雇了38次。即使我使用Enter事件或Click事件而不是MouseClick事件,也会发生同样的事情。

为什么会发生这种情况,以及如何使用这些事件?

提前致谢, RPS。

2 个答案:

答案 0 :(得分:3)

如果您想获取数据库列表,为什么不在Combobox值更改事件中这样做?

无论如何,我看到你在做什么

private void OnSQLServerChanged(object sender, EventArgs e)
    {
        DatabaseCmbBox.MouseClick += DatabaseCmbBox_Click;
    }

这应该可以解决您的问题

private void OnSQLServerChanged(object sender, EventArgs e)
    {
DatabaseCmbBox.MouseClick -= DatabaseCmbBox_Click;
        DatabaseCmbBox.MouseClick += DatabaseCmbBox_Click;
    }

答案 1 :(得分:1)

这是一个潜在错误:您应该只订阅DatabaseCmbBox.MouseClick一次,而不是每次OnSQLServerChanged订阅。相应地更正您的代码,并修复多次点击事件的问题:

private void OnSQLServerChanged(object sender, EventArgs e)
    {
        DatabaseCmbBox.MouseClick += DatabaseCmbBox_Click;
    }