15/5/2020 c# - Create a custom control which has a fixed height in designer - Stack Overflow
Create a custom control which has a fixed height in designer
Asked 10 years, 1 month ago Active 3 years, 10 months ago Viewed 8k times
I want to create a custom control (derived from Control class), and when I drag this custom
control to a form in designer, I can only change its width. This feature is same as single-line
5 textbox.
Update: My application is Windows Form.
c# .net winforms designer
edited Jul 8 '13 at 20:22 asked Apr 6 '10 at 5:00
nickb Leo Vo
54.8k 10 85 124 8,064 9 51 76
3 Answers Active Oldest Votes
See http://www.windowsdevelop.com/windows-forms-general/how-to-set-that-a-control-
resizes-in-width-only-9207.shtml.
10 You override SetBoundsCore and define a Designer to remove top and bottom resize handles.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace MyControlProject
{
[Designer(typeof(MyControlDesigner))]
public class MyControl : Control
{
protected override void SetBoundsCore(int x, int y, int width, int height,
BoundsSpecified specified)
{
height = 50;
base.SetBoundsCore(x, y, width, height, specified);
}
}
internal class MyControlDesigner : ControlDesigner
{
MyControlDesigner()
{
base.AutoResizeHandles = true;
}
public override SelectionRules SelectionRules
{
get
{
return SelectionRules.LeftSizeable | SelectionRules.RightSizeable |
SelectionRules.Moveable;
}
}
}
}
https://stackoverflow.com/questions/2582718/create-a-custom-control-which-has-a-fixed-height-in-designer 1/2
15/5/2020 c# - Create a custom control which has a fixed height in designer - Stack Overflow
edited Jul 8 '13 at 19:55 answered Dec 8 '10 at 15:01
Cobo Abel Fontes
118 7 116 1 2
The link is broken. Am I missing a reference? I have the includes, but it still doesn't find the class
ControlDesigner for me to inherit from. Which is the minimum version of the .NET Framework this
class is available? – Matheus Rocha Jun 16 '18 at 15:36
Matheus: You have to manually add System.Design as a reference in the project. – BenW Oct 7 '18 at
18:41
Try this
8 protected override void SetBoundsCore(int x, int y,
int width, int height, BoundsSpecified specified)
{
// Set a fixed height for the control.
base.SetBoundsCore(x, y, width, 75, specified);
}
http://msdn.microsoft.com/en-
us/library/system.windows.forms.control.setboundscore(VS.71).aspx
edited Apr 6 '10 at 6:42 answered Apr 6 '10 at 6:27
Amsakanna
8,612 5 40 52
Worked for me as well. The link from the accepted answer seems to be offline, so I'm glad we have this
answer. – xsl Jan 25 '12 at 16:45
Remember to set specified to the dimensions which your override changed. In this example, you
forcebly define the height, so specified should be BoundsSpecified.Height . Like so:
base.SetBoundsCore(x, y, width, 75, BoundsSpecified.Height); Source: Documentation on
MSDN. – Matheus Rocha Jun 16 '18 at 15:42
this.MaximumSize = new System.Drawing.Size(0, 20);
this.MinimumSize = new System.Drawing.Size(0, 20);
1
Apparently .NET assumes a minimum and maximum width of 0 to be "any width".
answered Apr 6 '10 at 14:11
C.Evenhuis
23.4k 2 52 65
I usually try to reproduce an issue before I answer, but this has been over a year ago; I don't remember
answering this question, so perhaps you should just take a look at the accepted answer instead. –
C.Evenhuis Aug 5 '11 at 20:04
https://stackoverflow.com/questions/2582718/create-a-custom-control-which-has-a-fixed-height-in-designer 2/2