4

我在 C# 中有一个要求,我有一个文本框,其中的数字由;例如 分隔(205)33344455;918845566778;

现在,当用户按下← Backspace(删除数字)时,一次删除一个字符。我想一次删除整个号码。

所以当用户按下←</kbd> the first time, the number will be highlighted i.e. if text is (205)33344455;918845566778;, the 918845566778; part will be highlighted in say black, and when the user presses ←</kbd> again the whole number i.e. 918845566778; will be deleted.

那么是否可以在文本框中突出显示特定部分并删除整个数字?

我使用了一个for循环,如:

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

但我无法达到预期的结果。

对此的任何帮助都会很棒。

4

3 回答 3

0

有几种方法可以实现您想要的想法:

  • 将文本框订阅到Control.Keydown将检查←</kbd> button and perform the highlight up to the last delimiter (;) using TextBox.SelectionLength meaning a ← Backspace will clear it.

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Left)
                return;
    
            e.SuppressKeyPress = true;
    
            //Select up to previous delimeter (;) here
        }
    
  • 使用列表框(或类似的东西)在输入时存储分隔数据。这将允许用户选择他们需要的内容,并通过您提供的按钮将其删除。

于 2013-04-21T08:46:22.087 回答
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;
                }
            }
        }
    }
}
于 2013-04-21T09:06:02.233 回答
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;
    }
}
于 2013-04-21T08:50:30.050 回答