0% found this document useful (0 votes)
45 views

C# - Create A Custom Control Which Has A Fixed Height in Designer - Stack Overflow

The document discusses how to create a custom control in C# that has a fixed height when dragged onto a Windows form in the designer. It provides code for overriding the SetBoundsCore method to set a fixed height and for removing unwanted resize handles in the designer. Multiple responses provide solutions and clarification on the code examples.

Uploaded by

tssman01 tss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

C# - Create A Custom Control Which Has A Fixed Height in Designer - Stack Overflow

The document discusses how to create a custom control in C# that has a fixed height when dragged onto a Windows form in the designer. It provides code for overriding the SetBoundsCore method to set a fixed height and for removing unwanted resize handles in the designer. Multiple responses provide solutions and clarification on the code examples.

Uploaded by

tssman01 tss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like