2013-03-09 47 views
5

我想壓制TextBox中的關鍵筆劃。爲了抑制比Backspace鍵以外的所有按鍵,我使用以下命令:如何防止TextBox中的退格鍵擊?

private void KeyBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
    { 
     e.Handled = true; 
    } 

但是,我只希望抑制按鍵時按下此鍵爲退格。我使用以下內容:

 if (e.Key == System.Windows.Input.Key.Back) 
     { 
      e.Handled = true; 
     } 

但是,這不起作用。選擇開始後面的字符仍然被刪除。我確實在輸出中顯示「TRUE」,所以後退鍵被識別。我將如何防止用戶按退格鍵? (我的理由是我想在某些情況下刪除字符而不是字符,所以我需要處理後退鍵自己))

+0

是不是有像「PreviewKeyDown」這樣的事件? – 2013-03-09 21:41:41

+0

Windows Phone的Silverlight不包含PreviewKeyDown的實現。 – 2013-03-09 23:46:45

回答

0

的確,沒有簡單的方法來處理這種情況,但這是可能的。

您需要在類中創建一些成員變量,以便在KeyDown,TextChanged和KeyUp事件之間跳轉時存儲輸入文本,光標位置和返回鍵按下狀態的狀態。

代碼應該是這個樣子:

string m_TextBeforeTheChange; 
    int m_CursorPosition = 0; 
    bool m_BackPressed = false; 

    private void KeyBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
    { 
     m_TextBeforeTheChange = KeyBox.Text; 
     m_BackPressed = (e.Key.Equals(System.Windows.Input.Key.Back)) ? true : false; 
    } 

    private void KeyBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     if (m_BackPressed) 
     { 
      m_CursorPosition = KeyBox.SelectionStart; 
      KeyBox.Text = m_TextBeforeTheChange; 
     } 
    } 

    private void KeyBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) 
    { 
     KeyBox.SelectionStart = (m_BackPressed) ? m_CursorPosition + 1 : KeyBox.SelectionStart; 
    } 
+0

謝謝你,很好的解決方法。 – msbg 2013-03-21 23:06:46

+0

接受的解決方案不是最好的。更好的建議[下面](http:// stackoverflow。com/questions/15316179/how-to-prevent-a-backspace-key-stroke-in-a-textbox/18062836#18062836)using e.SuppressKeyPress – tofo 2016-11-01 10:41:53

3

在Silverlight中,沒有辦法處理系統鍵事件,如退格鍵。因此,您可以檢測到它,但不能手動處理它。

0
string oldText = ""; 
    private void testTextBlock_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     if (testTextBlock.Text.Length < oldText.Length) 
     { 
      testTextBlock.Text = oldText; 
      testTextBlock.SelectionStart = oldText.Length; 
     } 
     else 
     { 
      oldText = testTextBlock.Text; 
     } 
    } 
0

這就要求我們存儲文本框的值的鍵按下事件之前。不幸的是退格是在事件被觸發前處理的,所以我們必須在事件發生之前捕獲它,然後在鍵盤事件發生後我們可以再次更新它。

private string textBeforeChange; 

    private void TextBox1_OnKeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Back) 
     { 
      e.Handled = true; 
      textBox1.Text = textBeforeChange; 
     } 
    } 

    private void TextBox1_OnKeyUp(object sender, KeyEventArgs e) 
    { 
     textBeforeChange = textBox1.Text; 
    } 

    private void MainPage_OnLoaded(object sender, RoutedEventArgs e) 
    { 
     textBox1.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox1_OnKeyDown), true); 
     textBox1.AddHandler(TextBox.KeyUpEvent, new KeyEventHandler(TextBox1_OnKeyUp), true); 
     textBox1.AddHandler(TextBox.ManipulationStartedEvent, new EventHandler<ManipulationStartedEventArgs>(TextBox1_OnManipulationStarted), true); 
    } 

    private void TextBox1_OnManipulationStarted(object sender, ManipulationStartedEventArgs e) 
    { 
     textBeforeChange = textBox1.Text; 
    } 
12

只要在要抑制擊鍵時設置e.SuppressKeyPress = true(在KeyDown事件中)即可。防爆,防止退格鍵使用下面的代碼更改文本框的文本:

private void textBox1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Back) 
    { 
     e.SuppressKeyPress = true; 
    } 
} 
0

這是我想出了要刪除以前(按CtrlBackspace鍵)和下一個單詞(按Ctrl刪除 ),處理多個後續的空白字符(0×09,0×20,0XA0):

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace DeleteWord 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void textBox1_KeyDown(object sender, KeyEventArgs e) 
     { 
      // Tab, space, line feed 
      char[] whitespace = {'\x09', '\x20', '\xA0'}; 
      string text = textBox1.Text; 
      int start = textBox1.SelectionStart; 

      if ((e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) && textBox1.SelectionLength > 0) 
      { 
       e.SuppressKeyPress = true; 
       textBox1.Text = text.Substring(0, start) + text.Substring(start + textBox1.SelectionLength); 
       textBox1.SelectionStart = start; 
       return; 
      } 

      else if (e.KeyCode == Keys.Back && e.Control) 
      { 
       e.SuppressKeyPress = true; 

       if (start == 0) return; 

       int pos = Math.Max(text.LastIndexOfAny(whitespace, start - 1), 0); 

       while (pos > 0) 
       { 
        if (!whitespace.Contains(text[pos])) 
        { 
         pos++; 
         break; 
        } 
        pos--; 
       } 

       textBox1.Text = text.Substring(0, pos) + text.Substring(start); 
       textBox1.SelectionStart = pos; 
      } 
      else if (e.KeyCode == Keys.Delete && e.Control) 
      { 
       e.SuppressKeyPress = true; 

       int last = text.Length - 1; 

       int pos = text.IndexOfAny(whitespace, start); 
       if (pos == -1) pos = last + 1; 

       while (pos <= last) 
       { 
        if (!whitespace.Contains(text[pos])) break; 
        pos++; 
       } 

       textBox1.Text = text.Substring(0, start) + text.Substring(pos); 
       textBox1.SelectionStart = start; 
      } 
     } 

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
     { 
      if (keyData == Keys.Tab) 
      { 
       textBox1.Paste("\t"); 
       return true; 
      } 
      else if (keyData == (Keys.Shift | Keys.Tab)) 
      { 
       textBox1.Paste("\xA0"); 
       return true; 
      } 
      return base.ProcessCmdKey(ref msg, keyData); 
     } 

    } 
} 

感謝伊阮爲e.SuppressKeyPress = true;

如果有一個選擇,無論是刪除和Backspace將刪除選擇,無論修改鍵(你不會得到那個醜陋的矩形字符控股Ctrl鍵

似乎爲字符工作像爲那麼,雖然它可能沒有多大意義(是不是這個字符是一個整詞?)