2015-09-28 71 views
-1

我有幾個按鈕的贏泡沫應用程序。當我運行這個應用程序,我可以按下這些按鈕,使用鼠標點擊也能夠使用鍵盤按鍵按下這些按鈕。我不想用鍵盤按鍵按下這些按鈕。如何使用「空格鍵或回車鍵」等鍵盤按鍵停止按下按鈕。 C#

當我點擊「Tab」或箭頭鍵「^,v,<,>」時,我也想停止按鈕上的焦點。

問候

+0

以下行則應該禁用按鈕,所以你不能選項卡給他們。 – Dbuggy

+2

關閉鍵盤 – Bgl86

+0

當我將Enable屬性更改爲False時,按鈕也不能按下鼠標單擊。 –

回答

4

而不是標準的Button,請使用以下的時候,你需要的是行爲

public class NonSelectableButton : Button 
{ 
    public NonSelectableButton() 
    { 
     SetStyle(ControlStyles.Selectable, false); 
    } 
} 

編輯: 這裏是一個小的測試證明了它的工作

using System; 
using System.Linq; 
using System.Windows.Forms; 

namespace Samples 
{ 
    public class NonSelectableButton : Button 
    { 
     public NonSelectableButton() 
     { 
      SetStyle(ControlStyles.Selectable, false); 
     } 
    } 

    static class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      var form = new Form(); 
      Control[] controls = { new TextBox(), new TextBox(), }; 
      Button[] buttons = { new NonSelectableButton { Text = "Prev" }, new NonSelectableButton { Text = "Next" }, }; 
      foreach (var button in buttons) 
       button.Click += (sender, e) => MessageBox.Show("Button " + ((Button)sender).Text + " clicked!"); 
      int y = 0; 
      foreach (var item in controls.Concat(buttons)) 
      { 
       item.Left = 8; 
       item.Top = y += 8; 
       form.Controls.Add(item); 
       y = item.Bottom; 
      } 
      Application.Run(form); 
     } 
    } 
} 

EDIT2: :要應用該解決方案,您需要執行以下操作:

(1)一個新的代碼文件添加到您的項目,把它與下面的內容NonSelectableButton.cs

using System; 
using System.Linq; 
using System.Windows.Forms; 

namespace YourNamespace 
{ 
    public class NonSelectableButton : Button 
    { 
     public NonSelectableButton() 
     { 
      SetStyle(ControlStyles.Selectable, false); 
     } 
    } 
} 

(2)編譯項目
(3)現在,新的按鈕將出現在控制工具箱(位於頂部),您可以將它拖到表格,而不是標準按鈕的

+0

無法正常工作...使用此代碼,按鈕仍然可以選擇.... –

+0

@MuhammedNaqi你換了你的表單按鈕嗎? –

+0

是的,我做到了。但仍然不工作... –

2

我聽出停止所有按鍵響應空欄最好是這樣的:

注:這是一個黑客位的:

設置你的表格的KeyPreview財產至true

然後將此代碼添加到窗體的KeyPress事件:

private void Form1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (this.ActiveControl is Button 
    && e.KeyChar == (char)Keys.Space) 
    { 
     var button = this.ActiveControl; 
     button.Enabled = false; 
     Application.DoEvents(); 
     button.Enabled = true; 
     button.Focus(); 
    } 
} 

,並停止從獲取焦點Tab鍵時,只需將按鈕的TabStop屬性設置爲false控制。

+0

您的解決方案大致解決了我的問題。在這裏,仍然繼續使用箭頭鍵「^,v,<,>」 –

0

看起來你有麻煩應用我以前的答案。下面是使用了同樣的想法的另一種方式:

添加一個新的代碼文件到您的項目,並把下面的代碼中(請務必用你更換YourNamespace!)

using System; 
using System.Reflection; 
using System.Windows.Forms; 

namespace YourNamespace 
{ 
    public static class Utils 
    { 
     private static readonly Action<Control, ControlStyles, bool> SetStyle = 
      (Action<Control, ControlStyles, bool>)Delegate.CreateDelegate(typeof(Action<Control, ControlStyles, bool>), 
      typeof(Control).GetMethod("SetStyle", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(ControlStyles), typeof(bool) }, null)); 
     public static void DisableSelect(this Control target) 
     { 
      SetStyle(target, ControlStyles.Selectable, false); 
     } 
    } 
} 

然後使用您的窗體中的加載每個按鈕所需的事件。

舉例來說,如果你的表單包含2個按鈕叫btnPrevbtnNext,包括在你的窗體加載事件

btnPrev.DisableSelect(); 
btnNext.DisableSelect(); 
+0

它的好!但你以前的代碼工作完美...謝謝你@lvan Stoev U很好.. –