【问题标题】:How can I find the character at my caret position?我怎样才能找到我的插入符位置的字符?
【发布时间】:2023-01-05 07:45:59
【问题描述】:

我正在尝试制作一个类似于记事本的程序。我想这样做,以便当您在文本框中键入内容时,打开的花括号会变成打开的和关闭的花括号。

我只需要 if 语句才能正常工作。

if(textBox1.Text[textBox1.SelectionStart] == '{')
{
  //Removes the last {
  textBoxList.Clear();
  for(int i = 0; i < textBox1.TextLength - 2; i++)
  {
    textBoxList.Add(Convert.ToString(textBox1.Text[i]));
  }
  textBox1.Text = "";
  for(int i = 0; i < textBoxList.Count; i++)
  {
    textBox1.Text += textBoxList[i];
  }
  //Adds the new curly braces
  textBox1.Text += indentCurlyBraces;
  //Goes inside the if
  textBox1.SelectionStart = textBox1.TextLength - 2;
  textBox1.ScrollToCaret();
}

此 if 语句位于文本框更改方法的内部。

我试过使用 textBox1.SelectionStart,但程序崩溃了,因为索引在 textBox1 string[] 之外 我也试过减去一、二,向 SelectionStart 添加东西,但一切都在数组的范围之外。

【问题讨论】:

  • 在 TextBox 的 KeyPress 事件处理程序中,添加 if (e.KeyChar == '{') { e.Handled = true; ((TextBoxBase)sender).SelectedText = "{}"; } -- 如果插入符应该在大括号内结束,添加 .SelectionStart -= 1; -- 你显示的代码真的很麻烦

标签: c# winforms


【解决方案1】:

在您的解决方案中复制此实用程序类:

public class TweakTextBox
{
    private readonly string _addValue;

    private readonly string _findValue;

    private readonly TextBox _textBox;

    private bool enable;

    public TweakTextBox(TextBox t, string findValue, string addValue)
    {
        _textBox = t;
        _findValue = findValue;
        _addValue = addValue;
        _textBox.TextChanged += T_TextChanged;
        _textBox.PreviewKeyDown += T_PreviewKeyDown;
    }

    private void T_PreviewKeyDown(object? sender, PreviewKeyDownEventArgs e) // this is required to allow backspacing on "}"
    {
        if (e.KeyCode == Keys.Back)
            enable = false; // disable the tweak in order to get the backspace working
        else
            enable = true; // replace the tweak functionality
    }

    private void T_TextChanged(object? sender, EventArgs e)
    {
        int index = _textBox.SelectionStart - 1; // find current position before char input = current SelectionStart - 1
        if (index >= 0 && (enable)) // if index is valid
        {
            string p = _textBox.Text[index..(index + 1)]; // get last input char
            if (p == _findValue) // if found char is the one specified (e.g. "{")
            {
                string pre = _textBox.Text[0..index]; // get all text preceding the found char
                string post = _textBox.Text[(index + 1)..]; // get all text following the found char
                _textBox.Text = pre + _findValue + _addValue + post; // final text is = previous text + found char + char to add + following text
                _textBox.Select(index + 1, 0); // reposition the caret at the last user's position
            }
        }
    }
}

使用示例:

public Form1()
        {
            InitializeComponent();
            TweakTextBox tweak = new(textBox1, "{", "}"); // add this line in your Form constructor
        }

我添加了 PreviewKeyDown 事件,如果按下退格键,该事件将禁用该功能:如果您尝试禁用此事件并在自动添加的“}”符号后使用脱字符退格,您就会明白我添加此功能的原因。

我确信有更有效的方法来实现这一点——可能对于 WPF,我会使用不同的方法,但问题被标记为 WinForms,所以我假设你没有使用 WPF。

另外,我没有使用 KeyPress 事件,因为文本可能因其他原因而改变——我真的认为 TextChanged 是您想要的事件。

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 2014-12-14
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多