System.Windows.Forms.Button 周围的边距
如何围绕 System.Windows.Forms.Button 控件制作“固定”边距?
var button = new System.Windows.Forms.Button();
button.Dock = DockStyle.Fill;
button.Margin = new Padding(20);
var panel = new System.Windows.Forms.Panel();
panel.Controls.Add(button);
在上面的示例中,按钮在容器面板内不会有任何边距。
是否可以以某种方式实现,以便按钮周围有 20px 空间,但仍然表现得像普通按钮(例如从 System.Windows.Forms.Button 类继承并进行一些自定义绘制)?
编辑:让我解释一下我想要做什么。我想要这样的按钮控件,它具有“.Dock = DockStyle.Right”属性集。此外,它的左侧也会有填充。因此,面板上的此类按钮很少,会将它们堆叠到面板的右侧。 为什么?例如,我有 3 个这样的按钮堆叠在右侧。在某些情况下,我想隐藏中间的一个。我会设置它的属性“.Visibile = false”,以便它被隐藏。在这种情况下,最右边的按钮将堆叠到最左边,它们之间的间距相同。
How can "solid" margins be made around System.Windows.Forms.Button control?
var button = new System.Windows.Forms.Button();
button.Dock = DockStyle.Fill;
button.Margin = new Padding(20);
var panel = new System.Windows.Forms.Panel();
panel.Controls.Add(button);
In the example above the button won't have any margins within the container panel.
Is it somehow possible to implement so the button would have 20px space around it but still behave like an ordinary Button (e.g. inherit from System.Windows.Forms.Button class and do some custom draw)?
Edit: Let me explain what I am trying to do. I would like to have such button control which would have ".Dock = DockStyle.Right" property set. Also it would have padding on the left. Thus, having few such buttons on the panel would stack them to the right of the panel.
Why for? E.g. I have 3 such buttons stacked to the right. In some cases I would like to hide middle one. I would set it's property ".Visibile = false" so it is hidden. In this case the rightmost button would stack to the leftmost having the same space between them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像您尝试的那样将按钮放置在面板中,但将 Dock 属性保留为默认值。相反,根据需要调整面板中按钮的大小,并设置按钮的 将属性锚定到顶部、右侧、左侧和底部。保留默认的 Dock 属性值并按照我的描述设置 Anchor 属性将导致按钮在调整面板大小时在边距内调整大小。
Place the Button in the Panel like you tried but leave the Dock property at default. Instead, size the Button within the Panel as you want and set the Button's Anchor property to Top, Right, Left and Bottom. Keeping the default Dock property value and setting the Anchor property as I desribed will cause the button to resize within the margin as you resize the Panel.