2013-02-28 124 views
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 
} 

回答

1

每次按Enter键时显示您的线路。 以下代码::: ---- ----

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(); 
    } 
} 
1

你可以订阅RichTextBoxMouseMove用当前鼠标位置更新ToolStrip标签的事件

示例:

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

或者,如果你想让它显示的位置reletive到RichTextBox可以从MouseEventArgs使用Location,这将返回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); 
} 
1

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

+0

您能分享一下MouseMove编码吗 – 2013-02-28 22:31:59

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(); 

     } 

    }