This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I suspend painting for a control and its children?
Winforms Double Buffering
I have a form that draws a bunch of user defined controls (81 of them) each of which contains another ten controls (total 8,100 ish). It all seems to be drawn sequentially (takes ~1 second or so).
I've looked at these two questions, and followed the ier reccomendations (setting this.DoubleBuffered = true on both the form and the custom control, and SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); on the form, but that did not appear to do anything.
Is there a way to save the paint function of the form, save it in a delegate, swap in a dummy function so that the form does not get redrawn AT ALL, and then putting the real function back and triggering it.
I assume there is a more standard way of doing this, but I was unable to find it (it is not SuspendLayout/ResumeLayout those just deal with layout logic).
How can I do this?
This is the answer: (a link).
But think of it. If you prevent Paint from being happened, how your UI controls should be updated when user interacts with them? There will be no button glowing, no textbox cursor flickerinig and so on.
If you are okay with that then why you just not RenderToBitmap the container of your user controls as its background image?..
Related
This question already has answers here:
change active form to show another form
(4 answers)
Closed 9 years ago.
Is there a way to make it so that, when I click a button, instead of needing to bring up a new dialog for my next screen, it just 'replaces' what's on the screen? (For instance, the default screen is a main menu with some buttons, which lead to more in depth screens, like Parts Inventory, and all of it's options now being on the screen).
In the realm of stacking panels, how efficient is this for a larger system? I need at least 30-40 screens going on.
Edit: I'm using Visual Studio 2010 to do this project for school, and using Windows 7 with the default packages.
Instead of creating a new forms for each screen you can create a new user control for each screen. Then you could have one form with a panel and loading the control into the form instead of creating a new form showing it and hiding the old one. Youcould do something like this:
...
MyControlWasForm1 form1 = new MycontrolWasForm1(); // this is a user control
panel1.Controls.Clear();
panel1.Controls.Add(form1);
...
You could also keep references to the old control if you want to be able to go back then just reload it into the panel.Controls when needed. Keep in mind if you don't have a variable referencing the control outside of the method, then when you remove it from the controls it would get garbage collected.
...
//property on main Form
public MyControlWasForm1 Form1 { get; set; }
...
//in method
if(Form1 == null) Form1 = new MycontrolWasForm1(); // this is a user control
panel1.Controls.Clear();
panel1.Controls.Add(Form1);
...
You can use multiple panels stacked and show/hide them depending on what you want to display
I'm having a problem focusing on a Control within a Window in WPF.
On the constructor of the Window I add a TextBox as follows:
TextBox tb = new TextBox();
tb.Text = input;
tb.SelectAll();
tb.Focus();
I also call the Focus() method again on the Window loaded event.
The problem is that sometimes the window focuses and other times it does not! This is very strange behavior and I was wondering if someone has had the same problem and might be able to give me some guidance.
-- UPDATE
I have found what is causing the problem but no solution yet. I am using WIA to scan a document, this brings up a ProgressBar which makes my entire application lose focus.
I have tried calling Application.Current.MainWindow.Focus() but this does not restore focus, any suggestions?
From memory, I think the last call on window creation is the Windows.Activated event. You could try running your focus code on that event?
Like I mentioned in my updated question my entire application loses focus due to another dialog being displayed, what I ended up using was:
Application.Current.MainWindow.Activate();
This returns focus to my application and when my Window with the TextBox is shown, Focus is applied to this control.
Hope it helps someone!
I have written a C# 4.0 Windows Forms Application that creates two panels which are both populated exclusively with text labels. I would like to add functionality to my application to print these panels exactly as they appear on the form, but I only want to print these panels and their contents, with no background or other parts of the form.
This link was especially helpful as an overview, but so far I'm only able to print what amounts to a screen capture of the form. The entire form is included, buttons, trim, background, and all. It looks like I need to "rebuild" the form by creating a graphics object (I use the term generically, as I'm not sure what specific graphics-related class I need to employ) and somehow transfer the contents of the panel into this object.
My question is: what classes and/or methods should I research in order to build this graphical object? Also helpful to know: are there any handy tricks within the .NET framework or any libraries out there to automatically fit the result to a single page when building the PrintDocument object?
OK, I found one way to do it (love to my Perl bros out there):
Wrap both panels in another parent Panel. Let's call this Panel "parentPanel." Create a bitmap (memoryBitmap)that is the size of parentPanel (parentPanel.Size).
Next: parentPanel.DrawToBitmap(memoryBitmap, new Rectangle(parentPanel.Location, parentPanel.Size)
Then, in the print page event handler: e.Graphics.DrawImage(memoryBitmap, 0, 0)
I'll leave this open in the likely event that someone else has a better idea.
I have a WinForms application that, at some point, will do some calculations that take a couple of seconds. During this time, I disable the controls. Additionally I want to display a popup that kind of 'blocks' the application more visibly.
I tried placing a Panel on top of the Form in order to do so, but there I'm somehow not able to add any text to it. I.e. the Labels and ImageBoxes I put there are somehow not displayed or behind the Panel.
Any suggestions how I could achieve this in a quick and easy way? .. I mean, without redesigning the whole Form.
Your problem with the things not showing up in your additional panel is that everything is happening in the same thread, but your calculations are blocking everything from appearing because they are hogging up the thread. You should look into using a backgroundWorker, which is one of the tools in WinForms. You should throw the function call that performs the calculations into the DoWork method of the backgroundWorker, then the form itself can continue to function during the calculations.
See http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx for examples
Create a Dialog Box (windows form) to popup during the processing, then you can have the Dialog Box close once your processing is completed.
By using a Dialog Box not a Message Box, you can still have control over closing the window when your processing is done.
You could create a simple UserControl with just a Label and an ImageBox, maybe with public setter for their values. You can add it to your form setting its Visible property to false. Then you can make it visible during your calculations and go back top invisible in the end.
I have a zoomable form-interface like MS Word. The form contains +70 controls (Richtextbox, checkbox, etc) that are positioned all over the form.
The problem is that generating the form takes 2.5 seconds. Adding the 70 controls with panel.Controls.Add( ctrl) alone takes 1 second. (16 ms per call).
Is there a way to cache the whole blank form ?
Perhaps someone smart knows another method to generate the form ?
Are these controls being added through the designer, or are you adding them manually in code? If it's the latter, I would recommend calling SuspendLayout on the Form before you load the controls, then ResumeLayout(true) after you're finished loading.
Additionally, if these controls are being added while the form is visible, then suspending and resuming drawing can be a great help. See the accepted answer to this question for more info on how to do that.
EDIT
Why do you need 70 RichTextBox controls? Have you considered redesigning the form so that so many aren't required (reusing some for multiple purposes, for instance)? Have you investigated your custom control to see if any speed can be gained in your own constructor?
After lots of tracing we discovered that the constructor for a Custom Inherited RichTextbox takes 7-8 ms per call.
70 controls means 70 * 8 = 560 ms.
Cant we just copy or clone an already constructed CustomRichTextBox ? Would that make a difference ?