gpt4 book ai didi

c# - 在设计器中创建一个具有固定高度的自定义控件

转载 作者:太空狗 更新时间:2023-10-29 22:07:41 25 4
gpt4 key购买 nike

我想创建一个自定义控件(派生自 Control 类),当我将此自定义控件拖到设计器中的窗体时,我只能更改其宽度。此功能与单行文本框相同。

更新:我的应用程序是 Windows 窗体。

最佳答案

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

您覆盖 SetBoundsCore 并定义一个 Designer 以删除顶部和底部调整大小的 handle 。

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;
}
}
}
}

关于c# - 在设计器中创建一个具有固定高度的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2582718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com