本文介绍了在设计模式下,在.NET自定义控件中锁定调整高度的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发C#.NET自定义控件,并且我想防止用户在设计模式下调整Height的大小,同时允许他们调整Width的大小.

I'm developing a C# .NET Custom Control and I want to prevent the user, while in design mode, from resizing the Height, while allowing them to reize the Width.

推荐答案

我知道这个问题有点老了,但是以防万一有人找这个问题,我会尽力回答:

I know this question is a little old, but just in case someone looks for this I'll try to answer it:

您必须覆盖 SetBoundsCore 方法.像这样:

You have to override the SetBoundsCore method in your user control. Something like this:

protected override void SetBoundsCore(
    int x, int y, int width, int height, BoundsSpecified specified)
{
    // EDIT: ADD AN EXTRA HEIGHT VALIDATION TO AVOID INITIALIZATION PROBLEMS
    // BITWISE 'AND' OPERATION: IF ZERO THEN HEIGHT IS NOT INVOLVED IN THIS OPERATION
    if ((specified & BoundsSpecified.Height) == 0 || height == DEFAULT_CONTROL_HEIGHT)
    {
        base.SetBoundsCore(x, y, width, DEFAULT_CONTROL_HEIGHT, specified);
    }
    else
    {
        return; // RETURN WITHOUT DOING ANY RESIZING
    }
}

这篇关于在设计模式下,在.NET自定义控件中锁定调整高度的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!