【问题标题】:C# - Automatically add 'X' amount of buttons, one after another.C# - 一个接一个地自动添加“X”个按钮。
【发布时间】:2016-09-27 08:19:52
【问题描述】:

我有一个 Windows 窗体,我在其中为连接到计算机的每台显示器添加一个按钮控件。自然,由于 PC 之间的显示器数量非常多,我想自动为每个显示器添加一个按钮并添加它们,以便它们连续显示。

目前我的代码是这样的:

 foreach (var screen in Screen.AllScreens)
                {

                    Button monitor = new Button
                    {
                        Name = "Monitor" + screen,
                        AutoSize = true,
                        Size = new Size(100, 60),
                        Location = new Point(12, 70),
                        ImageAlign = ContentAlignment.MiddleCenter,
                        Image = Properties.Resources.display_enabled,
                        TextAlign = ContentAlignment.MiddleCenter,
                        Font = new Font("Segoe UI", 10, FontStyle.Bold),
                        ForeColor = Color.White,
                        BackColor = Color.Transparent,
                        Text = screen.Bounds.Width + "x" + screen.Bounds.Height
                    };


                    monitorPanel.Controls.Add(monitor);

                }

这是可行的,但是它只是将每个按钮放在彼此之上,那里有多个显示器(正如我所期望的那样):

我想要实现的是添加每个按钮,但彼此相邻。我尝试了各种线程,在 Google 上搜索等都无济于事。谁能指出我正确的方向?

【问题讨论】:

  • 您应该使用Toolstrip 控件来执行此操作;在运行时向它添加和删除按钮很容易,它负责所有的定位。
  • 为什么要手动设置位置?您可以简单地使用TableLayoutPanelFlowLayoutPanel,而无需计算按钮的Location。例如,查看 this postthis one 或许多其他有关使用这些控件创建动态流/控件表的示例。
  • 感谢您的信息。我真的不需要,只是我在编码方面没有过多的经验,而且我还在学习很多东西。我大致知道如何按照我的方式进行操作,但我会查看推荐的资源。 :)

标签: c# winforms


【解决方案1】:

IIRC AllScreens 可以被索引,所以:

var padding = 5;
var buttonSize = new Size(100, 60);
for (int i = 0; i < Screen.AllScreens.Length; i++)
{
    var screen = Screen.AllScreens[i];
    Button monitor = new Button
    {
        Name = "Monitor" + screen,
        AutoSize = true,
        Size = buttonSize,
        Location = new Point(12 + i * (buttonSize.Width + padding), 70),
        ImageAlign = ContentAlignment.MiddleCenter,
        Image = Properties.Resources.display_enabled,
        TextAlign = ContentAlignment.MiddleCenter,
        Font = new Font("Segoe UI", 10, FontStyle.Bold),
        ForeColor = Color.White,
        BackColor = Color.Transparent,
        Text = screen.Bounds.Width + "x" + screen.Bounds.Height
    };

    monitorPanel.Controls.Add(monitor);
}

应该这样做。

相对于其他答案的优势:计数器/索引器内置于循环中。

【讨论】:

  • 完美,这是一种享受。按钮重叠得非常轻微,但我相信我可以玩弄并解决这个问题。非常感谢! :)
  • @Rawns 没问题,伙计,很高兴解决了。只需弄乱padding 并找到一个使其看起来像您想要的方式的值。 :)
  • 干得好,但请考虑以下几点:1) 如果您使用TableLayoutPanelFlowLayoutPanel,则无需手动设置Location 控件,并且可以简单地控制表格或垂直/水平流中的布局。 2) 如果设置AutoSize=true,为什么还要设置Size?您不需要同时使用这两个选项。 3) 不应像在"Monitor" + screenscreen.Bounds.Width + "x" + screen.Bounds.Height 中那样连接字符串和整数。您可以改用string.Format
【解决方案2】:

我无法尝试,但您不应该为每个按钮设置不同的位置吗?

Location = new Point(12, 70),

例如

Location = new Point(12 + (100 + gap) * screen_index, 70),

其中 100 是屏幕的宽度 gap 是两个屏幕之间的间隙 而screen_index是从左到右的索引

【讨论】:

    【解决方案3】:

    您可以控制设置位置。您实际上是在自己设置:

    Size = new Size(100, 60),
    Location = new Point(12, 70)
    

    我建议您使用每个按钮的大小和额外的填充来增加位置:

    Location = new Point(screenNumber * (100 + 5), 70)
    

    什么的。当然screenNumber 是一个计数器,你必须在每次迭代时声明、初始化和递增。

    【讨论】:

    • Order-of-operations 表明你这里的 padding 不会生效。想象一下这样的几个循环:第一个循环:0 * 100 + 5 == 5,第二个循环:1 * 100 + 5 == 105,第三个循环:2 * 100 + 5 == 205,也许你的意思是screenNumber * (100 + 5)
    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多