prevent height sizing at design time - c#

I'm working on a custom user control. How can I prevent the HEIGHT ONLY of the control from being modified during the design-time interface.

You can override the SetBoundsCore method and disallow changes to height by changing the height value before calling the base class implementation.
private const int FixedHeightIWantToKeep = 100;
protected override void SetBoundsCore(
int x,
int y,
int width,
int height,
BoundsSpecified specified)
{
// Fixes height at 100 (or whatever fixed height is set to).
height = this.FixedHeightIWantToKeep;
base.SetBoundsCore(x, y, width, height, specified);
}

You can override the Height attribute from the Control class and then set the BrowsableAttribute to prevent it from being displayed in the properties windows
You can also take a look at Attributes and Design-Time Support

Related

What is the use of SetBoundsCore?

I don't know the usage of the SetBoundsCore and if any sample program it is comfortable.
I have googled but didn't get one. Is it used for retain the same value?
For example, if set the height as 100 for first time it will remain the same and if i set the height as 200 it will not change again.
I assume like this.
protected override void SetBoundsCore(
int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width,height, specified);
}
You can look at MSDN
Basically it sets coordinates and size of your control. Since it is protected and virtual, you can only call it from your user control like you specified in your question.
Difference between public method SetBounds() and SetBoundsCore() is here:
What is the difference between SetBounds and SetBoundsCore

How can I implement an AutoSize property to a Custom Control?

I want to implement an AutoSize property in a Custom Control (not User Control), in such a way that it behaves like other standard .NET WinForms controls which implement AutoSize (ala CheckBox) in design mode.
I have the property set up, but it's the way the control behaves in design mode that bugs me. it can still be resized, which doesn't make sense because the visual resize isn't reflected in the AutoSize and Size properties i've implemented.
Standard .NET controls do not allow resizing (or even show resize handles) in design mode when AutoSize is true. I want my control to behave in the same fashion.
Edit: I have it working using the SetBoundsCore() override, but it doesn't visually restrict a resize when AutoSize is set to true, it just has the same effect; the functionality is equivalent, but it feels unnatural.
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
if (!this.auto_size)
this.size = new Size(width, height);
base.SetBoundsCore(x, y, this.size.Width, this.size.Height, specified);
}
Any ideas on doing this the standard way?
Here's my recipe to have your control AutoSize.
Create a method GetAutoSize() to calculate the required size of the control according to your specific implementation. Maybe it's the size of the text it contains or the total height of the controls for the current width, whatever.
Create a method ResizeForAutoSize() to force the control to resize itself following a change in its state. For example if the control is sized for the text it contains, changing the text should have the control resized. Just call this method when the text changes.
Override GetPreferredSize() to notify whoever wants to know (like a FlowLayoutPanel for instance) what is our preferred size.
Override SetBoundsCore() to enforce our sizing rule the same way an AutoSize label cannot be resized.
See sample here.
/// <summary>
/// Method that forces the control to resize itself when in AutoSize following
/// a change in its state that affect the size.
/// </summary>
private void ResizeForAutoSize()
{
if( this.AutoSize )
this.SetBoundsCore( this.Left, this.Top, this.Width, this.Height,
BoundsSpecified.Size );
}
/// <summary>
/// Calculate the required size of the control if in AutoSize.
/// </summary>
/// <returns>Size.</returns>
private Size GetAutoSize()
{
// Do your specific calculation here...
Size size = new Size( 100, 20 );
return size;
}
/// <summary>
/// Retrieves the size of a rectangular area into which
/// a control can be fitted.
/// </summary>
public override Size GetPreferredSize( Size proposedSize )
{
return GetAutoSize();
}
/// <summary>
/// Performs the work of setting the specified bounds of this control.
/// </summary>
protected override void SetBoundsCore( int x, int y, int width, int height,
BoundsSpecified specified )
{
// Only when the size is affected...
if( this.AutoSize && ( specified & BoundsSpecified.Size ) != 0 )
{
Size size = GetAutoSize();
width = size.Width;
height = size.Height;
}
base.SetBoundsCore( x, y, width, height, specified );
}
In the constructor of your control, call SetAutoSizeMode(AutoSizeMode.GrowAndShrink).
Override SizeFromClientSize() method. in this method you must calculate required size for your control and return it.
You only need to add this two lines on top of your custom control declaration
[Designer("System.Windows.Forms.Design.LabelDesigner")]
[ToolboxItem("System.Windows.Forms.Design.AutoSizeToolboxItem")]
And naturally implement your Autosize logic

