How to create Transparent panel in c#.Net - c#

I have a Panel on windows Form with few controls inside panel,
Can i make panel completely transparent.
(It should give the feel that controls are placed directly on Form)

If you go to the BackColor property, and change the Selector to "Web" the first choice is Transparent (at least it is in my VB IDE). I believe that the BackColor of the Panel would inherit the color of the component it is on.

I assume it is WinForms app.
Try this in Form.Load event:
private void Form1_Load_1(object sender, EventArgs e)
{
panel1.BackColor = Color.FromArgb(0, 0, 0, 0);
}
where panel1 is the panel you want to have transparent.
It will make the color transparent. You can have other controls on the panel.

Related

How to set a transparent winform, into a panel transparent panel, to show as background what's behind?

I'm coding a program which has a principal form with a pictureBox that i use as a common background image, i also use a panel, who has the pictureBox as a Parent, and I use that panel to join my other winforms in it, lets call this winforms "A" , my problem is that i set the A winforms as Transparent, so i can see the pictureBox that is behind the panel, now, my problem resides that when i join the A winforms, they show as transparent, but with the issue that the don't show the pictureBox, they show what's behind the program.
This is the PictureBox that i want to show behind the A winforms
This is what I'm talking about, as you cab see, the program is showing me my own desktop wallpaper
Please I need your help, as you can realise, as you can realise, my english is not so good, so i'm
apologizing myself in advance. :)
To set the form's background, you don't need to add the PictureBox. You only need to set its BackgroundImage Property.
this.BackgroundImage = Image.FromFile(#"D://pic/b.jpg");
Then set Panel to transparent.
panel1.BackColor = Color.Transparent;
panel1.Dock = DockStyle.Fill;
To set FormA to transparent, also need to call SetStyle in constructor.
public FormA()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
}
Here is the simple demo.
private void Form1_Load(object sender, EventArgs e)
{
FormA formA = new FormA();
formA.FormBorderStyle = FormBorderStyle.None;
formA.TopLevel = false;
formA.Dock = DockStyle.Fill;
panel1.Controls.Add(formA);
formA.BackColor = Color.Transparent;
formA.Show();
}
Hope this can help you.

Auto-resize multiple Forms rendered on Panel

