C#Winforms App需要两次点击Button_Click事件

时间:2017-02-07 21:54:11

标签: c# winforms events onclick click

按钮需要两次点击才能启动事件。这是一个图像和代码。有一个组合框触发了具有不同项目的按钮,但是当我单击按钮在页面上的面板中显示项目时,我必须单击它两次以便它可以触发事件。通过两次点击一次选择一个项目后,每次点击它都可以点击一下,就像它应该的那样。

Here is the image of the combobox which triggers the button

还有代码:

namespace Carbon
{
public partial class ucAnaliza : MetroFramework.Controls.MetroUserControl
{

    static ucAnaliza _instance;

    public static ucAnaliza Instance3
    {
        get
        {
            if (_instance == null)
                _instance = new ucAnaliza();
            return _instance;
        }
    }

    public MetroFramework.Controls.MetroPanel MetroAnaliza
    {
        get { return mPanelAnaliza; }
        set { mPanelAnaliza = value; }
    }

    public ucAnaliza()
    {
        InitializeComponent();
    }

    private void ucAnaliza_Load(object sender, EventArgs e)
    {

    }

    private void mPotvrdiElementi_Click(object sender, EventArgs e)
    {
        switch (((ComboBox)mDropAnaliza).SelectedItem.ToString())
        {
            case "Главна рамка":
                _instance = this;
                ucGlavna uc = new ucGlavna();
                uc.Dock = DockStyle.Bottom;
                mPanelAnaliza.Controls.Add(uc);
                break;

            case "Челна рамка":
                _instance = this;
                ucCelna uc2 = new ucCelna();
                uc2.Dock = DockStyle.Bottom;
                mPanelAnaliza.Controls.Add(uc2);
                break;

            case "Подолжна рамка":
                _instance = this;
                ucPodolzna uc3 = new ucPodolzna();
                uc3.Dock = DockStyle.Bottom;
                mPanelAnaliza.Controls.Add(uc3);
                break;
        }
        }
}

}

以下是设计师提供的按钮代码:

        // mPotvrdiElementi
        // 
        this.mPotvrdiElementi.BackColor = System.Drawing.Color.Transparent;
        this.mPotvrdiElementi.CausesValidation = false;
        this.mPotvrdiElementi.Cursor = System.Windows.Forms.Cursors.Hand;
        this.mPotvrdiElementi.ForeColor = System.Drawing.SystemColors.MenuBar;
        this.mPotvrdiElementi.Image = global::Carbon.Properties.Resources.Checked_Checkbox_24px;
        this.mPotvrdiElementi.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
        this.mPotvrdiElementi.ImageSize = 24;
        this.mPotvrdiElementi.Location = new System.Drawing.Point(758, 34);
        this.mPotvrdiElementi.Name = "mPotvrdiElementi";
        this.mPotvrdiElementi.Size = new System.Drawing.Size(80, 25);
        this.mPotvrdiElementi.Style = MetroFramework.MetroColorStyle.Orange;
        this.mPotvrdiElementi.TabIndex = 4;
        this.mPotvrdiElementi.TabStop = false;
        this.mPotvrdiElementi.Text = "Потврди";
        this.mPotvrdiElementi.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        this.mPotvrdiElementi.UseCustomBackColor = true;
        this.mPotvrdiElementi.UseCustomForeColor = true;
        this.mPotvrdiElementi.UseSelectable = true;
        this.mPotvrdiElementi.UseStyleColors = true;
        this.mPotvrdiElementi.Click += new System.EventHandler(this.mPotvrdiElementi_Click);

1 个答案:

答案 0 :(得分:0)

我知道这是很久以前但是我遇到了同样的问题...... 但我找到了问题的解决方案,并且每次都在工作,而不是杀死可用性。

private int focusFlag = 0;

private void MainForm_MouseEnter(object sender, EventArgs e)
{
     if (focusFlag < 1)
     {
         this.FocusMe();
         ++focusFlag;
     }
}

当尝试转到其他形式或其他形式时,这并不总是试图专注于该形式,它只会聚焦一次,这就足够了......之后它会表现正常:) 在您在表单中单击之前,MetroForm似乎没有获得焦点,而在表单中使用某些Metro控件时,它只是MetroFramework开发人员的错误。

我看到其他人在使用MetroFramework时发布了同样的问题。

希望这会有所帮助。