RichTextBox 和插入符位置

发布于 2024-12-18 03:52:03 字数 1151 浏览 3 评论 0原文

当我选择文本时,我找不到确定 RTB 中插入符号位置的方法。 SelectionStart 不是一个选项

我想检测选择的方向是向后还是向前。我试图在 SelectionChanged event 中实现这一目标。任何提示将不胜感激。

编辑:

我通过使用 mouseDown 和 mouseUp 事件注册鼠标移动方向(X 轴)来解决这个问题。

代码:

bool IsMouseButtonPushed = false;
int selectionXPosition = 0, sDirection=0;

private void richTextBox_SelectionChanged(object sender, EventArgs e)
{
    if (sDirection==2)//forward
    {
        //dosomething
    }
}

private void richTextBox_MouseMove(object sender, MouseEventArgs e)
{
    if (IsMouseButtonPushed && (selectionXPosition - e.X) > 0)//backward
    {
        sDirection = 1;
    }
    else if (IsMouseButtonPushed && (selectionXPosition - e.X) < 0)//forward
    {
        sDirection = 2;
    }
}

private void richTextBox_MouseDown(object sender, MouseEventArgs e)
{
    IsMouseButtonPushed = true;
    selectionXPosition = e.X;
}

private void richTextBox_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseButtonPushed = false;
}

还有哪些其他方法可以做到这一点?

I can't find a way to determine the caret position in the RTB while i'm selecting text. SelectionStart is not an option.

I want to detect the direction of selection whether its backward or forward. I'm trying to achieve this in SelectionChanged event. Any tips would be appreciated.

EDIT:

I solved it by registering mouse movement direction (X axis) with mouseDown and mouseUp events.

Code:

bool IsMouseButtonPushed = false;
int selectionXPosition = 0, sDirection=0;

private void richTextBox_SelectionChanged(object sender, EventArgs e)
{
    if (sDirection==2)//forward
    {
        //dosomething
    }
}

private void richTextBox_MouseMove(object sender, MouseEventArgs e)
{
    if (IsMouseButtonPushed && (selectionXPosition - e.X) > 0)//backward
    {
        sDirection = 1;
    }
    else if (IsMouseButtonPushed && (selectionXPosition - e.X) < 0)//forward
    {
        sDirection = 2;
    }
}

private void richTextBox_MouseDown(object sender, MouseEventArgs e)
{
    IsMouseButtonPushed = true;
    selectionXPosition = e.X;
}

private void richTextBox_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseButtonPushed = false;
}

What are other ways to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-12-25 03:52:03

SelectionStart 和 SelectionLength 属性在左侧选择期间发生变化,而 SelectionLength 属性在右侧选择期间发生变化。

简单的解决方案:

int tempStart;
int tempLength;

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    if (richTextBox1.SelectionType != RichTextBoxSelectionTypes.Empty)
    {
        if (richTextBox1.SelectionStart != tempStart)
            lblSelectionDesc.Text = "Left" + "\n";
        else if( richTextBox1.SelectionLength != tempLength)
            lblSelectionDesc.Text = "Right" + "\n";
    }
    else
    {
        lblSelectionDesc.Text = "Empty" + "\n";
    }

    tempStart = richTextBox1.SelectionStart;
    tempLength = richTextBox1.SelectionLength;

    lblSelectionDesc.Text += "Start: " + richTextBox1.SelectionStart.ToString() + "\n";
    lblSelectionDesc.Text += "Length: " + richTextBox1.SelectionLength.ToString() + "\n";
}

控件:

RitchTextBox + 2xLabels

在此处输入图像描述

  1. 我不知道为什么,但即使在禁用 AutoWordSelection 后,我的鼠标也会选择整个字。不幸的是,对于我的解决方案,这会导致选择方向发生变化。
  2. 您可能会为此使用属性更改事件。

SelectionStart and SelectionLength properties changes during left side selection and SelectionLength changes during right side selection.

Simple solution:

int tempStart;
int tempLength;

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    if (richTextBox1.SelectionType != RichTextBoxSelectionTypes.Empty)
    {
        if (richTextBox1.SelectionStart != tempStart)
            lblSelectionDesc.Text = "Left" + "\n";
        else if( richTextBox1.SelectionLength != tempLength)
            lblSelectionDesc.Text = "Right" + "\n";
    }
    else
    {
        lblSelectionDesc.Text = "Empty" + "\n";
    }

    tempStart = richTextBox1.SelectionStart;
    tempLength = richTextBox1.SelectionLength;

    lblSelectionDesc.Text += "Start: " + richTextBox1.SelectionStart.ToString() + "\n";
    lblSelectionDesc.Text += "Length: " + richTextBox1.SelectionLength.ToString() + "\n";
}

Controls:

RitchTextBox + 2xLabels

enter image description here

  1. I am not sure why but even after disabling AutoWordSelection my mouse select whole words. Unfortunately for my solution this leads to selection direction change.
  2. You might probably use property change events for this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文