2010-11-26 10 views
0

ich eine Combobox, dieDie statisch eingesetzt Elemente in Combobox verdoppelt auf den Knopf klicken für mehr als einmal

public void SetOperationDropDown() 
    { 
    //ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-. 
    cmbOperations.SelectedItem = "-SELECT OPERATIONS-"; 

    //This is for adding four operations with value in operation dropdown 
    cmbOperations.Items.Insert(0, "PrimaryKeyTables"); 
    cmbOperations.Items.Insert(1, "NonPrimaryKeyTables"); 
    cmbOperations.Items.Insert(2, "ForeignKeyTables"); 
    cmbOperations.Items.Insert(3, "NonForeignKeyTables"); 
    cmbOperations.Items.Insert(4, "UPPERCASEDTables"); 
    cmbOperations.Items.Insert(5, "lowercasedtables"); 
    } 

eingefügt folgende Elemente hat aber, wenn der Benutzer klickt auf die Schaltfläche mehr als einmal der Wert verdoppelt wird oder eine Unerwünschtes geschieht mit dem Wert.

auf die Schaltfläche klicken ist

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

    //Variable to store server address 
    string localHost = "192.168.10.3"; 

    //Variable to store userId and password of the database 
    string logInDetails = "gp"; 

    try 
     { 
     //Checking for the Valid entries in textboxes if all entries are correct then call functions accordingly 
     if((txtPassword.Text == logInDetails) && (txtUsername.Text == logInDetails) && (txtHost.Text == localHost)) 
      { 

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

      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); 
     } 
    } 

Kann mir jemand helfen?

+0

Welcher Wert wird verdoppelt? Und können Sie etwas genauer erklären, was "jede unerwünschte Sache" ist? –

+0

die Elemente in den CMBOperations – Srivastava

Antwort

1
public void SetOperationDropDown() 
{ 
if(CmbOperations.Items.Count == 0) 
{ 
//ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-. 
cmbOperations.SelectedItem = "-SELECT OPERATIONS-"; 
//This is for adding four operations with value in operation dropdown 
cmbOperations.Items.Insert(0, "PrimaryKeyTables"); 
cmbOperations.Items.Insert(1, "NonPrimaryKeyTables"); 
cmbOperations.Items.Insert(2, "ForeignKeyTables"); 
cmbOperations.Items.Insert(3, "NonForeignKeyTables"); 
cmbOperations.Items.Insert(4, "UPPERCASEDTables"); 
cmbOperations.Items.Insert(5, "lowercasedtables"); 


} 
else 
{ 
int? cbSelectedValue = null; 
if(!string.IsNullOrEmpty(cmbOperations.SelectedValue)) 
cbSelectedValue = convert.toInt32(cmbOperations.SelectedValue); 
} 
//load your combo again 
if(cbSelectedValue != null) 
cmbOperations.SelectedValue = cbSelectedValue.ToString(); 
} 

Es kann kleine Syntaxfehler geben, da ich VS nicht verwendet habe.

0

nur die SetOperationDropDown() nennen, wenn die Last Ihrer Seite ist ein nicht postback

if (!IsPostBack) { 
    SetOperationDropDown(); 
} 
+0

Oder machen Sie einen beschissenen Hack und löschen Sie Elemente vor dem Auffüllen der ComboBox: 'cmbOperations.Items.Clear()' :) –

+0

Clearing es ist keine gute Idee, denn in diesem Fall, der 'ausgewählte' Wert wird 'vergessen' – Stefanvds

+1

'IsPostBack' wird kein gültiges Konzept in einer Winforms-Anwendung sein. –

0

die Taste deaktivieren, wenn der Benutzer darauf klickt, setzen Sie das cmbOperations SelectItem zu Ihrem „nichts tun“ Wert, reaktivieren die Taste, wenn Sie haben die Bearbeitung der Anfrage abgeschlossen.

0

Dies ist unter WinForms getaggt, so dass Post-Rückseiten hier nicht anwendbar sind. In Ihrer btnStartAnalysis_Click Methode sehe ich es nicht SetOperationDropDown aufrufen. Versuchen Sie, in den DEBUG-Modus zu wechseln und setzen Sie einen Unterbrechungspunkt in SetOperationDropDown. Klicken Sie dann auf Ihren Button und sehen Sie, ob Ihr Breakpoint getroffen wurde. Wenn dies der Fall ist, sehen Sie in Ihrem Stack-Trace nach, woher SetOperationDropDown aufgerufen wird.

Wenn das WinForms-Tag falsch ist und Sie tatsächlich WebForms/ASP.NET verwenden, dann tun Sie, was Stefanvds und Marcel vorgeschlagen haben. Aber ich denke, es ist wichtig herauszufinden, wo SetOperationDropDown falsch von aufgerufen wird.

Verwandte Themen