Stopping text from highlighting in textbox

  • Thread starter Thread starter > Adrian
  • Start date Start date
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.
 
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.
 
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.
 
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.







- 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();
}
 
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.
 
private void textBox1_Click(object sender, EventArgs e)
{
textBox1.DeselectAll();
}

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

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

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

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

Adrian.
 
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.







- 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.
 
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.
 
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.

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
 
Peter Duniho said:
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

Thank you, I will have a go at that.

Adrian.
 
Peter Duniho said:
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

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

Adrian.
 
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).
[...]

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
 
Back
Top