How do I set the margin for my panel? - c#

I'm creating a windows form that will programatically add a panel to another panel. I'm trying to customize the various properties of the new panel, such as color, and size, which works. However when I try to modify the margin, nothing works. I cannot find the Thickness struct either, since it's not a WPF app, it's a Windows Forms Application. Here is my code:
private void buttonAddExercise_Click(object sender, EventArgs e)
{
Panel panel = new Panel();
panel.BackColor = Color.White;
panel.Size = new Size(200, 300);
panel.Margin = new Padding(20);
listOfExercisePanels.Add(panel);
panelNewWorkout.Controls.Add(panel);
}

Firstly
To use Thickness you need to create/change your project .NET framework platform
version to .NET Framework 4.5. because this method available only in version 4.5
Secondly
You must add DockStyle to Fill for Child components as:
Panel panel = new Panel();
panel.BackColor = Color.White;
panel.Size = new Size(200, 200);
Label lb = new Label() { Text = "Hello" };
panel.Padding = new Padding(10);
lb.Dock = System.Windows.Forms.DockStyle.Fill;
panel.Controls.Add(lb);
this.Controls.Add(panel);
Thirdly
If you want to change Margin of the panel, you can put it into another parent panel

Add a reference to "PresentationFramework.dll",
and a using statement for:
using System.Windows;
PresentationFramework is not included as a reference by default.

Related

Label not visible above ToolStrip

At runtime I add (and remove) several controls, as needed, to a main window which in Designer contains only a ToolStrip with some function buttons. In some cases I want to add an info label next to the toolStrip, but I cannot make it visible, ie. it is hidden below. The code for the label is straightforward
infoLabel = new Label();
infoLabel.AutoSize = true;
infoLabel.Location = new System.Drawing.Point(200, 10);
infoLabel.Size = new System.Drawing.Size(35, 13);
infoLabel.BackColor = System.Drawing.SystemColors.Control;
infoLabel.Font = new System.Drawing.Font("Arial", 13);
infoLabel.ForeColor = System.Drawing.Color.Black;
infoLabel.TabIndex = 1;
infoLabel.Text = "this is info";
infoLabel.BringToFront();
this.Controls.Add(infoLabel);
TabIndex and BringToFront I added as an act of desperation, it does not help. BTW the ToolStrip's TabIndex is 2, and its BackColor I changed to transparent.
However, when I placed a label over the ToolStrip in the Designer, it is visible (ie. on top). I analysed the code then but did not see anything different from what I am writing. What am I missing here?
I suggest calling infoLabel.BringToFront(); at the very end, at least after this.Controls.Add(infoLabel); you current code amended:
infoLabel = new Label();
...
infoLabel.Text = "this is info";
// First Add to this
this.Controls.Add(infoLabel);
// Only then we can make infoLabel be the topmost
// among all existing controls which are on this
infoLabel.BringToFront();
We create infoLabel, add it to this and finally make it topmost on this. To make code more readable I suggest something like this:
// Create a label on this
infoLabel = new Label() {
AutoSize = true,
Location = new System.Drawing.Point(200, 10),
Size = new System.Drawing.Size(35, 13),
BackColor = System.Drawing.SystemColors.Control,
Font = new System.Drawing.Font("Arial", 13),
ForeColor = System.Drawing.Color.Black,
TabIndex = 1,
Text = "this is info",
Parent = this // <- instead of this.Controls.Add(infoLabel);
};
// make infoLabel topmost among all controls on this
infoLabel.BringToFront();
Windows Forms controls do not have a property which you can use to set z-index of controls like one can do in CSS.
You'll need to call Parent.SetChildIndex(control, 0);. The control at the front of Controls collection is the topmost in z-order for a container control.

How to change BackColor to "none" [duplicate]