I have this Form with Panels. This Form has three Panels.
One Panel is collapsible and acts as a sidebar, the other one sits at the top and is there for showing title, the last one is the placeholder for the Forms being opened by clicking on one of the items being catered in Panel one.
Now what I want to do is resize (grow and shrink) the size (width only) of the placeholder Panel and the Form that is opened on the Panel according to the state of Panel one, which could either be expanded or collapsed. The dock is not working.
After some clarifications, it appears that the desidered layout and behaviour of the described Form is similar to this sample disposition:
A WinForms Form is embedded in another Form, and placed inside a Panel.
This Guest Form is stripped of its TopLevel coat-of-arms and parented to central Panel, as shown in this graphic example:
How do you dock these Panels to get this layout:
The Green Panel stays on top of the Form.
The DarkGray Panel lays on the left hand side of the Form.
The Gray Panel occupies the remaining space.
Insert three Panels on a Form container.
The Green Panel needs to maintain its position, it will never change:
Right click → SendToBack (!important :).
Dock → Top.
The DarkGray Panel is positioned under the Green Panel, on the left side of the Form. It needs to resize itself when needed, but will never cover the Green Panel:
Dock → Left
The Gray Panel needs to occupy the remaining space. It needs to resize itself when needed, but it will never cover Green Panel or Dark Gray Panel:
Right click → BringToFront (!important)
Dock → Center
The highest priority when docking, is assigned to the element which has the lowest z-order in the stack: the Green Panel, here.
The lowest priority is assigned to element with the highest z-order: the Gray Panel, which will then shrink and stretch among all other elements with higher priority (following the z-order).
How to embed the Form:
The easy part. It's a Form in our Project, no need to perform any magic to keep it alive when re-parented:
(This is just for 1 Form. With more Forms, you'll need something like a List<Control>:
//Define here the Form which will be embedded
[Your Form Class] EmbeddedForm;
private void button1_Click(object sender, EventArgs e)
{
EmbeddedForm = new [Your Form Class]() {
TopLevel = false,
Parent = panContainer,
Location = new Point(4, 4),
Enabled = true
};
EmbeddedForm.Show();
}
private void buttonShrink_Click(object sender, EventArgs e)
{
//Maybe insert a classic dotted mini-button to re-inflate the sidebar when needed
panelSideBar.Width = 6;
}
private void panelContainer_Resize(object sender, EventArgs e)
{
Rectangle rect = panelContainer.ClientRectangle;
rect.Inflate(-3, -3);
EmbeddedForm.Size = rect.Size;
}
If you allow your Container Panel to AutoScroll its content, the Resize event is not necessary.
Edit:
A PasteBin of the complete source code of the Form in sample graphics:
Embedded Forms

C# splitContainer1 resize child control

I have a splitContainer. I want to resize the form inside the splitContaner's panel to scale with when I move the splitter as below. But my form doesn't get redrawn. Any suggestion, thank so much!
private void splitContainer1_SplitterMoved(System.Object sender, System.Windows.Forms.SplitterEventArgs e)
{
// Define what happens when the splitter is no longer moving.
Cursor.Current = System.Windows.Forms.Cursors.Default;
statictisTableDisplayForm1.ClientSize = new Size(statictisTableDisplayForm1.Width, splitContainer1.SplitterDistance);
statictisTableDisplayForm1.Invalidate();
statictisTableDisplayForm1.Refresh();
Refresh();
}
Form supposed to be a top-level control which represents a window of your application. You should not embed forms as controls into other forms (well, unless there is no other option).
Usually, you should not resize and/or move controls manually. There are several layout options which allow automatic resizing of controls when the size of their container changes: Anchor, Dock.
So better create a UserControl which will contain controls and logic of your StatictisTableDisplayForm and place it to SplitContainer panel with Dock set to Fill. That will automatically resize user control when you move the splitter.
Note: if you have to use StatictisTableDisplayForm on it's own too, then just place same user control to this form.

How to reduce opacity for form only not for controls in c#?

I want to reduce opacity for form only, not for controls on forms. I reduced opacity for form, but the panel opacity also reduced.
I don't want to reduce panel opacity. How can I to do this using c#?
Instead of setting the opacity for a form, try to set the form to transparent backgroundcolor and use a seperate element (in WPF that could be e.g. a grid) to fill your form and set the opacity there.
Refering to this post, you can use HatchBrush Class.
using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
var hb = new HatchBrush(HatchStyle.Percent50, this.TransparencyKey);
e.Graphics.FillRectangle(hb,this.DisplayRectangle);
}

Winform: Panel with scroll needs to have their width/height defined?

I've a winform usercontrol, that has a Panel, which is containing some TableLayout(which have also some other user controls).
All my components have a Dock=Fill and Autosize=True properties.
Currently, when I resize the windows, I don't have any scrollbar, the overflow is just no show.
I found that if I set the AutoScrollMinSize of my panel to something, I'm having those scrollbar appearing when I reach the set size.
My problem is that I add/remove elements on runtime, and I've also some things that I display or not depending the configuration. So for me it's very hard to hardcode here a value, either I've scrollbar too soon, or too late.
I'm sure there should be a way to configure my userControl, without having to calculate myself the size, to have the component displaying scrollbar, when the children's content cannot be displayed, do you know how?
Thank you!
You can change the AutoScrollMinSize value on the panel resize event or the form's resize event. That way, it won't be a fixed value and there will be a scrollbar available if the panel's child controls go beyond the panel edges -
private void panel1_Resize(object sender, EventArgs e)
{
panel1.AutoScrollMinSize = new System.Drawing.Size(panel1.Width, panel1.Height);
}

Categories

Resources