Custom Component on Windows Forms not loading correctly - c#

I am new to Windows Forms and am trying to create a custom Panel which has a header bar with two labels. I want to be able to set the text of those labels from the designer when this panel is added to another form.
Here is the code for my Panel:
public partial class TitledPanel : Panel
{
public string Title { get; set; } = "";
public string TitleNumber
{
get { return numberedLabel.Text; }
set { numberedLabel.Text = value; }
}
public TitledPanel()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new SolidBrush(BackColor))
e.Graphics.FillRectangle(brush, ClientRectangle);
e.Graphics.DrawRectangle(
new Pen(Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(229)))), ((int)(((byte)(229)))))), 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
private void InitializeComponent()
{
this.titleLabel = new System.Windows.Forms.Label();
this.mainHeaderPanel = new System.Windows.Forms.Panel();
this.numberedLabel = new System.Windows.Forms.Label();
this.mainHeaderPanel.SuspendLayout();
this.SuspendLayout();
//
// titleLabel
//
this.titleLabel.AutoSize = true;
this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 26F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
this.titleLabel.Location = new System.Drawing.Point(75, 27);
this.titleLabel.Margin = new System.Windows.Forms.Padding(32,32,32,32);
this.titleLabel.Name = "titleLabel";
this.titleLabel.Size = new System.Drawing.Size(193, 30);
this.titleLabel.TabIndex = 2;
this.titleLabel.Text = "";
//
// mainHeaderPanel
//
this.mainHeaderPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(229)))), ((int)(((byte)(229)))));
this.mainHeaderPanel.Controls.Add(this.numberedLabel);
this.mainHeaderPanel.Controls.Add(this.titleLabel);
this.mainHeaderPanel.Dock = System.Windows.Forms.DockStyle.Top;
this.mainHeaderPanel.ForeColor = System.Drawing.SystemColors.ControlText;
this.mainHeaderPanel.Location = new System.Drawing.Point(64, 140);
this.mainHeaderPanel.Margin = new System.Windows.Forms.Padding(64, 140, 64, 0);
this.mainHeaderPanel.Name = "mainHeaderPanel";
this.mainHeaderPanel.Size = new System.Drawing.Size(1126, 80);
this.mainHeaderPanel.TabIndex = 4;
//
// numberedLabel
//
this.numberedLabel.AutoSize = true;
this.numberedLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(4)))), ((int)(((byte)(103)))), ((int)(((byte)(198)))));
this.numberedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
this.numberedLabel.ForeColor = System.Drawing.Color.White;
this.numberedLabel.Location = new System.Drawing.Point(23, 26);
this.numberedLabel.Name = "numberedLabel";
this.numberedLabel.Size = new System.Drawing.Size(31, 32);
this.numberedLabel.TabIndex = 3;
this.numberedLabel.Text = "0";
var path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(-1, 0, numberedLabel.Width, numberedLabel.Height);
this.numberedLabel.Region = new Region(path);
//
// TitledPanel
//
this.AutoSize = true;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.mainHeaderPanel);
this.Name = "BeforeYouStartForm";
this.Size = new System.Drawing.Size(1254, 839);
this.Text = "GetStarted";
this.mainHeaderPanel.ResumeLayout(false);
this.mainHeaderPanel.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label titleLabel;
private System.Windows.Forms.Panel mainHeaderPanel;
private System.Windows.Forms.Label numberedLabel;
}
When I put this panel on a form and run the app, it looks like it should. However the fields Title and TitleNumber show up in the designer with the text Object reference not set to an instance of an object. next to them. Also the panel does not load in the designer, when I add it to a form, it looks like the default Panel would, and it only loads when I actually run the app.
How can I get the TitledPanel to load in the designer and how can I get Title and TitleNumber to show correctly in the designer.
Thanks

Related

Custom groupbox control not painting correctly when added to tabpage

