首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在winform中,触发事件需要单击两次按钮

在winform中,触发事件需要单击两次按钮
EN

Stack Overflow用户
提问于 2010-12-03 18:54:02
回答 1查看 1.1K关注 0票数 2

我在click事件上有一个按钮,我调用它的一个函数。我已经将accept button属性设置为该button.But,它不会在单击时触发事件。

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

有人能帮我吗??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-03 18:59:15

Uh,第一次点击时, lblMessage 是否包含任何文本?

因为如果没有,在您第一次运行它时,它将导致条件测试失败,并将字符串"Invalid Credentials“插入到标签中。然后,第二次运行它时,它将通过条件测试,并如您所期望的那样调用BindDBDropDown方法。

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

代码语言:javascript
复制
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中。确保您的代码准确地反映了您的意图!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4344825

复制
相关文章

相似问题

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