2013-02-28 111 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(); 

     } 

    }