Why Refresh() does not work on a control on a TabPage?

N

Neville Lang

Hi all,

I have a TabControl and on one of the Tab Pages I have a Panel control. On
the panel I create a bitmap image in background so that when the Tab Page is
selected, it displays the bitmap fast. Also, on the Panel, I have a Button
that I call ReDraw and its Click event is mapped to a handler. The handler
code redraws the image in background. The reason for this is that if a
setting in the App changes then a user can click the button to force a
redraw of the bitmap image to reflect the setting change. However, my
problem is that the image does not get updated no matter how many setting
changes are made. On the button's click handler, I have called various
Refresh() methods of the Panel, the Tab Page, the TabControl and even the
main Form but nothing seems to update the Bitmap image on the TabPage.

What I did find was that after I made an App setting change then I tapped
another Tab Page then tapped the original Tab Page with the Bitmap image, it
was redrawn correctly. I then coded the button's click handler to set the
TabControl.SelectedIndex to another tab then set it back again, and this
combination does force the Bitmap image to update.

This seems rather an awkward way to force a refresh. At present, there does
not seem to be any other way to refresh (redraw) a control sitting on a Tab
Page without setting/resetting the SelectedIndex of the TabControl. Is this
correct? Any ideas or workarounds to make this thing easier?

Regards,
Neville Lang
 
N

Neville Lang

Hi all,

I should have also mentioned that I am using WM2003 and SP2 on the device .
I had put this problem on one side for a while until now. This problem was
also occurring under SP1.

Regards,
Neville Lang
 
N

Neville Lang

Alex,

Please find below a sample of my code and appreciate any comments.

MAIN FORM
// My controls
private MyControl dcc;

MAIN FORM'S CONSTRUCTOR
this.dcc = new MyControl();
this.dcc.Parent = this.pnlShow;
// Load this control's size to match that of the container
this.dcc.Top = 0;
this.dcc.Left = 0;
this.dcc.Width = this.pnlShow.ClientSize.Width;
this.dcc.Height = this.pnlShow.ClientSize.Height;

A METHOD OF THE MAIN FORM
private void ResetMyDrawing()
{
// Prepare MyControl in case it is drawn
this.dcc.SetupMyControl();

// Draw the graphic in background ready for later display
Bitmap backgroundBmp = new Bitmap(this.tpShow.ClientSize.Width,
this.tpShow.ClientSize.Height);
Graphics grfx = Graphics.FromImage(backgroundBmp);
this.dcc.DoMyControl(grfx, this.ForeColor); // Draw MyImage in the
background
this.dcc.MyImage = backgroundBmp; // Update MyControl's
MyImage property with the new drawing
}

MY CONTROL
private ArrayList NumHLines = new ArrayList();
....
.... // Various ArrayLists
....
private Bitmap myimage; // The Image to draw when painting the surface
public Bitmap MyImage
{
set
{
myimage = value;
}
get
{
return myimage;
}
}
public void SetupMyControl()
{
// Delete previous drawing coordinates
this.NumHLines.Clear();
...
... // Clear various ArrayLists
...

// Load ArrayLists with new values for drawing after App Setting changes
...
...
...
}

public void DoMyControl(Graphics grfx, Color clr)
{
// Fill background color
SolidBrush backBrush = new SolidBrush(SystemColors.Window);
grfx.FillRectangle(backBrush, 0, 0, this.Width, this.Height);

// Only Display if SetupMyControl method has already loaded this class's
arrays with drawing info.
if (this.NumHLines.Count > 0)
{
...
...
...

// Draw some stuff
grfx.DrawString(this.strName, this.Font, brush, 2.0f, 1.0f);
grfx.DrawLine(pen, hl.Left, hl.Top, hl.Right, hl.Bottom);
...
...
...
}
}

protected override void OnPaint(PaintEventArgs e)
{
// Draw the image created in the background
e.Graphics.DrawImage(this.MyImage, 0, 0);

// Pass the args on up the chain
base.OnPaint(e);
}

Regards,
Neville Lang
 
N

Neville Lang

Alex,

I should have also added on my last post that my Redraw button's Click event
handler simply calls the method ResetMyDrawing() on the main form, however,
nothing ever happens to change the bitmap image.

Regards,
Neville Lang
 
N

Neville Lang

Alex,

This has been the first opportunity that I have been able to get back to my
Refresh problem, and I have now solved it.

For some reason, I could not get your attached solution to work on my
system, however, the two .CS files were enough for me to reproduce your
files.

I began with your test solution and then added some extra code to more
closely replicate my problem in my app. Following your comment that
basically my code looked OK, the extra code I added continued NOT to refresh
the graphic. However, I have now found the problem in that I needed an
Invalidate() at a certain point to trigger the refresh. After I added this
Invalidate() line of code, the problem of the refresh has now been solved.

As feedback, I have added the two changed .CS files and have indicated the
extra line of code that fixed the problem.

Thanks again for your help on this problem.

Regards,
Neville Lang
 

mcj

Joined
Mar 10, 2018
Messages
1
Reaction score
0
HI there,
I have a similar problem with refresh on my tabControl. Listview doesnt get refreshed in tabcontrol.
Where do i need to try invalidate() method?

I know its an very old post, but please respond. am really struck at this refresh issue
 

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