C# - Create A Custom Control Which Has A Fixed Height in Designer - Stack Overflow
C# - Create A Custom Control Which Has A Fixed Height in Designer - Stack Overflow
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.
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);
}
}
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
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
http://msdn.microsoft.com/en-
us/library/system.windows.forms.control.setboundscore(VS.71).aspx
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
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