Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Automatically add 'X' amount of buttons, one after another.

Tags:

c#

winforms

I have a Windows form where I am adding a button control for each monitor attached to a computer. Naturally as the number of displays very from PC to PC, I want to automatically add a button per display and add them so they are displayed in a row.

Currently my code is as so:

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

                }

This is working however, it's simply placing each button on top of each other where there is more than one display (as I expected it would):

Currently.

What I want to achieve is that each button is added, but in a row next to each other. I've tried various threads, searches on Google etc to no avail. Could anyone point me in the right direction please?

What I'm trying to achieve.

like image 781
Rawns Avatar asked Dec 11 '22 14:12

Rawns


2 Answers

IIRC AllScreens can be indexed, so:

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

That ought to do it.

Advantages of this over the other answers: counter/indexer is built into the loop.

like image 120
Der Kommissar Avatar answered May 10 '23 13:05

Der Kommissar


I couldn't try but shouldn't you set the Location different per button?

Location = new Point(12, 70),

to e.g.

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

where 100 is the width of the screen gap is the gap between two screens and screen_index the index from left to right

like image 29
Michel Keijzers Avatar answered May 10 '23 14:05

Michel Keijzers