Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / September 2005

Tip: Looking for answers? Try searching our database.

question about handling selection in textbox/richtextbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nadav Popplewell - 14 Sep 2005 06:13 GMT
Hi everybody,

When selecting text in a textbox by moving the cursor while holding the
shift key
there are two 'modes':
1. if you start the selection by moving to the right then the selection
extends to the right, and moving the cursor left&right moves the RIGHT end of
the selection, as if the cursor was at the right side of the selection.
2. if you start the selection by moving to the left then the selection
extends to the left, and moving the cursor left&right moves the LEFT end of
the selection.

However,  in the code (for example, in SelectionChanged event) I can't tell
these modes apart!
In Both cases, SelectionStart points to the left end of the selection, and
SelectionLength holds the number of selected chars.

Is there some way to tell in which selection 'mode' the textbox is?

Thanks,
Nadav
"Jeffrey Tan[MSFT]" - 14 Sep 2005 08:30 GMT
Hi Nadav,

Thanks for your post.

I am not sure why you want to distinguish "left mode" with "right mode",
but there is no build-in support for this.

However, if you really want to get this information, I think we can write
some customized code to do the distinguishing. For example, we can handle
the TextBox_MouseDown event, record the first select start in a variable,
then record the last select end in another variable. To distinguish the
select start mouse down and select end mouse down, we can determine if the
SelectionLength is greater than 0(Only select end SelectionLength  will be
greater than 0). Code like this:

[DllImport("user32.dll")]
static extern bool GetCaretPos(out Point lpPoint);
private void timer1_Tick(object sender, System.EventArgs e)
{
               if(this.textBox1.SelectionLength!=0)
    {
        if(caretPos>oldPos)
        {
            this.Text=" right mode";
        }
        else if(caretPos<oldPos)
        {
            this.Text=" left mode";
        }
    }
    else
    {
        this.Text=" middle mode";
    }
}

int oldPos=-1;
int caretPos=-1;
private void textBox2_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
    Point pt;
    if(!GetCaretPos(out pt))
    {
        MessageBox.Show("failed");
        return;
    }

    if(this.textBox1.SelectionLength==0)
    {
        oldPos=pt.X;
    }
    else
    {
        caretPos=pt.X;
    }
}
This works well on my side. However, there are some extra work to be done,
we have to take care of the keyboard selection(handle KeyDown/KeyUp event).
Also, we should take care of the Tab key to enter into the textbox with the
entire text selected(this is always "right mode" :-))

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Herfried K. Wagner [MVP] - 14 Sep 2005 11:58 GMT
"Nadav Popplewell" <BacSoftDev@newsgroup.nospam> schrieb:
> When selecting text in a textbox by moving the cursor while holding the
> shift key
[quoted text clipped - 11 lines]
> tell
> these modes apart!

Getting and setting the position of the caret in a textbox
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=textboxcaretpos&lang=en>

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>

Nadav Popplewell - 15 Sep 2005 07:17 GMT
Hi Herfried,

I tried using GetCaretPos() & GetCharIndexFromPosition(), but it does not
seem to work properly.

I added this code to the SelectionChanged event handler of a RichTextBox:
Point p=GetCaretPos();
int inx=richTextBox1.GetCharIndexFromPosition(p);
System.Diagnostics.Debug.WriteLine(string.Format("caret:{0} ->
pos:{1}/sel:{2}",p,inx,richTextBox1.SelectionStart));

This is what I got when I typed 'testing' in the RichTextBox and then press
LEFT until the cursor returned to the beginning:

caret:{X=2,Y=1} -> pos:0/sel:1
caret:{X=8,Y=1} -> pos:1/sel:2
caret:{X=13,Y=1} -> pos:2/sel:3
caret:{X=16,Y=1} -> pos:3/sel:4
caret:{X=18,Y=1} -> pos:4/sel:5
caret:{X=24,Y=1} -> pos:5/sel:6
caret:{X=30,Y=1} -> pos:6/sel:7
caret:{X=24,Y=1} -> pos:6/sel:6
caret:{X=18,Y=1} -> pos:4/sel:5
caret:{X=16,Y=1} -> pos:3/sel:4
caret:{X=13,Y=1} -> pos:3/sel:3
caret:{X=8,Y=1} -> pos:2/sel:2
caret:{X=2,Y=1} -> pos:0/sel:1
caret:{X=-1,Y=1} -> pos:0/sel:0

As you can see the values for GetCharIndexFromPosition(GetCaretPos()) &
SelectionStart are not always the same.

Thanks,
Nadav.
"Jeffrey Tan[MSFT]" - 19 Sep 2005 06:49 GMT
Hi Nadav,

Have you tried the code snippet I provided? Based on my test, it works well
on my side. If you have any concern, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nadav Popplewell - 20 Sep 2005 11:05 GMT
Hi Jeffery,

I just tried your code,
and it does NOT work for me.
Two things I noticed :
1. you have a timer1_Tick() method, but you didn't include the declaration
of the timer itself. Which timer were you using?
(I Copied the code insite the timer_Tick() method to an Elapsed event
Handler of an System.Timers.Timer)
2. The Code inside the timer method compares caretPos&oldPos.
But these variables are only changed in the MouseDown event.
There is no handling of MouseMove, so how can this code handle moving the
mouse left or right?

Thanks,
Nadav.
"Jeffrey Tan[MSFT]" - 28 Sep 2005 07:11 GMT
Hi Nadav,

Have you received my sample project? Does it help you? If you still have
any concern, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.