In my C# Form I have a Label that displays a download percentage in the download event:
this.lblprg.Text = overallpercent.ToString("#0") + "%";
The Label control's BackColor property is set to be transparent and I want it to be displayed over a PictureBox. But that doesn't appear to work correctly, I see a gray background, it doesn't look transparent on top of the picture box. How can I fix this?
The Label control supports transparency well. It is just that the designer won't let you place the label correctly. The PictureBox control is not a container control so the Form becomes the parent of the label. So you see the form's background.
It is easy to fix by adding a bit of code to the form constructor. You'll need to change the label's Parent property and recalculate it's Location since it is now relative to the picture box instead of the form. Like this:
public Form1() {
InitializeComponent();
var pos = label1.Parent.PointToScreen(label1.Location);
pos = pictureBox1.PointToClient(pos);
label1.Parent = pictureBox1;
label1.Location = pos;
label1.BackColor = Color.Transparent;
}
Looks like this at runtime:
Another approach is to solve the design-time problem. That just takes an attribute. Add a reference to System.Design and add a class to your project, paste this code:
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design; // Add reference to System.Design
[Designer(typeof(ParentControlDesigner))]
class PictureContainer : PictureBox {}
You can just use
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239
You can draw text using TextRenderer which will draw it without background:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics,
overallpercent.ToString("#0") + "%",
this.Font,
new Point(10, 10),
Color.Red);
}
When overallpercent value changes, refresh pictureBox:
pictureBox1.Refresh();
You can also use Graphics.DrawString but TextRenderer.DrawText (using GDI) is faster than DrawString (GDI+)
Also look at another answer here and DrawText reference here
For easy for your design.
You can place your label inside a panel. and set background image of panel is what every image you want. set label background is transparent
After trying most of the provided solutions without success, the following worked for me:
label1.FlatStyle = FlatStyle.Standard
label1.Parent = pictureBox1
label1.BackColor = Color.Transparent
You most likely not putting the code in the load function. the objects aren't drawn yet if you put in the form initialize section hence nothing happens.
Once the objects are drawn then the load function runs and that will make the form transparents.
private void ScreenSaverForm_Load(object sender, EventArgs e)
{
label2.FlatStyle = FlatStyle.Standard;
label2.Parent = pictureBox1;
label2.BackColor = Color.Transparent;
}
One way which works for everything, but you need to handle the position, on resize, on move etc.. is using a transparent form:
Form form = new Form();
form.FormBorderStyle = FormBorderStyle.None;
form.BackColor = Color.Black;
form.TransparencyKey = Color.Black;
form.Owner = this;
form.Controls.Add(new Label() { Text = "Hello", Left = 0, Top = 0, Font = new Font(FontFamily.GenericSerif, 20), ForeColor = Color.White });
form.Show();
Using Visual Studio with Windows Form you may apply transparency to labels or other elements by adding using System.Drawing; into Form1.Designer.cs This way you will have Transparency available from the Properties panel ( in Appearance at BackColor ). Or just edit code in Designer.cs this.label1.BackColor = System.Drawing.Color.Transparent;

Why are Anchor properties not being honored within dynamically created nested components?

Let me begin by saying that I have not done a lot of Windows Forms development -- if there is an obvious mistake that I may be making, please don't hesitate to mention it.
Steps to reproduce my issue:
Create a new C# Windows Forms Project using VS 2010 or VS 2012
Using the VS Form Designer, add three FlowLayoutPanel components to the form
Set each FlowLayoutPanel to have the same height as the form and approximately 1/3 the width of the form
Position each FlowLayoutPanel so that they do not overlap each other horizontally and collectively consume approximately the entire area of the Form.
The leftmost FlowLayoutPanel is configured to have an Anchor of Top, Bottom, Left
The middle FlowLayoutPanel is configured to have an Anchor of Top, Bottom
The rightmost FlowLayoutPanel is configured to have an Anchor of Top, Bottom, Right
Add an event for Form_Shown:
private void Form1_Shown(object sender, EventArgs e)
{
Panel p = new Panel();
p.BorderStyle = BorderStyle.FixedSingle;
p.Width = 200;
p.Height = 100;
Label label1 = new Label();
label1.BorderStyle = BorderStyle.FixedSingle;
label1.Text = "Hello";
label1.Anchor = AnchorStyles.Top;
Label label2 = new Label();
label2.BorderStyle = BorderStyle.FixedSingle;
label2.Text = "World!";
label2.Anchor = AnchorStyles.Bottom;
p.Controls.Add(label1);
p.Controls.Add(label2);
middleFlow.Controls.Add(p); // add to the center most FlowLayoutPanel on Form1
}
The result seems to be that label1 is placed on top of label2, despite label2 being added second. Moreover, the anchor values seem to be ignored (as label1 is covering label2 when I intend for them to be anchored to the top and bottom of the Panel component, respectively)
If I use the Dock property instead of the Anchor property, the behavior is as desired. Why does the Anchor property not work in this situation?
Also, is there a way to anchor components to other components? I notice as I increase the size of my Form at runtime, horizontal "gaps" between panels appear. Ideally, I would like the panels to grow together, preventing any gaps/whitespace between them horizontally?
Thanks in advance for any suggestions or tips.
I'm still starting to learn c# and winforms, so the following may not be optimal but it does what you required.
Handled the labels with Dock=Top. Note that the labels are switched so that label1 is on top of label2, i.e., registering label1 last pushes down the already registered label2.
The positioning of the three panels is done without anchors and docks with an event handler for resize. Setting the size of the form after that raises a resize event. Colored to see the components.
using System;
using System.Drawing;
using System.Windows.Forms;
public class ThreePanel : Form {
FlowLayoutPanel leftFlow;
FlowLayoutPanel middleFlow;
FlowLayoutPanel rightFlow;
public ThreePanel(){
leftFlow = new FlowLayoutPanel() {
BackColor = Color.Yellow
};
middleFlow = new FlowLayoutPanel() {
BackColor = Color.LightGreen
};
rightFlow = new FlowLayoutPanel() {
BackColor = Color.LightBlue
};
this.Controls.Add(rightFlow);
this.Controls.Add(middleFlow);
this.Controls.Add(leftFlow);
this.Load += (s,e)=>Form1_Shown(s,e);
this.Resize += (s,e)=>{
int w=this.Width/3;
leftFlow.Width=middleFlow.Width
=rightFlow.Width=w;
leftFlow.Height=middleFlow.Height
=rightFlow.Height=this.Height;
leftFlow.Location=new Point(0,0);
middleFlow.Location=new Point(w,0);
rightFlow.Location=new Point(2*w,0);
};
this.Size = new Size(750,450);
}
private void Form1_Shown(object sender, EventArgs e)
{
Panel p = new Panel() {
BorderStyle = BorderStyle.FixedSingle,
Width = 200,
Height = 100,
BackColor = Color.Fuchsia,
};
Label label1 = new Label() {
BorderStyle = BorderStyle.FixedSingle,
Text = "Hello",
Dock = DockStyle.Top
};
Label label2 = new Label() {
BorderStyle = BorderStyle.FixedSingle,
Text = "World!",
Dock = DockStyle.Top
};
p.Controls.Add(label2);
p.Controls.Add(label1);
// add to the center most FlowLayoutPanel on Form1
middleFlow.Controls.Add(p);
}
public static void Main()
{
Application.Run(new ThreePanel());
}
}
I would expect exactly the behaviour that you mentioned.
The Anchor property only tells the parent container that the label should be sticked
to the parent. In your case AnchorStyles.Top means stick the label to the top and leave it there if the parent moves or resizes.
You did not specify dimensions or positions for the labels, so both overlapp.
The z-order of the controls is created implicitly from the order when added to middleFlow.Controls. You can check this using VS forms designer. Select "Bring to Front" or "Send to Back" and watch how the x.designer.cs changes.
Why it is in reverse order is one of the little .net secrets. The workaround is to change the order. Sometimes it is easier to do it manually than in the designer.

