2013-04-21 195 views
4

我在C#中的要求,我必須與;分隔號的文本框說例如 (205)33344455;918845566778;刪除字符串從後

現在,當用戶按下←退格(以去除數)每次一個字符被刪除。我想一次刪除整個號碼。

因此,當用戶按下的第一次,號碼將被高亮顯示 也就是說,如果文字是(205)33344455;918845566778;,該918845566778;部分將被高亮說黑色,而當用戶再次按下的整數即918845566778;將被刪除。

那麼,這可能突出在文本框中的特定部分,並刪除整數?

我用for循環,如:

for{back=txtPhone.Text.Length;back<=txtPhone.Text.indexOf(';');back--) 

但我沒能達到預期的效果。

對此的任何幫助將是偉大的。

+0

也將是巨大的,如果用戶想要從之間左右取出數,不僅回來了。 對不起,這個新的查詢。 – Siddharth 2013-04-23 07:22:21

回答

0

一對夫婦的方法來實現希望你想浮現在腦海中:

  • 訂閱文本框Control.Keydown事件,這將檢查該按鈕,直至最後一個分隔符進行強調( ;)使用TextBox.SelectionLength意思是←Backspace鍵將清除它。

    private void textBox1_KeyDown(object sender, KeyEventArgs e) 
        { 
         if (e.KeyCode != Keys.Left) 
          return; 
    
         e.SuppressKeyPress = true; 
    
         //Select up to previous delimeter (;) here 
        } 
    
  • 使用列表框(或類似的東西)來存儲分隔的數據被輸入時。這將允許用戶選擇他們需要的內容,並通過您將提供的按鈕將其刪除。

0

您可以實現如下圖所示

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (textBox1.Text.Length == 0) return; 

    if ((e.KeyChar == (char)Keys.Back) && (textBox1.SelectionLength == 0)) 
    { 
     textBox1.SelectionStart = Math.Max(0, textBox1.Text.Substring(0,textBox1.Text.Length-1).LastIndexOf(';')); 
     if (textBox1.Text.Substring(textBox1.SelectionStart, 1) == ";") textBox1.SelectionStart++; 
     textBox1.SelectionLength = textBox1.Text.Length-textBox1.SelectionStart ; 
     e.Handled = true; 
     return; 
    } 

    if ((e.KeyChar == (char)Keys.Back) && textBox1.SelectionLength >= 0) 
    { 
     textBox1.Text = textBox1.Text.Substring(0, textBox1.SelectionStart); 
     textBox1.SelectionStart = textBox1.Text.Length; 
     e.Handled = true; 
    } 
} 
+0

也只是多一個查詢,如果用戶想要從中間刪除一個數字,那麼這個代碼可能會失敗,因爲我們總是試圖獲取LastIndexOf(「;」)。 – Siddharth 2013-04-23 07:20:54

0

您的要求,您可以:

  1. 選擇令牌(即數終止通過;),該包含光標(方法selectToken())
  2. 刪除它,當退格是 第二次按下

實施例: 你的文本框包含「(205)33344455 ; 918845566778; 8885554443;」

用鼠標左鍵單擊9188455和66778; (第二個數字)

然後按下退格

字符串918845566778;被選中

按退格第二次以及如果顯示的代碼串被刪除


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

namespace Remove_String_from_Text_Box_from_back 
{ 
    public partial class Form1 : Form 
    { 
    //selects token that contains caret/cursor  
    private void selectToken() { 
     string str0 = textBox1.Text; 
     int caretPosition = textBox1.SelectionStart; 
     int tokenEndsAtIndex = str0.IndexOf(';', caretPosition, (textBox1.Text.Length - caretPosition)); 
     string prefix = ""; 
     if (tokenEndsAtIndex == -1) 
     { 
      tokenEndsAtIndex = str0.IndexOf(';'); 
     } 

     prefix = str0.Substring(0, tokenEndsAtIndex); 
     int tokenStartsAtIndex = 0; 
     tokenStartsAtIndex = prefix.LastIndexOf(';'); 
     if (!(tokenStartsAtIndex > -1)) { tokenStartsAtIndex = 0; } else { tokenStartsAtIndex++; } 
     textBox1.SelectionStart = tokenStartsAtIndex; 
     textBox1.SelectionLength = tokenEndsAtIndex - tokenStartsAtIndex + 1;//may be off by one 

    } 
private void selectLastToken(string str0) 
     { 
      Regex regex = new Regex(@"([\d()]*;)$"); 
      var capturedGroups = regex.Match(str0); 

      int idx0 = 0; 
      if (capturedGroups.Captures.Count > 0) 
      { 
       idx0 = str0.IndexOf(capturedGroups.Captures[0].Value, 0); 
       textBox1.Select(idx0, textBox1.Text.Length); 
      } 
     } 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      textBox1.Text = "(205)33344455;918845566778;"; 
      textBox1.Select(0, 0); 
     } 

     //selects last token terminated by ; 
     private void selectTextOnBackSpace() 
     { 
      string str0 = textBox1.Text; 
      int idx0 = str0.LastIndexOf(';'); 
      if (idx0<0) 
      { 
       idx0 = 0; 
      } 
      string str1 = str0.Remove(idx0); 
      int idx1 = str1.LastIndexOf(';'); 
      if (idx1 < 0) 
      { 
       idx1 = 0; 
      } 
      else 
      { 
       idx1 += 1; 
      } 
      textBox1.SelectionStart = idx1; 
      textBox1.SelectionLength = str0.Length - idx1; 
     } 


     private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (e.KeyChar == (char)Keys.Back) 
      { 
       if (textBox1.SelectionLength==0) 
       { 
        selectToken(); 
        e.Handled = true; 
       } 
       else 
       { 
        e.Handled = false; 
       } 
      } 
     } 
    } 
} 
+0

我想你重新編輯你的答案。我嘗試了你給出的舊解決方案,它有效,儘管我已經修改了一點,而且我確實得到了答案。 但我還沒有嘗試你的新代碼,因爲我不太熟悉正則表達式。 – Siddharth 2013-04-23 07:12:31

+0

另外還有一個查詢,如果用戶想要從中間刪除一個數字,那麼這個代碼可能會失敗,因爲我們總是試圖獲取LastIndexOf(「;」)。 – Siddharth 2013-04-23 07:20:14

+0

感謝您的反饋,您的意思是,如果插入/光標介於(205)和33344455之間;你想讓那部分數字被刪除?如果是這樣的話,你是對的,這不是我的代碼所涵蓋的,儘管我會盡快給你一個新的答案。再次感謝你的反饋! – 2013-04-23 07:37:55