从文本框后面删除字符串

本文关键字:删除 字符串 文本 | 更新日期: 2023-09-27 18:10:16

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

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

所以当用户第一次按时,数字会高亮显示即,如果文本为(205)33344455;918845566778;, 918845566778;部分将以黑色突出显示,当用户再次按时,整个数字即918845566778;将被删除。

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

我使用了一个for循环:

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

但我没能达到预期的结果。

从文本框后面删除字符串

您可以按照如下所示实现您的需求

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;
    }
}

有几个方法可以达到你想要的效果:

  • 将文本框订阅到Control.Keydown事件,该事件将检查按钮并执行高亮显示直到最后一个分隔符(;),使用TextBox.SelectionLength表示←退格将清除它。

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

您可以:

  1. 选择包含游标的令牌(即以;结尾的数字)(方法selectToken())
  2. 删除它时,退格是第二次按

的例子:您的文本框包含'(205)33344455;918845566778;8885554443,"

你用鼠标左键点击9188455和66778之间;(第二号)

然后按退格键

string 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;
                }
            }
        }
    }
}