在winform按钮需要点击两次以进行触发事件

时间:2010-12-03 10:54:02

标签: c# winforms

我在click事件中有一个buttton我正在调用一个函数。我已经将accept按钮属性设置为该按钮。但是它不会在单击时触发事件。

 private void btnConnect_Click(object sender, EventArgs e)
 {
        try
        {
            //Function call for validating the textboxes entry
            ValidateForm();

            if(lblMessage.Text != string.Empty)
            {
                //Function call for binding the dropdown with all DB names 
                BindDBDropDown();

                //Function call for binding the operation names in dropdown 
                SetOperationDropDown();
            }
            else
            {
                //Else give the error message to user
                lblMessage.Text = "Invalid Credentials";
            }
        }
        catch(Exception ex)
        {
            //All the exceptions are handled and written in the EventLog.
            EventLog log = new EventLog("Application");
            log.Source = "MFDBAnalyser";
            log.WriteEntry(ex.Message);
        }
    }


public void BindDBDropDown()
{

        SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
        objConnectionString.DataSource = txtHost.Text;
        objConnectionString.UserID = txtUsername.Text;
        objConnectionString.Password = txtPassword.Text;

        SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString);

        //If connected then give this message to user
        lblMessage.Visible = true;
        lblMessage.Text = "You are connected to the SQL Server....";

        try
        {
            //To Open the connection.
            sConnection.Open();

            //Query to select the list of databases.
            string selectDatabaseNames = @"SELECT 
                                                NAME 
                                             FROM 
                                                [MASTER]..[SYSDATABASES]";

            //Create the command object
            SqlCommand sCommand = new SqlCommand(selectDatabaseNames, sConnection);

            //Create the data set 
            DataSet sDataset = new DataSet("master..sysdatabases");

            //Create the dataadapter object
            SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectDatabaseNames, sConnection);
            sDataAdapter.TableMappings.Add("Table", "master..sysdatabases");

            //Fill the dataset
            sDataAdapter.Fill(sDataset);

            //Bind the database names in combobox
            DataViewManager dsv = sDataset.DefaultViewManager;

            //Provides the master mapping between the sourcr table and system.data.datatable
            cmbDatabases.DataSource = sDataset.Tables["master..sysdatabases"];
            cmbDatabases.DisplayMember = "NAME";
            cmbDatabases.ValueMember = ("NAME");
        }
        catch(Exception ex)
        {
            //All the exceptions are handled and written in the EventLog.
            EventLog logException = new EventLog("Application");
            logException.Source = "MFDBAnalyser";
            logException.WriteEntry(ex.Message);
            MessageBox.Show ("Login Failed!!", "Error Occured");
        }
        finally
        {
            //If connection is not closed then close the connection
            if(sConnection.State != ConnectionState.Closed)
            {
                sConnection.Close();
            }
        }
    }

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

呃,在第一次点击时,lblMessage是否包含任何文字?

因为如果没有,第一次运行它时,它将无法通过条件测试并将字符串“Invalid Credentials”插入到标签中。然后,第二次运行它时,它将通过条件测试并按照您的预期调用BindDBDropDown方法。

具体来说,代码的这一部分:

if(lblMessage.Text != string.Empty)
{
    //Function call for binding the dropdown with all DB names 
    BindDBDropDown();

    //Function call for binding the operation names in dropdown 
    SetOperationDropDown();
}
else
{
    //Else give the error message to user
    lblMessage.Text = "Invalid Credentials";
}

我假设您要么检查用户输入其凭据的文本框的内容是否为空,或者您要确保lblMessage中当前未显示错误消息。确保您的代码准确反映您的意图!