Problem with creating a "smart" textbox.

K

Keld Laursen

Hi, all.

I am trying to extend a TextBox to select the text in it upon focusing it.
I thought that it could be accomplished by simply attaching a GotFocus event
handler, and then fiddle with SelectionStart and SelectionLength.
I of course found out that this didn't work because the click event fires
afterwards. The suggestion I found on the net were to use a timer, but I
don't like timers for stuff like that.

I therefore tried implementing this little class:

public class SmartTextBox : TextBox
{
private bool m_newFocused;
public SmartTextBox():base()
{
m_newFocused=false;
this.Click += new EventHandler(SmartTextBox_Click);
this.GotFocus +=new System.EventHandler(SmartTextBox_GotFocus);
}

private void SmartTextBox_GotFocus(object sender, System.EventArgs e)
{
this.m_newFocused=true;
}

private void SmartTextBox_Click(object sender, EventArgs e)
{
if(this.m_newFocused)
{
this.m_newFocused=false;
this.SelectionStart=0;
this.SelectionLength=9999;
}
}
}

The code should raise a flag on GotFocus and then act on it when the Click
event fires.
Unfortunately the above code only works for the GotFocus event, but not for
the Click event.
I tried the same kind of code elsewhere, just creating the TextBox and
attaching the events. Here everything works as intended.

Did I do something wrong in creating this?

TIA

Best regards,

Keld Laursen
 
W

William Bates

Keld said:
Hi, all.

I am trying to extend a TextBox to select the text in it upon focusing it.
I thought that it could be accomplished by simply attaching a GotFocus event
handler, and then fiddle with SelectionStart and SelectionLength.
I of course found out that this didn't work because the click event fires
afterwards. The suggestion I found on the net were to use a timer, but I
don't like timers for stuff like that.


I am very new to .NET, but as I understand it...

A derived control will have at most the events of the original control,
so no matter if you add the event, it will not fire. Unfortunately, the
help is of little use in determining if an event is available. This is a
fault of CF IDE. I think there is better support in 2005.

To others of the ng, if my explanation is incorrect please correct me.
As I said, this is very new to me. Dotnet is a very intuitive framework,
the designers should be congratulated.
 
K

Keld Laursen

William Bates said:
I am very new to .NET, but as I understand it...

A derived control will have at most the events of the original control, so
no matter if you add the event, it will not fire. Unfortunately, the help
is of little use in determining if an event is available. This is a fault
of CF IDE. I think there is better support in 2005.

To others of the ng, if my explanation is incorrect please correct me. As
I said, this is very new to me. Dotnet is a very intuitive framework, the
designers should be congratulated.

AFAIK, I am not trying to use any events that aren't in the original
control.
The click event is shown as implemented in the CF when I look at the MSDN
help.

/Keld Laursen
 
K

Keld Laursen

Hi, Mark.

I saw your code when I searched for a solution. As I stated, I were trying
to avoid the timer.
I don't really like to use timers for stuff like this. But if it is the only
solution.....

What I tried were to create a flag-driven version that enabled me to forward
the processing from the GotFocus event to the Click event. The Click event
should be implemented if I read MSDN right, so I'm still at a loss as to why
this doesn't work.

/Keld Laursen
 
D

Daniel Moth

. The Click event should be implemented if I read MSDN right,

Can you point us to the URL where you read that?. Notice how MSDN shows the
methods and properties of a class noting which are supported by the Compact
Framework. Also note how it does not do the same for events; do not deduce
from this that *all* events for *all* controls are supported, instead treat
it as incomplete documentation. For more see here:
http://www.danielmoth.com/Blog/2005/04/intellisense-for-smart-device-project.html

As for your problem, Mark has already provided you with the solution.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Keld Laursen said:
Hi, Mark.

I saw your code when I searched for a solution. As I stated, I were trying
to avoid the timer.
I don't really like to use timers for stuff like this. But if it is the
only solution.....

What I tried were to create a flag-driven version that enabled me to
forward the processing from the GotFocus event to the Click event. The
Click event should be implemented if I read MSDN right, so I'm still at a
loss as to why this doesn't work.

/Keld Laursen
 
K

Keld Laursen

Daniel Moth said:
Can you point us to the URL where you read that?. Notice how MSDN shows
the

No I can't. I felt fairly certain that I had seen it work at some point
during my testing on this, but before I tried to put it into a separate
class. Having looked all over the place and recreating my original code has
revealed that you are indeed right, and I must have been delirious :(
As for your problem, Mark has already provided you with the solution.

And I am indeed implementing just that right now.
For a few seconds I tried playing around with the
Microsoft.WindowsCE.Forms.MessageWindow class, but I gave it up for being a
too convoluted answer to an otherwise simple problem.

Thanks for your help.

/Keld Laursen
 

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