How to set the button to the maximum on the left in the C # panel

I have a button already on the panel that is not visible until scrolling (since the size of the panel view is smaller than the x-cordinate of button A). I want to place button after button buttonA. How to do it? I use this, but it only puts the button to the left of the control view not on the inside maximum width.

I want it to be general, if any button goes beyond the maximum internal width, the next button should even go to that button. I can not use the docking station, since I need the same function to be placed on top.

"New question editing"

Buttons are generated after each click and have an arbitrary width. A button can be removed, but a new button must be added to the maximum width occupied so far, if the last button is removed, the next button should take place after the second leftmost button

button1.Left = buttonA.Parent.Size.Width+button1.Width;

enter image description here

+4
source share
2 answers

If you want to put button1right by buttonA, you can use the properties Leftand Width buttonAfor this:

// Places button1 to the right of buttonA by 10 pixels
button1.Left = buttonA.Left + buttonA.Width + 10;

Edit:

To be sure that I always add to the right of the last button, I can simply save the link to the last position that was used:

// Remember the last Left used. 
// We first set it to the Left of buttonA plus its Width.
int lastLeft = buttonA.Left + buttonA.Width;

// button1 now gets set to this plus a gap of 10 pixels
button1.Left = lastLeft + 10;
// Remember the last position
lastLeft = button1.Left + button1.Width;

// Set next button
button2.Left = lastLeft + 10;
// Remember...
lastLeft = button2.Left + button2.Width;

You can make this cleaner by wrapping part of this method, but I left the verbal version for clarity.

+3

Width Left:

private void button1_Click(object sender, EventArgs e)
{
    Button btn = new Button();
    btn.Left = nTotalWidth;

    panel1.Controls.Add(btn);
    nTotalWidth += btn.Width;
}

, button1.

+2

Source: https://habr.com/ru/post/1615418/


All Articles