C# resizing issue - c#

I have a form with a TableLayoutPanel docked to it, and with a TextBox, Image, TreeView and ListView within that Panel.
When the user resizes the form, these controls resize with it, but there is a noticeable delay if the user resizes quickly or if the user maximizes the entire form; the delay is so considerable that I was able to screenshot it:
You can also notice that the "Options" button is displayed twice, once where it should be (on the upper right-hand corner) and once in the middle of the search box (where it was before the form resized). This isn't supposed to happen, but is also caused by the delay.
Shortly after this screenshot was taken, the form looked fine, but does anyone know of a way to eliminate or reduce this delay?
I edited the image for privacy reasons, but there were only 60 or so items in the listview.

Do you have any special code handling the redrawing?
If so are you using the BeginUpdate() method and the EndUpdate() method?
From msdn:
Prevents the control from drawing until the EndUpdate method is called.
These methods (depending on your situation) could cause the components not to be updated until after the resize has occurred. This might stop the items appearing twice on the screen and speed up the resize.

Maybe you have virtual items in your listview, and your items are recalculating slowly? Do you have any code that is running when listview need redraw?

Related

How to prevent premature painting of MemoEdit control when the form is loading

How to prevent premature painting of MemoEdit control when the form is loading?
I have a form which is DevExpress.XtraBars.Docking.ControlContainer.
Red area in the pic.1 is the devexpress LayoutControl which consists of a bunch of LayoutControlItems, almost all of them have a TextEdit editors, but the second one from top is MemoEdit.
In the pic.2 is the what i see when the form is loading (after i called Show()).
As you can see, other controls are not drawn or displayed at the moment, but the MemoEdit do. I tried to use LockWindowUpdate, Begin/End Update, SuspendLayout but isn't working.
I would like to know what are reasons of this behaviour.
I want to specify something. Even when form is loaded and the LayoutControl was once filled with controls, MemoEdit still doing strange things, for example when I am switch between horizontal tabs (pic. 3 and pic. 4). At the moment when I select tab, that was already once "loaded" MemoEdit starts painting at old tab.

Avoid reload/redraw all components when swapping TabPages

