Using OpenNetCF to prevent textbox Flicker

A

a

I've been getting pretty annoyed with the Textbox Flicker. Any changed
to selected text causes the control to jump back to line one at the top
of the text for a second, then return to normal.

I've tried just about everything I that I can think of to prevent it,
but it looks like I'm going to use double textboxes and just keep moving
them around.

In the mean time though I wonder if someone from the OpenNetCF team
could comment on the following code. I can't seem to get OpenNetCF
sources to compile right now, but I would like to add something like
this to TextBoxEx to save lots of work in my application.

I was planning on adding the following to TextBox.cs - the TextBoxEx
class...

1. DrawControl property - bool -
TRUE - Control is painted
FALSE - Control is not painted
Default value = TRUE

2. Add OnPaint override to TextBoxEx
I just copied the OnPaint override from the #DESIGN section
of TextBox.cs. I hope it will work on the handheld at runtime
as well.

I will only draw the control if DrawControl property is true

So to prevent flicker, I would use something like this in my application...

TextBoxEx1.DrawControl = false;
TextBoxEx1.SelectedText = "HELLO";
TextBoxEx1.ScrollToCaret();
TextBoxEx1.DrawControl = true;
TextBoxEx1.Refresh():

Here is my suggested changes. Comments appreciated
--------------------------------------------------------------------------------
/// <summary>
/// Represents an enhanced TextBox.
/// </summary>
public class TextBoxEx : System.Windows.Forms.TextBox, IWin32Window
{

...

#if !DESIGN
private bool pDrawControl = true;

...

public bool DrawControl
{
get { return pDrawControl; }
set
{
pDrawControl = value;
}
}

/// <summary>
/// OnPaint override - taken from #IF Design section
/// </summary>
protected override void
OnPaint(System.Windows.Forms.PaintEventArgs pea)
{
// This is my amazingly complex change
if (pDrawControl) {
Pen pb = new Pen(Color.Black);

SolidBrush bw = new SolidBrush(Color.White);
SolidBrush bb = new SolidBrush(Color.Black);
SolidBrush bg = new SolidBrush(Color.Gray);

if (this.ReadOnly)
pea.Graphics.FillRectangle(bg,this.ClientRectangle);
else
pea.Graphics.FillRectangle(bw,this.ClientRectangle);

pea.Graphics.DrawString(
this.Text,
this.Font,
bb,
2,
2);

pb.Dispose();

bb.Dispose();
bw.Dispose();
bg.Dispose();

// base.OnPaint(pea);
}
}

...
#Endif //!DESIGN
 
P

Peter Foot [MVP]

The OpenNETCF TextBoxEx doesn't do any painting of it's own - it inherits
from the TextBox control. The display is therefore a native edit control.
What other code do you have which interacts with the TextBox or the form
which may cause this behaviour? are you setting the focus to another control
for example.

Peter
 
A

a

Peter said:
The OpenNETCF TextBoxEx doesn't do any painting of it's own - it inherits
from the TextBox control. The display is therefore a native edit control.
What other code do you have which interacts with the TextBox or the form
which may cause this behaviour? are you setting the focus to another control
for example.

Peter

I believe the Textbox has focus while I am working on it. I did call
textbox1.focus previously, but I haven't checked it it still had focus
before changing the selection.

The code looks like this. (vb.net for CF)

'TextBox1 has about 5K characters
' starts being noticeable after several screen fulls
TextBox1.SelectionStart = oldPos
TextBox1.SelectionLength = oldLength
TextBox1.SelectedText = NewString 'This line causes textbox to
'jump to top of text quickly
TextBox1.ScrollToCaret() 'This causes it too

I can send you anyone an example application to demonstrate my problem
if you like. I've seen several people mention it in the newsgroups and
websites, but I've never seen a solution. I tried using
Win32Window.SendMessage(hwnd, EM_SCROLLCARET,0,0), but I get the same
results with that too.

I'm using VS .NET 2003, on a Dell AXIM X5.
 
A

a

Peter said:
The OpenNETCF TextBoxEx doesn't do any painting of it's own - it inherits
from the TextBox control. The display is therefore a native edit control.
What other code do you have which interacts with the TextBox or the form
which may cause this behaviour? are you setting the focus to another control
for example.

Peter

You had the Answer Peter.

FOCUS!

The control did not have focus because the user tapped a button, then
the click code tried to change the SelectedText while the button still
had focus.

So, if textbox flicker is happening it is because the textbox control
doesn't have focus. It isn't mentioned in the MSDN, but adding that
little line made it suddenly work.

Thank you SO MUCH!!

- Russ
 

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