How to send graphics to another form - c#

I have 2 forms and in those 2 forms i have two board.
In the first form user put some object on the board. And then it clicks next. I want to send the exact graphics to the other form so i can draw the same things in second form.
I've tried to use Graphics.Save() and Restore() functions, but didn't work. Any suggestions ?
Thanks in advance.

You may need to call Invalidate() on your second form to make the picture appear after you have passed it? If you minimise and then maximise your second form and the image appears, then that will be the problem.

Related

See elements outside of the main form in C# Forms?

I'm a little bit frustrated with forms and feel like there have to be some features that I do not know yet (I'm still new to this). Currently I want to toggle two panels on button click, and have one panel move out on the left while the other moves in on the right. I can get it working somehow, but I'm losing so much time because I can only work on the panel that I see and have to keep dragging the panel that is out of the main form into the main form in order to see it and work on it.
Is there a way to also draw elements that are technically out of the draw range?
The panels should have transparent background and the main form background should not move , so moving in another form does not look like a viable option.

Loading Windows Form before Showing

I have a Parent Windows Form from which there are multiple Child forms. Almost every single one of these has the same background image, the parent is a MDIContainer.
It is working properly, my problem is that when I call the Form.Show() method, after selecting a MenuStrip item for a child Form, the form is already showing WHILE the background image is being set.
I think this would be solved by first loading the BackgroundImage and then, after a while, showing the form itself.
Any possible advice? Thanks in advance
Best fix I think would be to set the right background image for each child form at design time and simply call. show to display the form..
Setting BackgroundImageLayout to ImageLayout.Stretch seems to ease the transition, however it still looks a bit odd. I'll keep trying stuff

Center form during runtime

I've been developing a game. As such, I need to be able to display information that are group accordingly. I've been using a menu system that allows me to utilize 1 form to display different bits of grouped information using the panel control. Using a timer on the form, it automatically changes in size depending on just how much information is displayed, using the currently visible panel as a guide for dimensions.
I have the form's screen position behavior set to Center Screen. This works great.
However, there is a problem. The form is only truly centered when it is the size of the biggest panel - the log in screen - is active, being that it is the first menu a user sees.
My question is, using code, could I get the form to be re-centered during runtime? If so, how would I go about doing so?
You can call the method Form.CenterToScreen
or follow this answer!
StartPosition is set to CenterPosition but my form is not centered

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()

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