Create a custom fixed height control in the designer

I want to create a custom control (derived from the Control class), and when I drag this custom control onto the form in the designer, I can only change its width. This function is similar to a single line text field.

Update. My application is Windows Form.

+4
source share
3 answers

See http://www.windowsdevelop.com/windows-forms-general/how-to-set-that-a-control-resizes-in-width-only-9207.shtml .

You override SetBoundsCore and define a constructor to remove the top and bottom resizing 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; } } } } 
+8
source

try it

 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

+7
source
  this.MaximumSize = new System.Drawing.Size(0, 20); this.MinimumSize = new System.Drawing.Size(0, 20); 

Obviously, .NET accepts a minimum and maximum width of 0 as "any width."

+1
source

Source: https://habr.com/ru/post/1306084/


All Articles