c# winforms dock fill tree-view make it disappear

I have a winforms application. In my application I have a user control which I loaded programmatically.
Inside this user-control I have tree view that also will be loaded with items programmatically. My problem is that I want to make my tree-view take the whole size of its parent.
What I have tried
I set the user-control Dock property to DockStyle.Fill to make it take the size of its parent.
I have done the same for the tree-view Dock property; set it to DockStyle.Fill.
What I get
The user-control takes the full size as expected but the tree-view looks like it is hidden. I checked the height, and I noticed it's 0. When I tried to change the height while it has DockStyle.Fill I can't, it changes back to 0.
Any ideas?
Update
The auto generated code for the tree-view:
private void InitializeComponent()
{
this.btnAddServer = new System.Windows.Forms.Button();
this.pnlServersContainer = new System.Windows.Forms.FlowLayoutPanel();
this.treeViewServers = new System.Windows.Forms.TreeView();
this.pnlServersContainer.SuspendLayout();
this.SuspendLayout();
//
// btnAddServer
//
this.btnAddServer.Location = new System.Drawing.Point(89, 478);
this.btnAddServer.Name = "btnAddServer";
this.btnAddServer.Size = new System.Drawing.Size(107, 23);
this.btnAddServer.TabIndex = 3;
this.btnAddServer.Text = "Add New Server";
this.btnAddServer.UseVisualStyleBackColor = true;
this.btnAddServer.Click += new System.EventHandler(this.btnAddServer_Click);
//
// pnlServersContainer
//
this.pnlServersContainer.AutoScroll = true;
this.pnlServersContainer.Controls.Add(this.treeViewServers);
this.pnlServersContainer.Dock = System.Windows.Forms.DockStyle.Fill;
this.pnlServersContainer.Location = new System.Drawing.Point(0, 0);
this.pnlServersContainer.Name = "pnlServersContainer";
this.pnlServersContainer.Padding = new System.Windows.Forms.Padding(8, 20, 0, 0);
this.pnlServersContainer.Size = new System.Drawing.Size(318, 463);
this.pnlServersContainer.TabIndex = 2;
//
// treeViewServers
//
this.treeViewServers.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeViewServers.Location = new System.Drawing.Point(11, 23);
this.treeViewServers.Name = "treeViewServers";
this.treeViewServers.Size = new System.Drawing.Size(275, 0);
this.treeViewServers.TabIndex = 0;
this.treeViewServers.DoubleClick += new System.EventHandler(this.treeViewServers_DoubleClick);
//
// ucServersList
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.btnAddServer);
this.Controls.Add(this.pnlServersContainer);
this.Name = "ucServersList";
this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 60);
this.Size = new System.Drawing.Size(318, 523);
this.Load += new System.EventHandler(this.ucServersList_Load);
this.pnlServersContainer.ResumeLayout(false);
this.ResumeLayout(false);
}
I recommend to open Document outline editor in Visual Studio.
This shows all controls in their hierarchical order as tree.
It lets you also drag & drop the controls to the right place.
Open it with View > Other windows > Document outline.
You may fix your problem when looking at the controls order.
I have figured it out. but still don't know why this happened!
my tree-view was inside FlowLayoutPanel not Panel. When i changed it to Panel everything goes fine. that's it!
The problem might be that you have added several items to the same parent control, and then when you fill the parent dock with one of them, the behaviour would not be what you expect.
Use a splitcontainer. And when you want to fill out the dock, make sure your control belongs to two differnt panels of a splitcontainer.
See this for an concrete example.
Is there any specific reason why you use FlowLayoutPanel?
It seems that the FlowLayoutPanel does not deal with any other than Dock.None.
I think you should use a simple Panel for this application, because it does not resize the contained controls - the Dock property behaves as expected.
Replacing the FlowLayoutPanel with a Panel will fix your problem.
This is a super old question... but since there are no accepted answers I’ll give it a go.
This happened to me when my Control was set to autosize. Either removing autosize or specifying a minimum height could solve this issue.

