且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用按钮单击在面板中添加滚动

更新时间:2023-11-30 16:00:40

这是一种解决方法,无需移动所有按钮:



结构:



0.所有面板都有DockStyle.None



1.面板:'outmostPanel:有两个按钮,'btnPrevious,'btnNext位于'outmostPanel的左侧和右侧,垂直居中于'outmostPanel。定位在您想要的表格上。



包含一个面板的外部面板



2.面板: 'outerPanel:添加到'outmostPanel的ControlCollection中,水平居中于'outmostPanel,大小为垂直填充'outmostPanel。



包含一个Panel'innerpanel



3.面板:'innerPanel:添加到'outertPanel的ControlCollection,水平居中,



4.面板的外部面板和'innerPanel使用相同的BackGroundColor。'outmostPanel可以有一个对比的BackColor,或者与其他面板一样,如你所愿。



为了测试目的,让我们添加一个网格'innerPanel:
Here's one way to go about this that eliminates the need to move all the Buttons:

Structure:

0. all Panels have DockStyle.None

1. Panel: 'outmostPanel: has two Buttons, 'btnPrevious, 'btnNext positioned at the left and right sides of the 'outmostPanel vertically centered in the 'outmostPanel. Positioned on a Form where you want it.

Contains a Panel 'outerPanel

2. Panel: 'outerPanel: added to ControlCollection of 'outmostPanel, centered horizontally in 'outmostPanel, sized to fill 'outmostPanel vertically.

Contains a Panel" 'innerpanel

3. Panel: 'innerPanel: added to ControlCollection of 'outertPanel, centered horizontally,

4. Panels 'outerPanel and 'innerPanel use the same BackGroundColor. 'outmostPanel can have a contrasting BackColor, or the same as the other Panels, as you wish.

For testing purposes let's add a grid of Buttons to the 'innerPanel:
private const int RowOffset = 50;
private const int ColumnOffset = 120;
private const int Margin = 20;

private const int NRowsButtons = 6;
private const int NColsButtons = 8;

private Size buttonSize = new Size(40, 25);

private void MakeButtons()
{
    innerPanel.Width = 1200;

    for (int i = 0; i < NRowsButtons; i++)
    {
        for (int j = 0; j < NColsButtons; j++)
        {
            var btn = new Button
            {
                AutoSize = false,
                Text = string.Format("{0}:{1}", i.ToString(), j.ToString()),
                Size = buttonSize,
                Location = new Point((j * ColumnOffset) + Margin, (i * RowOffset) + Margin)
            };

            // assign EventHandler
            btn.Click += btn_Click;

            innerPanel.Controls.Add(btn);
        }
    }
}

// the Button Click EventHandler
private void btn_Click(object sender, EventArgs e)
{
    Button buttonClicked = sender as Button;

    // do what you need to do ...
}

控制移动'innerPanel的Button EventHandlers:

The Button EventHandlers that control moving the 'innerPanel:

// for you to figure out the values of ...

// private const int LeftThreshold = ?;
// private const int RightThreshold = ?;
// private const int PanelMoveRight = ?;
// private const int PanelMoveLeft = ?;

private void btnPrevious_Click(object sender, EventArgs e)
{
    if(innerPanel.Left < LeftThreshold) innerPanel.Left += PanelMoveRight;
}

private void btnNext_Click(object sender, EventArgs e)
{
    if (innerPanel.Left > RightThreshold) innerPanel.Left -= PanelMoveLeft;
}

当你点击'btnPrevious或'btnNext按钮时,你需要确定要移动'innerPanel的方向和像素数。当你计算出来时,将值分配给移动按钮事件中使用的常量。

The task is left up to you to figure out which direction, and by how many pixels, you want to move the 'innerPanel when the 'btnPrevious or 'btnNext Buttons are clicked. When you figure those out, assign the values to the constants used in the move button events.