Is it Possible to set the height smaller than 21 pixels for the System.Windows.Forms.Combobox-Control

Hello Community,
i've a problem with the height of the System.Windows.Forms.Combobox-Control. I can't change it. I want to use that to write an own implementation (owner drawn custom control).
The following code does not work for me (It's only to try). The height is still 21px!
public class TestBox : ComboBox
{
public TestBox()
{
DropDownHeight = 15;
}
protected override Size DefaultSize
{
get
{
return new Size(15,15);
}
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, 15, 15, specified);
}
}
Please help me.
Regars,
Marco
The ComboBox height should be resized
based on the font that is assigned to
it.
So, change the combo font. see another discussion on this subject.
ComboBox's MinimumSize property is coded like this:
public override Size MinimumSize
{
get
{
return base.MinimumSize;
}
set
{
// can see that Height is not taken in consideration - is 0
base.MinimumSize = new Size(value.Width, 0);
}
}

Getting initial dimensions from inherited UserControl

I am trying to create a series of UserControls that all inherit from a custom UserControl object. One of the key behaviors I want to implement is the ability to dynamically resize all of the controls to fit the control size.
In order to do this, I need to get the initial width and height of the control to compare it to the resized dimensions. In my inherited controls, I can put code in the constructor after the InitializeComponent() call to grab the dimensions. Is there any way I can do this from the base object code?
Also, if there is a better approach to doing this, I am open to suggestions.
I ended up using my base control's dimensions as the fixed size for all inherited controls. This was fairly easy to do by overriding the SetBoundsCore() method:
public partial class BaseControl : UserControl
{
private int _defaultWidth;
private int _defaultHeight;
public BaseControl()
{
InitializeComponent();
_defaultWidth = this.Width;
_defaultHeight = this.Height;
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
if (this.DesignMode)
{
width = _defaultWidth;
height = _defaultHeight;
}
base.SetBoundsCore(x, y, width, height, specified);
}
}
Any controls inherited BaseControl automatically default to its fixed dimensions.
At runtime, my resize code calculates a resize ratio based on the new Width and Height vs. the _defaultWidth and _defaultHeight members.
In the user control, take advantage of the docking and anchoring properties of every control in the container. When the user control is sized or resized, the contents should adjust themselves automatically. Then in code, all you need to do is set the size of the user control.

Fixing the position of a form

I am starting a winform application[.NET 3.5, C#], where in the the main form of the application starts at a particular specified location. Am calling the following code in the constructor for this
private void SetFormPosition()
{
this.StartPosition = FormStartPosition.Manual;
this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
}
After the application starts, I would like to keep the location of the form fixed throughout the application lifetime.
Perhaps, I could 'tap' the Location event changed but am not sure if that would be very elegant.
Please suggest.
Thanks.
I agree with others that you probably shouldn't be doing this, but if you must, read on.
You can override the SetBoundsCore method and prevent any movement. We use this to prevent vertical resizing on some UserControl implementations (such as those that contain a ComboBox or other fixed height control), but it is also responsible for the location changing.
The following should get you started:
protected override void SetBoundsCore(
int x, int y, int width, int height, BoundsSpecified specified)
{
x = this.Location.X;
y = this.Location.Y;
//...etc...
base.SetBoundsCore(x, y, width, height, specified);
}
You could set the FormBorderStyle to None. This has the added benefit of removing the bar at the top of the window that would give users a false sense that they should be able to move the window.
Just change this
Location = new Point(this.Width,this.Height);

Categories