I have a groupbox which I created a custom paint event for so that I
can control the border color.
It works great when the groupbox is on a form but when the groupbox is
on a tabpage, the control does not paint properly. Everything is blurry
and the child controls inside the groupbox are also blurry and get a border
drawn.
Any gentle help would be greatly appreciated!
Thanks,
Dan
using System.Drawing;
using System.Windows.Forms;
namespace CustomGroupBoxPaint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
///////////////////////////////////////////////////////////////////////
private void groupBox_Paint(object sender, PaintEventArgs e)
{
// Default border line color
Color lineColor = Color.LightGray;
// Get the control
GroupBox gb = (GroupBox)sender;
// Figure out which color to use
if (gb.Enabled == true)
{
// User black line
lineColor = Color.Black;
}
else
{
// Use dark gray color
lineColor = Color.DarkGray;
}
//get the text size in groupbox
System.Drawing.Size tSize = TextRenderer.MeasureText(e.Graphics, gb.Text, gb.Font);
// Draw groupbox border
Rectangle borderRect = e.ClipRectangle;
borderRect.Y = (borderRect.Y + (tSize.Height / 2));
borderRect.Height = (borderRect.Height - (tSize.Height / 2));
ControlPaint.DrawBorder(e.Graphics, borderRect, lineColor, ButtonBorderStyle.Solid);
// Draw groupbox text
Rectangle textRect = e.ClipRectangle;
textRect.X = (textRect.X + 6);
textRect.Width = tSize.Width + 5;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(gb.BackColor), textRect);
TextRenderer.DrawText(e.Graphics, gb.Text, gb.Font, textRect, gb.ForeColor);
}
}
}
namespace CustomGroupBoxPaint
{
partial class Form1
{
/// <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.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox1.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(40, 24);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(376, 352);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.groupBox2);
this.tabPage1.Controls.Add(this.groupBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(368, 326);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.checkBox2);
this.groupBox2.Enabled = false;
this.groupBox2.Location = new System.Drawing.Point(52, 168);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(272, 100);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "DisabledGBTabPage";
this.groupBox2.Paint += new System.Windows.Forms.PaintEventHandler(this.groupBox_Paint);
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(64, 42);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(80, 17);
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "checkBox2";
this.checkBox2.UseVisualStyleBackColor = true;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Location = new System.Drawing.Point(48, 32);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(272, 100);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "EnabledGBTabPage";
this.groupBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.groupBox_Paint);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(64, 48);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
//
// tabPage2
//
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(368, 326);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.checkBox3);
this.groupBox3.Location = new System.Drawing.Point(512, 78);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(200, 100);
this.groupBox3.TabIndex = 1;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "EnabledGBform";
this.groupBox3.Paint += new System.Windows.Forms.PaintEventHandler(this.groupBox_Paint);
//
// checkBox3
//
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new System.Drawing.Point(60, 42);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(80, 17);
this.checkBox3.TabIndex = 1;
this.checkBox3.Text = "checkBox3";
this.checkBox3.UseVisualStyleBackColor = true;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.checkBox4);
this.groupBox4.Enabled = false;
this.groupBox4.Location = new System.Drawing.Point(512, 238);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(200, 100);
this.groupBox4.TabIndex = 2;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "DisabledGBform";
this.groupBox4.Paint += new System.Windows.Forms.PaintEventHandler(this.groupBox_Paint);
//
// checkBox4
//
this.checkBox4.AutoSize = true;
this.checkBox4.Location = new System.Drawing.Point(60, 42);
this.checkBox4.Name = "checkBox4";
this.checkBox4.Size = new System.Drawing.Size(80, 17);
this.checkBox4.TabIndex = 1;
this.checkBox4.Text = "checkBox4";
this.checkBox4.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox4;
}
}
Screen shot showing problem

Winforms: ComboBox height doesn't resize when resolution is changed

I have a basic combobox in a form. Compared to other controls(Button,label, etc) the height of the combobox doesn't change when the resolution is changed.
public partial class Form1 : Form
{
string result;
string fontInformation;
private bool scaleFactorKnown = false;
private SizeF scaleFactor;
public Form1()
{
SizeChanged += Form1_SizeChanged;
InitializeComponent();
label1.Location = new Point(12, 36);
label1.Size = new Size(100, 21);
label1.Scale(scaleFactor);
//
// textBox1
//
textBox1.Location = new Point(133, 33);
textBox1.Size = new Size(100, 21);
textBox1.Scale(scaleFactor);
//
// comboBox1
//
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
// button1
//
button1.Location = new Point(365, 32);
button1.Size = new Size(100, 21);
button1.Scale(scaleFactor);
//
// radioButton1
//
radioButton1.Location = new Point(480, 32);
radioButton1.Size = new Size(100, 21);
radioButton1.Scale(scaleFactor);
//
// checkBox1
//
checkBox1.Location = new Point(586, 33);
checkBox1.Size = new Size(100, 21);
checkBox1.Scale(scaleFactor);
//
// textBox2
//
textBox2.Location = new Point(26, 102);
textBox2.Size = new Size(660, 250);
textBox2.Scale(scaleFactor);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (!scaleFactorKnown)
{
scaleFactor = AutoScaleFactor;
scaleFactorKnown = true;
}
Size controlSize = new Size((int)(comboBox1.Width * scaleFactor.Width),
(int)(comboBox1.Height * scaleFactor.Height)); //use for sizing
//set bounds
comboBox1.Bounds = new Rectangle(comboBox1.Location, controlSize);
}
}
I have tried the method Scale() to scale all other controls, it works for other controls except for combobox. I also tried manually changing the bound but it didn't work. I also tried change the anchor and dock as well.
Expected result: Combobox height(At 150%)=42
Actual result: Combobox
height(At 150%)=28
Would appreciate any help on how to fix this issue.
You have to set the IntegralHeight property of the ComboBox to false:
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
comboBox1.IntegralHeight = false;

