Prevent cursor flicker in custom textbox

P

PeterB

If I override the OnKeyDown event and insert a case where a hyphen (minus
sign) is inserted in the textbox if 'n' is pressed, the cursor jumps from
the current position to the beginning of the textbox, writes the hyphen and
then back to the original position:

//Add Hyphen
this.Text = this.Text.Insert(0,"-");
this.SelectionStart = iCurrCaretPos + 1;

This causes some not very wanted flicker, and in my desperate attempts to
remove this I first thought of overriding the OnPaint method. In the OnPaint
event, I check a variable (_bPaint) to see if I should call base.OnPaint or
not. I set this variable to false while moving the caret around and true
whenever I'm not manipulating text in this matter

_bPaint = false;
this.Text = this.Text.Insert(0,"-");
this.SelectionStart = iCurrCaretPos + 1;
_bPaint = true;

protected override void OnPaint(PaintEventArgs e)
{
if( _bPaint )
base.OnPaint (e);
}

But OnPaint doesn't seem to be called during textchanges in the textbox
(breakpoint is never reached), or am I doing something wrong here?

Is there a way to prevent any changes to the textbox to be visible until all
operations has finished (prevent flicker).

Thanks,

Peter
 
G

Guest

You're correct, OnPaint is not overridable in the TextBox class.
Unfortunate but true.

-Chris
 
P

PeterB

Oh... :-(

Well thanks for your swift reply, do you (or anyone else) know why? Good to
understand the limitations...

/ Peter
 
C

Chris Tacke, eMVP

I'd love to know why as well, but there are a lot of "whys" about omissions
that I think each of us has. The general answer is that it would have
either taken toolong to implement or test or it would have made things
larger. In this specific case it's hard to imagine, but since neither you
or I architected the CF, it's easy to armchair quarterback and say "that
would have been easy to implement" and as such I'm hesitant to do so. I
know the team worked hard on it and made painful decisions, so I assume that
this was a decision that was meted out, thought over carefully, and we got
what we got for good reason.
 
P

Paul G. Tobey [eMVP]

I'll take a guess: because that control is actually just a wrapper around
the native control and you don't really get a 'paint' event. Of course, it
would be possible for Microsoft to grab that message and trigger the
OnPaint() method at the right time, but they'd have to handle a much
stranger set of operations with calling the inherited method, etc. in order
to have the control work right when OnPaint is not overridden.

Paul T.
 
C

Chris Tacke, eMVP

A logical guess, and probably accurate (though why not subclass it? hmm,
giving us the ability to pass delegates to native methods would be useful
there...)

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here
 

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