C# 组合框中静态插入的项在按钮单击多次时加倍

C# 组合框中静态插入的项在按钮单击多次时加倍,c#,visual-studio,winforms,C#,Visual Studio,Winforms,我有一个组合框,其中插入了以下项目 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 valu

我有一个组合框,其中插入了以下项目

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");
    }
但是,当用户多次单击按钮时,该值会加倍,或者该值会发生任何不必要的事情

点击按钮是正确的

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

有人能帮我吗?

只有在页面加载不是
回发时才调用
设置操作下拉列表()

 if (!IsPostBack) {
    SetOperationDropDown();
 }

在用户单击按钮时禁用该按钮,将CMB Operations SelectItem重置为“不做任何事情”值,在处理完请求后重新启用该按钮。

这是在WinForms下标记的,因此我认为这里不适用回发。查看您的
btnStartAnalysis\u Click
方法,我没有看到它调用
SetOperationDropDown
。尝试进入调试模式,并在
设置操作下拉列表中设置断点。然后单击按钮,查看是否达到断点。如果是,请参考堆栈跟踪以查看调用
SetOperationDropDown
的位置

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();
}
如果WinForms标记不正确,并且您实际使用的是WebForms/ASP.NET,请按照Stefanvds和Marcel的建议执行。但我认为重要的是要弄清楚
SetOperationDropDown
在哪里被错误调用

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

可能会有一些小的语法错误,因为在填充组合框之前,我没有使用VS.

,或者做了一个糟糕的hack并清除了项:
cmbcoperations.items.clear()
:)清除它不是一个好主意,因为在这种情况下,“选定”值将被“遗忘”
IsPostBack
在winforms应用程序中不是有效的概念。什么值会加倍?你能更详细地解释一下什么是“任何不想要的东西”吗