1

我正在开发使用 以及更多控件的 Windows 窗体应用RichtextBox程序Menustrip

我做了一些工作,但无法让它工作。当我的鼠标光标在 RichTextBox 中移动时,我想自动更改位置,就像在简单的记事本上一样。

我的位编码是....

我想要它,所以当我的鼠标光标移动时,它会改变状态栏上的动态位置

private void sizeToolStripMenuItem_Click(object sender, EventArgs e)
{        
    int line = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
    int column = richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexFromLine(line);
    toolStripStatusLabel5.Text ="Line"+" "+ line.ToString();
    toolStripStatusLabel6.Text = " Column" + " " + line.ToString();
    toolStripStatusLabel3.Text= Cursor.Position.ToString(); // where is my mouse cursor at this Time like that x and y cordinate 330,334
}
4

4 回答 4

3

每次按回车键时显示您的线路。代码在下面提到:::----

private void Key_Down(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        int line = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
        int column = richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexFromLine(line);

        toolStripStatusLabel5.Text = "Line" + " " + line.ToString();
        toolStripStatusLabel6.Text = " Column" + " " + column.ToString();
        toolStripStatusLabel3.Text = Cursor.Position.ToString(); // where is my mouse cursor at this Time like that x and y cordinate 330,334
        Update();
    }
}
于 2013-02-28T23:10:29.847 回答
1

您可以订阅RichTextBox MouseMove事件以ToolStrip使用当前鼠标位置更新标签

例子:

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
    toolStripStatusLabel3.Text = string.Format("X={0}, Y={1}", e.X, e.Y);
}

或者,如果您希望它显示相对于您的位置,RichTextBox您可以使用Locationfrom MouseEventArgs,这将返回RichTextBox(文本框的左上角 = 0,0)内的位置

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
    toolStripStatusLabel3.Text = string.Format("X={0}, Y={1}", e.Location.X, e.Location.Y);
}
于 2013-02-28T22:19:59.297 回答
1

如果你想自动更新位置,你应该使用来自 Richtextbox 的 MouseMove 事件。当您移动鼠标时,它总是在更新。此外,MouseMove 调用中的“MouseEventArgs e”可以为您提供富文本框中的光标位置。

于 2013-02-28T22:21:41.873 回答
1

我在 StackoverFlow 和可爱的用户(程序员)的帮助下完成了。谢谢您的回复。我的代码是

private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Left)
        {
            int line = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
            int column = richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexFromLine(line);

            toolStripStatusLabel5.Text = "Line" + " " + line.ToString();
            toolStripStatusLabel6.Text = " Column" + " " + line.ToString();
            toolStripStatusLabel3.Text = Cursor.Position.ToString(); // where is my mouse cursor at this Time like that x and y cordinate 330,334
            Update();

        }

    }
于 2013-02-28T23:03:45.193 回答