Transparent control over PictureBox

In my C# Form I have a Label that displays a download percentage in the download event:
this.lblprg.Text = overallpercent.ToString("#0") + "%";
The Label control's BackColor property is set to be transparent and I want it to be displayed over a PictureBox. But that doesn't appear to work correctly, I see a gray background, it doesn't look transparent on top of the picture box. How can I fix this?
The Label control supports transparency well. It is just that the designer won't let you place the label correctly. The PictureBox control is not a container control so the Form becomes the parent of the label. So you see the form's background.
It is easy to fix by adding a bit of code to the form constructor. You'll need to change the label's Parent property and recalculate it's Location since it is now relative to the picture box instead of the form. Like this:
public Form1() {
InitializeComponent();
var pos = label1.Parent.PointToScreen(label1.Location);
pos = pictureBox1.PointToClient(pos);
label1.Parent = pictureBox1;
label1.Location = pos;
label1.BackColor = Color.Transparent;
}
Looks like this at runtime:
Another approach is to solve the design-time problem. That just takes an attribute. Add a reference to System.Design and add a class to your project, paste this code:
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design; // Add reference to System.Design
[Designer(typeof(ParentControlDesigner))]
class PictureContainer : PictureBox {}
You can just use
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239
You can draw text using TextRenderer which will draw it without background:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics,
overallpercent.ToString("#0") + "%",
this.Font,
new Point(10, 10),
Color.Red);
}
When overallpercent value changes, refresh pictureBox:
pictureBox1.Refresh();
You can also use Graphics.DrawString but TextRenderer.DrawText (using GDI) is faster than DrawString (GDI+)
Also look at another answer here and DrawText reference here
For easy for your design.
You can place your label inside a panel. and set background image of panel is what every image you want. set label background is transparent
After trying most of the provided solutions without success, the following worked for me:
label1.FlatStyle = FlatStyle.Standard
label1.Parent = pictureBox1
label1.BackColor = Color.Transparent
You most likely not putting the code in the load function. the objects aren't drawn yet if you put in the form initialize section hence nothing happens.
Once the objects are drawn then the load function runs and that will make the form transparents.
private void ScreenSaverForm_Load(object sender, EventArgs e)
{
label2.FlatStyle = FlatStyle.Standard;
label2.Parent = pictureBox1;
label2.BackColor = Color.Transparent;
}
One way which works for everything, but you need to handle the position, on resize, on move etc.. is using a transparent form:
Form form = new Form();
form.FormBorderStyle = FormBorderStyle.None;
form.BackColor = Color.Black;
form.TransparencyKey = Color.Black;
form.Owner = this;
form.Controls.Add(new Label() { Text = "Hello", Left = 0, Top = 0, Font = new Font(FontFamily.GenericSerif, 20), ForeColor = Color.White });
form.Show();
Using Visual Studio with Windows Form you may apply transparency to labels or other elements by adding using System.Drawing; into Form1.Designer.cs This way you will have Transparency available from the Properties panel ( in Appearance at BackColor ). Or just edit code in Designer.cs this.label1.BackColor = System.Drawing.Color.Transparent;

Categories