c# - 如何设置 anchar 以删除按钮之间的空间

标签 c# button

我有一些按钮位于面板内部,如图所示(1) 它是一个用户控件。

我希望当有人使用我的用户控件时,当调整面板大小时,按钮大小和它们之间的空间会改变。

其实我想要图(3) 但是图(2) 会发生...

我将面板的 anchar 设置为 right、left、top、botton。如何修复图 3 中的按钮?! enter image description here

最佳答案

1 - 为用户控件中的所有按钮设置“MaximumSize”和“MinimumSize”。像这样:

btnSave.MaximumSize = new Size(80, 30);
btnSave.MinimumSize = new Size(60, 30);
btnEdit.MaximumSize = new Size(80, 30);
btnEdit.MinimumSize = new Size(60, 30);
.
.
.

或从属性窗口设置。

2 - 将所有按钮的“Anchor”属性设置为 Left,Right

3 - 在您的用户控件中为“调整大小”事件编写以下代码

private void UserControl1_Resize(object sender, EventArgs e)
    {
        int lastLeft = 0 , lastWidth  = 0 ;
        foreach (Control ctrl in this.Controls)
        {
            ctrl.Left = lastLeft + lastWidth + 3;
            lastLeft = ctrl.Left;
            lastWidth = ctrl.Width;
        }
    }

注意:不要忘记您必须从面板中剪切按钮并将它们粘贴到用户控件中。您不需要面板并且可以将其删除。

希望这有用。

关于c# - 如何设置 anchar 以删除按钮之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903363/

相关文章:

jquery - jQuery 不会在禁用的按钮上触发悬停事件吗?

c# - Convert.ToDouble 在法语系统中抛出错误

c# - 测试所需的原始电子邮件

c# - 数据集/内存不足的替换

c# - 反序列化对象时出错

java - 为什么这段Java代码在Eclipse中的结构是这样的?

c# - 如何使用 C# 测量内存使用情况(就像我们在 Java 中所做的那样)?

button - 将函数解除绑定(bind)到kivy中的按钮

android - MultiAutoCompleteTextView Android 开发中的按钮

c# - 应用按钮进入选择事件两次