Screen Location of RichTextBox SelectedText?

J

Joe Cool

Is there any way to determine the location on the screen where the
currently selected text of a RichTextBox control is at?
 
J

Joe Cool

No. For one thing, the selection might not be on the screen; it might have
been scrolled far away.

Eq.

And that is part of the problem, if the selected text is not on the
screen I want to scroll the RichTextBox with code until the selected
text is on the screen.

I know this can be done as I have seen a commercial application do it.
Hell, Microsoft Word can do it.
 
J

Joe Cool

What, do you think Microsoft Word is made by dragging a few pre-designed
components onto a form?

Well, not really, but the other commercial app I have seen do this was
a piece of $20 software.
 
J

Joe Cool

To be fair: there must be *some* way to do this, but knowing the screen
position of a selection is not a common requirement, and I'd be very, very
surprised if .NET offers anything to do it.

You probably need to look up some details of the internals of RichTextBox,
and try to manipulate those things using API calls (via
System.Runtime.InteropServices). It will be messy and annoying.

I'm sure there is probably a way to do with API, but as you say, that
is messy and I'd really like to avoid that.
 
J

Joe Cool

No. For one thing, the selection might not be on the screen; it might have
been scrolled far away.

Thanks for the HideSelection property tip. Worked great. And once I
had that working, I see now that if the text found is on an area not
currently on the screeen, the RichTextBox automatically scrolls until
it is.

But I need to know the physical location as it may be under the find
dialog and if it is, I want to shift the position of the dialog box so
the found text is now visible.
 
P

Peter Duniho

Thanks for the HideSelection property tip. Worked great. And once I
had that working, I see now that if the text found is on an area not
currently on the screeen, the RichTextBox automatically scrolls until
it is.

The RichTextBox class has a ScrollToCaret() method that does exactly what
you're asking about. I didn't see any mention of the HideSelection
property in this message thread, but I'm assuming it was proposed as a
hack. Since there's a method in the class that does the same thing by
design rather than as a side-effect, it seems to me you should use that
instead.
But I need to know the physical location as it may be under the find
dialog and if it is, I want to shift the position of the dialog box so
the found text is now visible.

RichTextBox also has several methods (names starting with "Get...") that
provide this kind of information.

I suggest you read the documentation for more details.

Pete
 
P

Peter Duniho

That doesn't do what he asked.

Yes, it does. From one of his posts: "if the selected text is not on the
screen I want to scroll the RichTextBox with code until the selected text
is on the screen"

That's what ScrollToCaret() does.
The poster wants to determine the screen
location (i.e. a Point in pixels, or a Region, etc.) of the currently
selected text -- not to move it or display it.

Yes, he also wants to do that. And I pointed out the methods for that as
well.
Nope, it was in his second, unrelated thread about "Finding Text in a
RichTextBox".

Thanks, yes...I realized that once I got to the next thread. :)

Pete
 
P

Peter Duniho

He wrote: "I need to know the physical location as it may be under the
find
dialog and if it is, I want to shift the position of the dialog box so
the
found text is now visible." How would you do that entire task with
ScrollToCaret?

You don't. So what?

Just because I provided the answer to one question he asked, that doesn't
mean that answer is supposed to apply to EVERY question he asked. It
doesn't even make sense for you to think it should; obviously different
questions require different answers.
 
J

Jeff Johnson

And that is part of the problem, if the selected text is not on the
screen I want to scroll the RichTextBox with code until the selected
text is on the screen.
I know this can be done as I have seen a commercial application do it.
Hell, Microsoft Word can do it.

Word is not a wrapper around a rich text box....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top