I am new to WinForms, and I am very used to styling in CSS, so maybe I am not looking at WinForm's Bottom Margin property correctly, but, no matter what element I set an arbitrarily large bottom-margin number to, it seems to have no effect, at all.
What I want is to extend the design of the form to below the initial viewable window (vertical-scroll bar shows up just fine), and set a bottom-margin to these elements so that the very bottom of the element isn't flush with the very bottom of the window (a little space would be nice).
I have tried this on several elements just to see if it was only the one element (or the fact that it was out of the initial visible part of the window) that was giving me problems, but I can't seem to get any effect out of the margin property at all.
Looking here: http://msdn.microsoft.com/en-us/library/ms229627.aspx It seems that this is, indeed, what the margin property should do. Also, I can't find any padding within the GUI controls for any element.
As it stands, I am mostly only coding C# for the event handlers, until I get a better grasp of where Visual Studio puts everything within the two partial classes (and the other .cs files).
If it helps, here is the code for the designer file:
namespace WindowsFormsApplication1
{
partial class IntroForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.WelcomeHeader = new System.Windows.Forms.Label();
this.ActionSelect = new System.Windows.Forms.ComboBox();
this.ProceedBtn = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// WelcomeHeader
//
this.WelcomeHeader.AutoSize = true;
this.WelcomeHeader.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.WelcomeHeader.Location = new System.Drawing.Point(84, 30);
this.WelcomeHeader.Name = "WelcomeHeader";
this.WelcomeHeader.Size = new System.Drawing.Size(367, 25);
this.WelcomeHeader.TabIndex = 0;
this.WelcomeHeader.Text = "Please Select Which Content You";
//
// ActionSelect
//
this.ActionSelect.ForeColor = System.Drawing.SystemColors.WindowFrame;
this.ActionSelect.FormattingEnabled = true;
this.ActionSelect.Items.AddRange(new object[] {
"Events",
"Headline News",
"Images For Slideshow",
"Agendas",
"Job Opportunities",
"Schedule Of Meetings",
"Legal Notices",
"Main Street (Main Link)",
"Tourism (Main Link)",
"Rental Properties",
"Concert In The Park",
"Main Street News Letters"});
this.ActionSelect.Location = new System.Drawing.Point(126, 116);
this.ActionSelect.Name = "ActionSelect";
this.ActionSelect.Size = new System.Drawing.Size(283, 28);
this.ActionSelect.TabIndex = 1;
this.ActionSelect.Text = "Please Select";
//
// ProceedBtn
//
this.ProceedBtn.BackColor = System.Drawing.SystemColors.ButtonFace;
this.ProceedBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.ProceedBtn.ForeColor = System.Drawing.Color.DimGray;
this.ProceedBtn.Location = new System.Drawing.Point(221, 192);
this.ProceedBtn.Name = "ProceedBtn";
this.ProceedBtn.Size = new System.Drawing.Size(93, 34);
this.ProceedBtn.TabIndex = 2;
this.ProceedBtn.Text = "Proceed";
this.ProceedBtn.UseVisualStyleBackColor = false;
this.ProceedBtn.Click += new System.EventHandler(this.ProceedBtn_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(142, 55);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(251, 25);
this.label1.TabIndex = 3;
this.label1.Text = "Would Like To Change";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(147, 268);
this.richTextBox1.Margin = new System.Windows.Forms.Padding(3, 3, 3, 30);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(246, 269);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "";
//
// IntroForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(102)))), ((int)(((byte)(0)))));
this.ClientSize = new System.Drawing.Size(535, 306);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.ProceedBtn);
this.Controls.Add(this.ActionSelect);
this.Controls.Add(this.WelcomeHeader);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(204)))), ((int)(((byte)(153)))), ((int)(((byte)(0)))));
this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.Name = "IntroForm";
this.Text = "Okmulgee Online Web File Generator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label WelcomeHeader;
private System.Windows.Forms.ComboBox ActionSelect;
private System.Windows.Forms.Button ProceedBtn;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.RichTextBox richTextBox1;
}
}
------------------------------------------UPDATE-------------------------------------------
The element I have set the bottom margin property to within the code above is simply, richTextBox1.
Also, I did find a Padding element to the main form element, but sadly, this doesn't push other elements away from its edges either :(
What do these properties do (margin, padding)?
The Margin property is used by the automatic layout feature built into Winforms. But it does require that you allow the container to grow so it can provide the requested margin. So you must set the form's AutoSize property to True.
Combining AutoSize and AutoScroll is possible, you can set the MaximumSize property to prevent it from growing too much. The scrollbar automatically appears when the layout calculation produces a layout that exceeds the MaximumSize. The default maximum size is the Screen.WorkingArea on which the form is displayed, usually good enough.
Related
I would like to create a button with a plus sign, however when I created the plus sign with the text, it is not aligned from the top of the button as in the image below:
The problem occurs when I try to increase the font size. In default size of the text the plus sign is centered. What can I do to center it in my case?
TextAlign (TopLeft) and UseCompatibleTextRendering didn't work. (Winforms C# Visual Studio 2019)
Note: Text align property did not work because my font size is larger than default it is 36+.
You can use an png of + from resource instead of setting directly the text property of the button.
Example
this.button1.Image = NameSpace1.Properties.Resources.Image2.png;
One way is to set the Button's FlatStyle (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.buttonbase.flatstyle?view=net-5.0) to System (System.Windows.Forms.FlatStyle.System), if you are Ok to change the FlatStyle.
I created a test application and the combination of font "Microsoft Sans Serif" in size 36pt with button size 32x32px and FlatStyle = FlatStyle.System (as elimad already mentioned) works quite well for me.
namespace ControlTest
{
public class MainForm : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
private Button button1;
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(32, 32);
this.button1.TabIndex = 0;
this.button1.Text = "+";
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(157, 92);
this.Controls.Add(this.button1);
this.Name = "MainForm";
this.Text = "ControlTest";
this.ResumeLayout(false);
}
}
}
You can create custom controls and then render the text yourself
https://www.c-sharpcorner.com/UploadFile/f5a10c/creating-custom-controls-in-C-Sharp/
After compiling it should just show up inside the toolbox
I have a user control that I need to manually resize during runtime, although I would like the margin to be applied. I found, from link 1 below, that for the margin to work the AutoSize property needs to be set to true. Although, since I need to manually resize the control during runtime, the AutoSize property needs to be false to my understanding.
The user control (Accordion) contains a horizontal split panel where the first panel contains a button and the second panel contains a Panel control. In the image below you can see multiple "Accordions" which, in the picture, is one of the buttons. The Accordions are docked at the top of the container. The intent is when I click on the button it resizes and shows the "contents panel" and the contents panel's control. My problem is that setting the User Control's margin property does not cause them to automatically distance themselves from the other controls nor the container they are in.
Link 1: WinForm Bottom Margin Property Doesn't Do Anything
Hopefully some useful chunks of what I'm working with.
namespace CustomControls
{
partial class Accordion
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.split = new System.Windows.Forms.SplitContainer();
this.ToggleCollapse = new System.Windows.Forms.PictureBox();
this.MainButton = new System.Windows.Forms.Button();
this.contentsPanel = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.split)).BeginInit();
this.split.Panel1.SuspendLayout();
this.split.Panel2.SuspendLayout();
this.split.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.ToggleCollapse)).BeginInit();
this.SuspendLayout();
//
// split
//
this.split.Dock = System.Windows.Forms.DockStyle.Fill;
this.split.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.split.IsSplitterFixed = true;
this.split.Location = new System.Drawing.Point(0, 0);
this.split.Margin = new System.Windows.Forms.Padding(3, 3, 3, 10);
this.split.Name = "split";
this.split.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// split.Panel1
//
this.split.Panel1.Controls.Add(this.ToggleCollapse);
this.split.Panel1.Controls.Add(this.MainButton);
this.split.Panel1MinSize = 0;
//
// split.Panel2
//
this.split.Panel2.Controls.Add(this.contentsPanel);
this.split.Size = new System.Drawing.Size(663, 488);
this.split.SplitterDistance = 40;
this.split.TabIndex = 0;
//
// ToggleCollapse
//
this.ToggleCollapse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ToggleCollapse.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.ToggleCollapse.Enabled = false;
this.ToggleCollapse.Image = global::CustomControls.Properties.Resources.TrianglePointer;
this.ToggleCollapse.Location = new System.Drawing.Point(621, 13);
this.ToggleCollapse.Name = "ToggleCollapse";
this.ToggleCollapse.Size = new System.Drawing.Size(39, 24);
this.ToggleCollapse.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.ToggleCollapse.TabIndex = 0;
this.ToggleCollapse.TabStop = false;
//
// MainButton
//
this.MainButton.AutoEllipsis = true;
this.MainButton.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.MainButton.Dock = System.Windows.Forms.DockStyle.Fill;
this.MainButton.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.MainButton.FlatAppearance.BorderSize = 3;
this.MainButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.MainButton.Font = new System.Drawing.Font("Verdana", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.MainButton.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.MainButton.Location = new System.Drawing.Point(0, 0);
this.MainButton.Name = "MainButton";
this.MainButton.Size = new System.Drawing.Size(663, 40);
this.MainButton.TabIndex = 0;
this.MainButton.Text = "Accordion Activator";
this.MainButton.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
this.MainButton.UseVisualStyleBackColor = false;
this.MainButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainButton_MouseDown);
this.MainButton.MouseEnter += new System.EventHandler(this.MainButton_MouseEnter);
this.MainButton.MouseLeave += new System.EventHandler(this.MainButton_MouseLeave);
this.MainButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainButton_MouseUp);
//
// contentsPanel
//
this.contentsPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.contentsPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.contentsPanel.Location = new System.Drawing.Point(0, 0);
this.contentsPanel.Name = "contentsPanel";
this.contentsPanel.Size = new System.Drawing.Size(663, 444);
this.contentsPanel.TabIndex = 0;
this.contentsPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseDown);
this.contentsPanel.MouseLeave += new System.EventHandler(this.Panel_MouseLeave);
this.contentsPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseMove);
this.contentsPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseUp);
//
// Accordion
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.split);
this.DoubleBuffered = true;
this.Margin = new System.Windows.Forms.Padding(3, 3, 3, 10);
this.Name = "Accordion";
this.Size = new System.Drawing.Size(663, 488);
this.split.Panel1.ResumeLayout(false);
this.split.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.split)).EndInit();
this.split.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.ToggleCollapse)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer split;
private System.Windows.Forms.Button MainButton;
private System.Windows.Forms.PictureBox ToggleCollapse;
protected System.Windows.Forms.Panel contentsPanel;
}
}
This is when and how I manually resize
public bool Collapsed
{
get
{
return split.Panel2Collapsed;
}
set
{
Freeze(true);
if (Dock == DockStyle.Fill && this.DesignMode)
{
if (firstResize)
{
if (value)
this.Parent.Height = MainButtonHeight;
else
this.Parent.Height = ExpandedHeight;
}
else
{
if (value)
CollapseAnimation(1);
else
ExpandAnimation(5);
}
}
else
{
if (firstResize)
{
if (value)
this.Height = MainButtonHeight;
else
this.Height = ExpandedHeight;
if (loadBegan)
firstResize = !firstResize;
}
else
{
if (value)
CollapseAnimation(1);
else
ExpandAnimation(5);
}
}
split.Panel2Collapsed = value;
ToggleCollapse.Image = toggleImageNeutral;
Freeze(false);
}
}
Not sure I completely understood your problem but maybe this can be of help. If you want a fixed margin to stay around your control after a resize (due to the SplitContainer panel extending/collapsing), you can try this:
on your blank user control drag a Panel control and set its Margin to (0,0,0,0) and Padding to (x,x,x,x), where x is the width of the margin you want.
Be careful when doing calculations on the height of the children controls to resize the control, always take the paddings and margins into account if you have them set on some children.
UPDATE:
As stated here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.margin?view=netframework-4.8
"Setting the Margin property on a docked control has no effect on the distance of the control from the edges of its container."
So, I think you have to choose a specific container like TableLayoutPanel or a FlowLayoutPanel. Try with a FlowLayoutPanel and set it like this:
AutoSize = true;
BorderStyle = FixedSingle (just to see if the margin is kept correctly)
FlowDirection = TopDown
Then drag inside of it some of your controls with the AutoSize property set to false and with the Margin set as you prefer. You should get something like this:
Note: the height of your controls may still be changed (maybe setting a minimum control height can be of help), but once you set their width, the FlowLayoutPanel control can't cut the control because of the margin.
I'm working in a c# application in winform.
I saw that there is no Style for element (unlike WPF). But is there a way to simply set all the labels to a specific design ?
Actually I do :
public partial class myControl : UserControl
{
private Color LabelColor = Color.Indigo;
private Color LabelFont = new System.Drawing.Font("Arial",
18F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
public myControl()
{
InitializeComponent();
//Set design
designLabels();
}
private void designLabels()
{
List<Label> labelsToStyle = new List<Label>();
labelsToStyle.Add(labelName);
labelsToStyle.Add(labelAge);
labelsToStyle.Add(labelSize);
foreach (Label l in labelsToStyle)
{
l.ForeColor = LabelColor;
l.Font = LabelFont;
l.Dock = DockStyle.Fill;
}
}
}
It works but it doesn't display correctly in designer (I have to run application to see my design). And maybe it exists a simplest way ?
As per my comment the easiest is to create a custom control and use that one in your windows.
here how simple it is. Simply override the label and set in constructor the default value you want
public class DesignLabel : Label
{
public DesignLabel()
{
ForeColor = Color.Indigo;
Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
then compile once and go to your design view, open your toolbox and you will see the "DesignLabel", simply drag drop on the window and that's it. If you change default in the class it will be changed all over the place.
If you want to change the style already at design time so that you can see in in the Form.cs[design] you need to edit the Form.Designer.cs file! But this you have to do label for label by hand. I saved in this example the Font and Color in the project properties (sorry for the german version):
in my example I have 3 Labels. In the Form.Designer.cs file you can add the properties:
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 15);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.Font = Properties.Settings.Default.LabelFont;
this.label1.ForeColor = Properties.Settings.Default.LabelColor;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 68);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 15);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
this.label2.Font = Properties.Settings.Default.LabelFont;
this.label2.ForeColor = Properties.Settings.Default.LabelColor;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 122);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 15);
this.label3.TabIndex = 2;
this.label3.Text = "label3";
this.label3.Font = Properties.Settings.Default.LabelFont;
this.label3.ForeColor = Properties.Settings.Default.LabelColor;
The result looks like this:
DISCLAIMER
I would not recommend this approach! Editing the Form.Desginer.cs file is never a good idea! I would stick to the run time changes. If you want to change all Labels just filter the this.Controls for it and foreach through the collection like this:
this.Controls.OfType<Label>().ToList().ForEach(lbl =>
{
lbl.Font = LabelFont;
lbl.ForeColor = LabelColor;
//lbl.Dock = DockStyle.Fill; // uncommented, because I could see only 1 Label
});
The result will be the same.
I am trying to have my custom control somehow auto-fit into the flow layout panel at horizontal edges.
What I am trying to accomplish is a vertical list of shown custom control. But what I get is below:
Code for my custom control is:
namespace CustomControl
{
partial class ChatMessage
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ChatMessage));
this.ContentsTable = new System.Windows.Forms.TableLayoutPanel();
this.DateTimeLabel = new System.Windows.Forms.Label();
this.NumberLabel = new System.Windows.Forms.Label();
this.NameLabel = new System.Windows.Forms.Label();
this.MessageLabel = new System.Windows.Forms.Label();
this.DirectionLabel = new System.Windows.Forms.Label();
this.ContentsTable.SuspendLayout();
this.SuspendLayout();
//
// ContentsTable
//
this.ContentsTable.AutoSize = true;
this.ContentsTable.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ContentsTable.BackColor = System.Drawing.Color.White;
this.ContentsTable.ColumnCount = 2;
this.ContentsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.ContentsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.ContentsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.ContentsTable.Controls.Add(this.DateTimeLabel, 1, 2);
this.ContentsTable.Controls.Add(this.NumberLabel, 1, 0);
this.ContentsTable.Controls.Add(this.NameLabel, 0, 0);
this.ContentsTable.Controls.Add(this.MessageLabel, 0, 1);
this.ContentsTable.Controls.Add(this.DirectionLabel, 0, 2);
this.ContentsTable.Dock = System.Windows.Forms.DockStyle.Fill;
this.ContentsTable.Location = new System.Drawing.Point(48, 0);
this.ContentsTable.Name = "ContentsTable";
this.ContentsTable.RowCount = 3;
this.ContentsTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.ContentsTable.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.ContentsTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.ContentsTable.Size = new System.Drawing.Size(592, 54);
this.ContentsTable.TabIndex = 0;
//
// DateTimeLabel
//
this.DateTimeLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.DateTimeLabel.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.DateTimeLabel.ForeColor = System.Drawing.SystemColors.ControlDark;
this.DateTimeLabel.Location = new System.Drawing.Point(299, 104);
this.DateTimeLabel.Name = "DateTimeLabel";
this.DateTimeLabel.Size = new System.Drawing.Size(290, 20);
this.DateTimeLabel.TabIndex = 0;
this.DateTimeLabel.Text = "Date / Time";
this.DateTimeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// NumberLabel
//
this.NumberLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.NumberLabel.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.NumberLabel.Location = new System.Drawing.Point(299, 0);
this.NumberLabel.Name = "NumberLabel";
this.NumberLabel.Size = new System.Drawing.Size(290, 20);
this.NumberLabel.TabIndex = 1;
this.NumberLabel.Text = "Number or ID";
this.NumberLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// NameLabel
//
this.NameLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.NameLabel.Font = new System.Drawing.Font("Calibri", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.NameLabel.Location = new System.Drawing.Point(3, 0);
this.NameLabel.Name = "NameLabel";
this.NameLabel.Size = new System.Drawing.Size(290, 20);
this.NameLabel.TabIndex = 2;
this.NameLabel.Text = "Name";
this.NameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// MessageLabel
//
this.MessageLabel.AutoSize = true;
this.ContentsTable.SetColumnSpan(this.MessageLabel, 2);
this.MessageLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.MessageLabel.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.MessageLabel.Location = new System.Drawing.Point(3, 20);
this.MessageLabel.Name = "MessageLabel";
this.MessageLabel.Size = new System.Drawing.Size(586, 84);
this.MessageLabel.TabIndex = 3;
this.MessageLabel.Text = resources.GetString("MessageLabel.Text");
//
// DirectionLabel
//
this.DirectionLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.DirectionLabel.ForeColor = System.Drawing.Color.LightGray;
this.DirectionLabel.Location = new System.Drawing.Point(3, 104);
this.DirectionLabel.Name = "DirectionLabel";
this.DirectionLabel.Size = new System.Drawing.Size(290, 20);
this.DirectionLabel.TabIndex = 4;
this.DirectionLabel.Text = "Direction";
this.DirectionLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// ChatMessage
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.Controls.Add(this.ContentsTable);
this.MinimumSize = new System.Drawing.Size(320, 0);
this.Name = "ChatMessage";
this.Padding = new System.Windows.Forms.Padding(48, 0, 0, 0);
this.Size = new System.Drawing.Size(640, 54);
this.ContentsTable.ResumeLayout(false);
this.ContentsTable.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TableLayoutPanel ContentsTable;
private System.Windows.Forms.Label DateTimeLabel;
private System.Windows.Forms.Label NumberLabel;
private System.Windows.Forms.Label NameLabel;
private System.Windows.Forms.Label MessageLabel;
private System.Windows.Forms.Label DirectionLabel;
}
}
You can see my custom control overflows form size.
Note: Flow layout panel used in the picture has a dock fill, flow direction = top down & wrap contents = false.
Can anyone please suggest?
Using the custom label class from this page for the message label inside panel as suggested by #Reza Aghaei did the job. See the result below, this is exactly what I wanted.
I have a system windows forms web browser on top of another control. The web browser has an image set.
On top of the web browser control is an inherited panel where I can control the opacity.
If I make this transparent, it doesn't show the web browser image, it shows the back colour of the control under the web browser.
Ie
Layer1 control. Back colour is blue
Layer2 web browser with image as HTML
Layer3 transparent panel
Layer3 when transparent shows layer1 not layer2
How can I make the web browser opaque so layer3 shows through to layer2 (webbrowser) ?
I have tried setting SetStyles to control opacity on the web browser.
Thanks
Short Answer:
You can't.
Real World Solution:
Have you looked into WPF?
Long Answer:
Opacity is a trick that WinForms does.
When a control is marked to be displayed transparent (or semi transparent), WinForms queries the parent object's visuals to ask "What would you have printed here had my control not existed?". From this result WinForms either displays the pixel of the control, the pixel of the parent, or a combination of the two (if semi transparent).
This becomes really apparent in a simple example:
Create a new winforms project
Create 2 labels, place them over top of each other (slighty offset)
Set both labels backgrounds to Color.Transparent
You'll see immediately that transparency takes a chunk out of the label underneath.
Example Code (All in one file, compile and run):
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication5
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
public class MainForm : Form
{
private Random random = new Random();
private Button btnRandomBackgroundColor;
private Label lblBackgroundLabel;
private Label lblTransparent;
public MainForm()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
BackColor = Color.FromArgb(random.Next(0, 255),
random.Next(0, 255),
random.Next(0, 255));
}
private void InitializeComponent()
{
this.btnRandomBackgroundColor = new System.Windows.Forms.Button();
this.lblBackgroundLabel = new System.Windows.Forms.Label();
this.lblTransparent = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnRandomBackgroundColor
//
this.btnRandomBackgroundColor.Location = new System.Drawing.Point(12, 12);
this.btnRandomBackgroundColor.Name = "btnRandomBackgroundColor";
this.btnRandomBackgroundColor.Size = new System.Drawing.Size(144, 23);
this.btnRandomBackgroundColor.TabIndex = 0;
this.btnRandomBackgroundColor.Text = "Randomize Background Color";
this.btnRandomBackgroundColor.UseVisualStyleBackColor = true;
this.btnRandomBackgroundColor.Click += button_Click;
//
// lblBackgroundLabel
//
this.lblBackgroundLabel.AutoSize = true;
this.lblBackgroundLabel.BackColor = System.Drawing.Color.Transparent;
this.lblBackgroundLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblBackgroundLabel.Location = new System.Drawing.Point(41, 49);
this.lblBackgroundLabel.Name = "lblBackgroundLabel";
this.lblBackgroundLabel.Size = new System.Drawing.Size(184, 33);
this.lblBackgroundLabel.TabIndex = 1;
this.lblBackgroundLabel.Text = "Simple Label";
//
// lblTransparent
//
this.lblTransparent.AutoSize = true;
this.lblTransparent.BackColor = System.Drawing.Color.Transparent;
this.lblTransparent.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblTransparent.Location = new System.Drawing.Point(61, 63);
this.lblTransparent.Name = "lblTransparent";
this.lblTransparent.Size = new System.Drawing.Size(251, 33);
this.lblTransparent.TabIndex = 2;
this.lblTransparent.Text = "Transparent Label";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(341, 114);
Controls.Add(this.lblTransparent);
this.Controls.Add(this.lblBackgroundLabel);
this.Controls.Add(this.btnRandomBackgroundColor);
this.Name = "Form1";
this.Text = "MainForm";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}