c# - 为什么我没有得到我点击/选择的真实当前字符?

标签 c# winforms

这是代码:

private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (mouseisup == false)
                {
                    textBox1.Text = "";
                    int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
                    richTextBox1.SelectionStart = positionToSearch;
                    textBox1.Text = richTextBox1.Text.Substring(positionToSearch, 1);
                    previousChar = positionToSearch;
                    textBox2.Text = "";
                    mouseisup = true;//add this statement
                }
                else
                {
                    currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
                    if (currentChar > previousChar + 2 || currentChar < previousChar - 2)
                    {
                        richTextBox1.SelectionStart = currentChar;
                        textBox2.Text = richTextBox1.Text.Substring(currentChar, 1);
                        button1.Enabled = true;
                    }
                    mouseisup = false;
                }
            }
        }

        private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (textBox2.Text == "")
            {
                mouseisup = true;
                button1.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            string[] text = Parse_Text.ParseText(richTextBox1.Text, textBox1.Text, textBox2.Text);
            for (int i = 0; i < text.Length; i++)
            {
                richTextBox2.Text = text[i];
            }
        }

我为测试创建了一个文本新文本文件并输入了文本文件:

“Daniel > 更小 >< 然后 daniel 或 Daniel 取决于 D > 但 <<>” 然后在程序中第一次点击第一个大牛的第一个D,第二次点击第二个大牛的第二个D。

然后在这段代码中的 parse_text 中的新类中:

List<string> parsedText = new List<string>();
            string[] entries = null;
            int startIndex = text.IndexOf(startTag);
            if (startIndex >= 0)
            {
                int endIndex = text.IndexOf(endTag, startIndex);

startIndex 是 0 但 endIndex 也是 0。但是 endTag 是第二个 D 它不应该是索引 0。 所以这也是我的问题。

最佳答案

你的问题是你取了一个确实会给你第一次出现的字符的 IndexOf 。 为避免这种情况,请使用字符的位置而不是实际字符。

您已经在线上获得了该信息:

currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));

编辑

您应该更改 Parse_Text 方法以使用位置而不是字符。 在我的脑海中,你应该得到类似的东西:

    public static string[] ParseText(string text, int startPos, int endPos)
    {
        List<string> parsedText = new List<string>();
        string[] entries = null;

        if (startPos >= 0 && endPos > startPos)
        {
            string images = text.Substring(startPos + 1, endPos - startPos - 1);
            entries = images.Split(new[] { ',' });
            for (var i = 0; i < entries.Length; i++)
            {
                entries[i] = entries[i].Replace("\"", "");
            }

            for (int i = 0; i < entries.Length; i++)
            {
                parsedText.Add(entries[i]);
            }
        }

        return entries;
    }

显然,您应该添加一些关于允许参数的额外测试。 例如:

  • 文本不能是string.Empty
  • startPos 不能小于零
  • endPos 必须大于 startPos

关于c# - 为什么我没有得到我点击/选择的真实当前字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20188577/

相关文章:

c# - Datagridview 多选不起作用

c# - 标签的默认填充

c# - 将编辑后的数据按行保存

c# - 在线获取错误 "The remote server returned an error: (400) Bad Request."WebResponse response = request.GetResponse();

c# - 为什么这些是 "Special Classes"?

c# - 使用 ObjectSet 有什么好处

c# - Linq选择没有 child 的父记录

c#: winforms: tooltip: 如何延迟 tooltip.Show

c# - 带有 resultselector 重载的 Linq GroupBy 方法有什么用?

C# 在面板周围拖动控件