Fit-width custom control in FlowLayoutPanel

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.

Dynamically added winforms control Not displaying?

I have this custom control which is basically a panel:
class ResultPanel : Panel {
Label scoreValueLabel = new Label();
public ResultPanel() : base(){
scoreValueLabel.AutoSize = true;
scoreValueLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
scoreValueLabel.Location = new System.Drawing.Point(265, 99);
scoreValueLabel.Name = "scoreValueLabel";
scoreValueLabel.Size = new System.Drawing.Size(49, 25);
scoreValueLabel.TabIndex = 10;
scoreValueLabel.Text = "+10";
Controls.Add(scoreValueLabel);
}
}
And I'm trying to add it to a panel in an event handler:
private void ResultsReceivedHandler(object sender, List<QuestionResult> results) {
ResultPanel resultPanel = new ResultPanel();
allResultsPanel.Controls.Add(new ResultPanel());
resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
resultPanel.BorderStyle = BorderStyle.FixedSingle;
resultPanel.Location = new Point(0, 155);
resultPanel.Name = "questionResultPanel";
resultPanel.Size = new Size(325, 148);
resultPanel.TabIndex = 0;
}
I know that an instance of ResultPanel can be displayed in allResultsPanel because I have added(using designer view) a ResultPanel to allResultsPanel that has the same size as this one at the top of allResultsPanel and that displays.
allResultsPanel is just a normal Panel btw, and its big enough to fit the control because its height is 800.
So why can i see the control added through the design view but not one added dynamically?
While setting up resultPanel:
ResultPanel resultPanel = new ResultPanel();
resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
resultPanel.BorderStyle = BorderStyle.FixedSingle;
resultPanel.Location = new Point(0, 155);
resultPanel.Name = "questionResultPanel";
resultPanel.Size = new Size(325, 148);
resultPanel.TabIndex = 0;
You are adding another new panel to the allResultsPanel
allResultsPanel.Controls.Add(new ResultPanel());

keep button position on panel resize

I've been messing around with something that should be simple.
I've moved jobs and trying to get some of the basic tools I have used before, but of course I don't have the old source to look at.
We had extended panel to have some standard properties and functions (save, close, save and close).
But I can't get the buttons to be positioned correctly on a resize. I put this ExtPanel on a form but the buttons keep disappearing as I resize, or don't move as expected (frozen on bottom right).
The class
public partial class ExtPanel: UserControl
{
private System.Windows.Forms.Button btnSaveandClose;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnSave;
public ExtPanel ()
{
InitializeComponent ();
}
// misc things this class does...
}
public partial class ExtPanel
{
private void InitializeComponent ()
{
this.btnSaveandClose = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// btnSaveandClose
//
this.btnSaveandClose.Location = new System.Drawing.Point(899, 689);
this.btnSaveandClose.Name = "btnSaveandClose";
this.btnSaveandClose.Size = new System.Drawing.Size(100, 30);
this.btnSaveandClose.TabIndex = 0;
this.btnSaveandClose.Text = "Save and Close";
this.btnSaveandClose.UseVisualStyleBackColor = true;
this.btnSaveandClose.Click += new System.EventHandler(this.Click_SaveandClose);
this.btnSaveandClose.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(687, 689);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(100, 30);
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.Click_Close);
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(793, 689);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(100, 30);
this.btnSave.TabIndex = 2;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.Click_Save);
this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.btnSave);
this.panel1.Controls.Add(this.btnCancel);
this.panel1.Controls.Add(this.btnSaveandClose);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(1008, 730);
this.panel1.TabIndex = 0;
//
// ExtPanel
//
this.Controls.Add(this.panel1);
this.Name = "ExtPanel";
this.Size = this.panel1.Size;
this.Click += new System.EventHandler(this.click_this);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
}
Have you tried anchoring the button?
...I missed the anchoring code.
If you have a button in the lower right-hand corner that you want to stay in the lower right-hand corner when resizing the form, set it's anchor properties to Bottom, Right.
Update:
I loaded your code. You have a panel inside of ExtPanel. If you dock (Fill) that panel then you should be working fine by resizing ExtPanel.

Categories