0

I am trying to make a new application on C#, as a part of this I want to know the caret position (The exact point within the control) on a rich text control box.

I will explain it: assume I have a win form, rich textcontrol box and a contextmenustrip. When I type a specific charector or string on textbox I want to pop up this contextmenu item.

For this reason I want to know the exact point of caret on that text box.

As a result of googling + SO articles I found a way through GetCaretPos(), but I am unable to use it.

I did something with richtextbox get functions. One is the following:

Point k= richTextBox1.GetPositionFromCharIndex((richTextBox1.Lines[richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine())].Count() + 1));

I don't know if this is the exact point or not, but sometimes I am getting the correct value.

How can I fix the problem?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Dileep DiL
  • 83
  • 1
  • 1
  • 6
  • you said " I dont know this is the exact point or not ,but some times i am getting the correct value." If it is right sometimes, when is it right and when is it wrong? – agent-j Jul 15 '11 at 06:45
  • where do you want the context menu to appear? right below the caret? – agent-j Jul 15 '11 at 06:50

1 Answers1

1

Here's a quick way of seeing where the context menu would appear. Just make sure you subscribe to the event.

  private void richTextBox1_SelectionChanged(object sender, EventArgs e)
  {
     Point point = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
     Text = point.ToString ();// Write to window title for fun
     new ContextMenu(new MenuItem[] {new MenuItem("test")}).Show (richTextBox1, point);
  }
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
agent-j
  • 27,335
  • 5
  • 52
  • 79