2010-12-03 420 views
0

我有一個按鈕事件,我正在調用一個函數的按鈕。我已經設置了該按鈕的接受按鈕屬性。但它不是單發事件單擊。在winform按鈕需要點擊兩次擊發事件

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

你在哪裏註冊事件處理程序?你可以發佈該代碼嗎? – Oded 2010-12-03 10:56:11

回答

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中顯示錯誤消息。確保你的代碼準確地反映你的意圖!

相關問題