I have multiple TabPages on a TabControl. Some of these TabPages contain up to 100 Panels in a FlowLayoutPanel. Each of these Panels also contain a bunch of different controls...
The issue is simple: when I swap between TabPages, the one containing lots of controls take ages (1-2 seconds) to load.
I would like them not to lose "focus", to keep the components in memory or something when left for another tab, so that they don't have to reload everything when we come back on it.
Is it even possible? Or is it the drawing itself that take all this time? I do not do operations when changing tabs. Just displaying them.
Clarification edits:
Hum, maybe I was not clear enough. The problem is not how the stuff is drawn when being loaded. I have fixed this issue my own way (working with selected tab events). My problem is the time taken by the page to actually load. When I enter the TabPage containing lots of controls (in FlowLayoutPanel), it will load for about 1-2 sec THEN be displayed. I would like that to be almost instant, all controls being already available or something. But maybe it is not possible? Maybe it is the time taken to draw all those controls that is long?
What's weird is that it takes less time to actually generate all those panels (100 in less than 0.5) for the first time, than to redisplay them when leaving/entering the page after.
The displaying is also instant when coming from Minimized state (the form itself). I would like that to be the same when changing form state than swapping between tabs.
This is known issue with FlowLayoutPanel when having many controls inside. It tries to layout all of the controls and each time control is positioned part of the screen is redrawn.
To reduce this, try to enable double buffering on your form or FlowLayoutPanel control. This allows to do all rendering in memory first and swap buffers once when it is done.
Add this code:
public static void SetDoubleBuffered(System.Windows.Forms.Control c)
{
//Taxes: Remote Desktop Connection and painting
//http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx
if (System.Windows.Forms.SystemInformation.TerminalServerSession)
return;
System.Reflection.PropertyInfo aProp =
typeof(System.Windows.Forms.Control).GetProperty(
"DoubleBuffered",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
aProp.SetValue(c, true, null);
}
Then just call this on your form or control.
SetDoubleBuffered(mainForm);
SetDoubleBuffered(myFlowPanel);

WinForm Controls are transparent, and not displaying properly

When a form loads, I'd like it to show a loading image (within a Picture Box) and a standard Windows label with some text. However, all I see are white boxes, or sometimes I see another form underneath. How do I get the image and label to display properly.
I've tried setting AllowTransparency to false when the form loads, and also setting the Transparency Key of the form to some other colour, but nothing has worked.
The project is C# .Net v3.5 (also tried v4 and v4.5).
Any ideas?
First, you can't display an image, busy-wait, and then change the image - this will never redraw anything, leading to the symptoms you describe. To "wait" you will need to return control to your main application loop so it can continue to process messages (e.g. to handle redraw requests for your window). One way to do what you want is to display your initial state (splash screen) and then use a timer to call you back later to change the display to your second state.
The next problem you face is using forms controls with transparency. Most controls treat "transparent" as "fill your background with your parent controls color", which is not what you want. An easy way around this is to implement a Paint handler and draw the image and text for yourself - this gives you much more control of how your display looks, and will also allow you to get a cleaner redraw (no flicker or other problems caused by the display being built up but by bit in several controls)
Lastly, consider implementing your splash screen display as a separate control/form that you show above your main form during loading, as that makes it easy to "overlay" on your main form without having to change its design at all.
Just write formObjectName.Refresh() after formObjectName.Show()

Graphical hiccups in C# User Control - Resize obscures components

I'm experiencing difficulty with a custom-made User Control, and my searching on Stack Overflow, MSDN, and Google didn't pop up any troubles quite like the one I'm experiencing.
I have a very simple User Control: It's a label, a text box, and a button, with a SaveFileDialog and a FolderSelectDialog available. The text box and button are anchored Left,Right and Right respectively, with the intent that if the control is resized larger, the text box will enlarge to fill the gap, and the button will stay on the right edge of the control.
The problem I am encountering is that when the control is enlarged, the area to the right of the default width of the control becomes blank space when the project is built and run. The pictures here will illustrate what I mean:
In editor:
Running:
The control is smallish in its design window, but when I add it to a form and widen it, it behaves as intended. However, when I run the form the control was added to, half the control isn't visible.
I suspect that I'm overlooking something fairly straightforward, but I wasn't able to find anything addressing this point in my search. Help would be much appreciated.
My guess is that there is a panel or something that is added to your control and will be brought to front somehow runtime.
from property window's top there's a combo from which you can select all the controls in your User Control.
check if all the controls are what you want.
if you find that panel or anything delete it :)
EDIT:
alright this was not your problem.
now I can only assume that you have set some manual sizes to your user control, i.e. in its constructor. in that case designer will show the correct size of you user control,
now some other place in your code, you have set the user controls size manually again. if the layout is suspended and size changes, I think that the anchored controls' size will not change automatically.
if this is your problem, it is probably hard to find.

C# Only refresh PictureBox/Panel when tell them to refresh?

I'm making a bomberman game in a C# windows form application. It has over 300 pictureboxes (walls) that are placed on a panel. The picturebox of bomberman himself is also on that panel.
When the location of bombermans picturebox changes, all the controls on the panel are automatically refreshed. Because there are so many controls on that panel, and because the location of the picturebox changes multiple times per second, the program becomes laggy when I try to move.
I want to have control over the refresh event of the panel (and it's controls), because I think my problem is solved when only the pictureboxes that need to be refreshed, are refreshed programmatically.
I hope someone can help me out with this!
Ruud.
If you move the child, the parent has to be refreshed because it may need to draw the area where child was located previously. It would also mean that all children (of parent) would get refreshed.
OTH, using so many controls may not be a good idea. I would suggest you to keep data structures describing walls and then use it to draw it within Panel (or your custom control). You can write your own logic for hit testing (mouse or keyboard click within wall boundary) by capturing mouse/keyboard events at Panel/Parent level. With correct organization data structure, hit testing can be very efficient.
You are trying to paint the whole form which will surely take time . If you want to change only a part of the form, which in your case is Moving the bomberman to a new position, Only invalidate the area you want to repaint and then pass it to the Invalidate method.
Do something similar to this.
//Invalidate previous position of bomberman
Rectangle invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);
//Add code to move your picture box and then call above two lines again
invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);
Note sure but somthing similar polished code would work...
Here is a link to an example for reference. http://msdn.microsoft.com/en-us/library/ms229628.aspx

Categories