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 / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Stopping text from highlighting in textbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
> Adrian < - 03 Jun 2007 18:09 GMT
Is there a way of stopping text from highlighting in textbox?
Many thanks,
Adrian.
Aneesh P - 03 Jun 2007 18:22 GMT
> Is there a way of stopping text from highlighting in textbox?
> Many thanks,
> Adrian.

You mean to ask, we should be able to select the complete text?..Pls
clarify.
Aneesh P - 03 Jun 2007 18:23 GMT
> Is there a way of stopping text from highlighting in textbox?
> Many thanks,
> Adrian.

You mean to ask, we should not be able to select the complete
text?..Pls
clarify.
> Adrian < - 03 Jun 2007 20:11 GMT
Supposing I have a textbox with a text. Then as soon as - later on - I do
something in the same textbox, with the text still there, say I want to
click the textbox for some action, I risk some of the text getting
"highlight". I want to stop that from happening.

Adrian.

> > Is there a way of stopping text from highlighting in textbox?
> > Many thanks,
[quoted text clipped - 3 lines]
> text?..Pls
> clarify.
Aneesh P - 04 Jun 2007 06:01 GMT
> Supposing I have a textbox with a text. Then as soon as - later on - I do
> something in the same textbox, with the text still there, say I want to
[quoted text clipped - 12 lines]
>
> - Show quoted text -

I don't see a way to restrict the highlighting. But in the Click Event
and Double Click event we deselect the selected text. So the behavior
will be like - the text will be selected for a fraction of second and
deselected.

       private void textBox1_Click(object sender, EventArgs e)
       {
           textBox1.DeselectAll();
       }

       private void textBox1_DoubleClick(object sender, EventArgs e)
       {
           textBox1.DeselectAll();
       }
Moty Michaely - 04 Jun 2007 07:07 GMT
Hi,

Maybe it's out of the posts' scope but you can try to hook the text
box, catch for example the mouse click message and not forward the
message. This might prevent the further behavior of the message
(highlighting for example).
Maybe you can *save* the highglight prior to the event and set it
again afterwards (this may cause the UI not look good)...

I am not aware of and text highlight disabling option in the TextBox
control class.

Feel free to ask any further questions.

Cheers,
Moty.
> Adrian < - 04 Jun 2007 09:21 GMT
> I am not aware of and text highlight disabling option in the TextBox
> control class.
>
> Feel free to ask any further questions.

Thank you.

Adrian.
> Adrian < - 04 Jun 2007 09:23 GMT
>       private void textBox1_Click(object sender, EventArgs e)
>         {
[quoted text clipped - 5 lines]
>             textBox1.DeselectAll();
>         }

This will do the trick I guess.
I am very pleased with your solution.
Thank you for responding.

