Suppose there is a picturexox on the panel
and there is a datagridview on the picturebox.
I use the following code to capture the panel:
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
bmp.Save(#"test.jpg");
But it seems that only the picturebox has been captured, the datagridview on it is missing.
How can capture the picturebox together with the datagridview?
The DrawToBitmap method will capture the graphics drawn in the Paint event and all nested controls. If one is missing it isn't nested either directly of indirectly. Here is how this probably happened:
The designer behaviour of Panels and PictureBoxes is different.
Panels, like GroupBoxes, TabPages and a few more are container controls.
This means that any control you drag onto them with your mouse gets nested in them.
PictureBox is not a Container, like, say a Button or a Label..
To nest your DataGridView in the PictureBox you have a choice of
moving it there with the keyboard (!)
nesting it in code ( yourDGV.Parent = yourPBox1;)
Do test the success of the keyboard way by moving the PictureBox to see if the DGV moves with it..
Update:
If you nest the DGV in the PictureBox, which itself is nested in the Panel, all will be well and the result will looks just like what you see.
However, if you nest both DGV and PictureBox in the Panel and merely let them overlap, a strange bug will appear: Multiple overlapping controls are drawn in reverse, See here for a post and a link about this issue, which is documented
Controls inside containers are rendered in reverse order.
but obviously still a bug.
Related
Suppose there is a picturexox on the panel
and there is a datagridview on the picturebox.
I use the following code to capture the panel:
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
bmp.Save(#"test.jpg");
But it seems that only the picturebox has been captured, the datagridview on it is missing.
How can capture the picturebox together with the datagridview?
The DrawToBitmap method will capture the graphics drawn in the Paint event and all nested controls. If one is missing it isn't nested either directly of indirectly. Here is how this probably happened:
The designer behaviour of Panels and PictureBoxes is different.
Panels, like GroupBoxes, TabPages and a few more are container controls.
This means that any control you drag onto them with your mouse gets nested in them.
PictureBox is not a Container, like, say a Button or a Label..
To nest your DataGridView in the PictureBox you have a choice of
moving it there with the keyboard (!)
nesting it in code ( yourDGV.Parent = yourPBox1;)
Do test the success of the keyboard way by moving the PictureBox to see if the DGV moves with it..
Update:
If you nest the DGV in the PictureBox, which itself is nested in the Panel, all will be well and the result will looks just like what you see.
However, if you nest both DGV and PictureBox in the Panel and merely let them overlap, a strange bug will appear: Multiple overlapping controls are drawn in reverse, See here for a post and a link about this issue, which is documented
Controls inside containers are rendered in reverse order.
but obviously still a bug.
I created 2 empty labels, using winforms, to cover some groupboxes
They were created exactly the same way, but they dont behave the same way
The first one:
The second one:
The first covers everything, the second one only covers one groupbox, and displays the other two.
I tried the right click+ send to back /send to front, but I believe this doesn't mean anything in the code, it only purpose is to allow you to visualize better what you are placing in your form, I guess.
Can someone shed some light?
Groubox, Panel, TabPage etc are containers in which we keep controls. Form itself is also a container..
When you drag a label or any other control to place in a form, and if you want to hide the other controls, you should place the label in the form itself and the label should not become child of any of the groupBoxes.
For that the left top corner of the label you drag should not go inside any of the groupBoxes. This is only when you drag. If you want to position at a point inside groupBox, then Drag the control outside the groupBox and then use keyboard arrows to position, this will not let the control to become child of the groupBox. You can also set position on property window.
I have an array of picture boxes that are arranged in a square. I want to put a larger, mostly transparent picture box over the top. But when I do it covers the other picture boxes and just draws the background of the form.
Is there a way to get it to have all the other picture boxes show where it is transparent?
Transparency in WinForms isn't great. Some controls have transparency support, others don't. Some controls can be subclassed to enable this (by calling Control.SetStyle() with the SupportsTransparency flag). I believe this can be done with PictureBox.
However, transparency in all WinForms controls works by having the transparent control call its parent control to draw the background before the child control draws. This means that you cannot have two sibling controls and expect transparency on one to show through to the other. Sorry!
All that said, it would be possible to code your own workaround to support this. It would involve subclassing PictureBox and clever coding in the OnPaint override to locate sibling controls and manually trigger painting of them into in-memory bitmaps. Lots of gotchas with this approach.
Try WPF!
Here's a tip to get the desired result:
Create multiple copies of your top image.
Add each copy to the Controls of each picture box it should cover.
Adjust location of each copy according to the offset of each picture box to be covered.
So you will see every copy of your big image covering each picture box as if they were a single image.
i tried to search for this on google, but didnt seem to find anything related that could help me.
My problem is this, i have a panel in which i draw a cube, and i added a group box with 3 radio buttons and 4 normal buttons(these do the rotation of the cube)
For testing i have another button added on the panel but not in the group box.
The problem is this, when i push on any button it doesn't update the cube's rotation, only when i move the mouse on the test button(over it)
If i try to move the buttons outside the group box then all works well, but they don't work if they stay inside the group box.
Does anyone know how i can fix this?
May I suggest that instead of redrawing the panel you create a Bitmap for drawing your (rotated) cube and use a PictureBox to display it? The PictureBox could be inside the Panel together with the GroupBox.
The flickering comes because the panel redraws his background first and then raises the Paint event. To avoid this you must to create a custom control and do the drawing in OnPaintBackground protected method. This seems a overkill. Or you can write to a Bitmap and put it in the BackgroundImage property.
I'm writing a form in C# and have several panels. I need to draw a line between two of the panels. I've found online several ways to go about this, the most promising appears to be to create a third panel, make it transparent, place it on top of my original panels and draw the line here.
I'm not able to get the panel to be transparent, even if I set its BackColor and ForeColor properties to transparent (in code or in design view of VS).
Any ideas on how to make the panel itself transparent (or not Visible) but have the line I draw on it still visible on top of everything else?
Thanks in advance.
No, it's transparent. See this by giving the form's BackgroundImage a value. You'll see it through the transparent panel. Of course, that's not the kind of transparency you want, you want stacking effects to work. There is no direct support for that.
If you want layers to work then don't use controls. Use the Paint event to draw. Now there's no problem, if you want transparency then just don't paint. Draw a line across an image simply by drawing the image first. This is also the rendering model of WPF.
You can actually do this pretty easily as your own UserControl. Here's a code example:
Drawing on top of controls inside a panel (C# WinForms)
This is similar to what you were originally attempting to do, only instead of drawing a line on top of a transparent panel, this code creates an irregularly-shaped user control (which happens to be in the irregular shape of a line).