Adrian
> Adrian < - 04 Jun 2007 11:15 GMT
> >       private void textBox1_Click(object sender, EventArgs e)
> >         {
[quoted text clipped - 11 lines]
>
> Adrian

No it didn't do the trick :( ...
Can the forecolor of the selected text be changed?

Adrian.
-pb- - 04 Jun 2007 11:56 GMT
> Supposing I have a textbox with a text. Then as soon as - later on - I do
> something in the same textbox, with the text still there, say I want to
[quoted text clipped - 12 lines]
>
> - Show quoted text -

Can you please expalin how some of the text getting hightlighted when
you are still in same textbox. Is it the case that user press Shift
key when they click it somewhere else. If this is the case then trap
the kepress event and check of code for shift key and then set
deselectall.
> Adrian < - 04 Jun 2007 14:10 GMT
> Can you please expalin how some of the text getting hightlighted when
> you are still in same textbox.

Supposing I have a textbox with a text. Then as soon as - later on - I do
something in the same textbox, with the text still there, say I want to
click the textbox for some action, I risk some of the text getting
"highlight". I want to stop that from happening.

Adrian.
Peter Duniho - 04 Jun 2007 17:24 GMT
>> Can you please expalin how some of the text getting hightlighted when
>> you are still in same textbox.
[quoted text clipped - 3 lines]
> click the textbox for some action, I risk some of the text getting
> "highlight". I want to stop that from happening.

Do you want to disable all mouse interaction with the control except for  
the click?  Or do you want to still preserve the ability to move the  
insertion caret with the mouse?

It seems to me that either way, you should be able to do what you want by  
overriding the OnMouse... methods() in the control.  It does mean you have  
to create your own custom control that inherits from TextBox, but it  
should work fine.  If you want to disable all mouse interaction, then  
simply write the overrides and then do nothing in them.  If you just want  
to disable selection, then you may be able to do that by making the  
OnMouseDown() method do nothing, and then in OnMouseUp() call both the  
base OnMouseDown() method and the base OnMouseUp() method with the same  
event args (so that it looks to the base class as if the mouse was clicked  
in a single spot).

I will reiterate my usual warning against unnecessarily disabling or  
changing standard functionality.  But if you have a genuinely important  
need to do this, I think the above might work.

Pete
> Adrian < - 04 Jun 2007 21:04 GMT
> >> Can you please expalin how some of the text getting hightlighted when
> >> you are still in same textbox.
[quoted text clipped - 24 lines]
>
> Pete

Thank you, I will have a go at that.

Adrian.
> Adrian < - 05 Jun 2007 20:03 GMT
> >> Can you please expalin how some of the text getting hightlighted when
> >> you are still in same textbox.
[quoted text clipped - 24 lines]
>
> Pete

It didn't work I'm afraid to say.

Adrian.
Peter Duniho - 06 Jun 2007 09:03 GMT
>> you should be able to do what you want by
>> overriding the OnMouse... methods() in the control.  It does mean you  
[quoted text clipped - 13 lines]
>
> It didn't work I'm afraid to say.

Sorry about that.  The theory was sound, but it turns out (unbeknownst to  
me) that the TextBox class does not actually do its real work in the  
OnMouse... methods.  After thinking about it a moment, I realize that this  
may be because the TextBox class doesn't really implement most of the  
behavior, but instead just uses the built-in Windows text edit control.

With that in mind, I looked to see if you could accomplish the same thing  
at a lower level, and lo and behold you can.  Here is some code that does  
basically what I was talking about (it goes in the control class derived  
from TextBox, and you need to use the derived class as the control in your  
form):

        const int WM_MOUSEMOVE = 0x0200;
        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;

        protected override void WndProc(ref Message m)
        {
            bool fHandled = false;

            switch (m.Msg)
            {
                case WM_LBUTTONDOWN:
                    // Ignore the left mouse button down event
                    fHandled = true;
                    break;
                case WM_MOUSEMOVE:
                    // Ignore any mouse move events
                    fHandled = true;
                    break;
                case WM_LBUTTONUP:
                    // Before we process the left mouse button up event,
                    // send a left mouse button down event with the same
                    // mouse location as the mouse up event, to simulate
                    // an actual click at the location.
                    Message mT = m;           // copy the current message
                    mT.Msg = WM_LBUTTONDOWN;  // change the message ID
                    base.WndProc(ref mT);     // send the simulated message
                    break;
            }

            if (!fHandled)
            {
                base.WndProc(ref m);
            }
        }

Because this version of the implementation operates on the underlying  
Windows messages themselves, it means that even when the text selection is  
being handled by a non-.NET piece of code, the mouse messages are still  
intercepted before that code handles them.

Note that the above disables selection, but allows the user to reposition  
the caret.  If you want no mouse interaction at all, just set "fHandled"  
in the WM_LBUTTONUP case as well, and take out the code that's in that  
case now.

Also note that the above is really just proof-of-concept.  I have not  
verified that hiding the mouse messages like this does not also result in  
some other funny behavior, that may or may not be desired.  You should  
think about the exact behavior and do thorough testing before using  
anything like this in real-world code (especially since I did neither :) ).

Hope that helps.

Pete

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.