如何使用按钮单击在面板中添加滚动 [英] How Can I Add Scroll In Panel Using Button Clicks

查看:63
本文介绍了如何使用按钮单击在面板中添加滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的Coders,

我的Winform(c#)中有一个面板,其中包含n个按钮。我想使用位于面板左侧和右侧的按钮单击滚动面板。我写了一些东西,但它并不像我预期的那样适合我。我的代码是这样的,



private void btnPrevious_Click(object sender,EventArgs e)

{

for(int i = 0; i< pnlSubCat.Controls.Count; i ++)

{

Button btn = pnlSubCat.Controls [i] as Button;

if(i> 0)

{





Button prebtn = pnlSubCat。将[i - 1]控制为按钮;

int x = btn.Location.X - 172;

btn.Location = new Point(x,7);

}

if(i == 0)

{

int x = btn.Location.X - 172;

btn.Location = new Point(x,7);

}

}

}



private void btnNext_Click(object sender,EventArgs e )

{

for(int i = pnlSubCat.Controls.Count;我> 0;我 - )

{



if(i< = pnlSubCat.Controls.Count)

{

Button btn = pnlSubCat.Controls [i-1] as Button;



Button prebtn = pnlSubCat.Controls [i - 1] as按钮;

int x = btn.Location.X + 172;

btn.Location = new Point(x,7);

}

}

}

写这个有效但是如果面板中没有控件,它就不会停止改变位置。我想在第一个和最后一个停止位置变化。

Dear Coders,
I have a panel in my Winform(c#) which contains n number of Buttons. I want to Scroll the panel using Button clicks which was situated in Left and Right of the panel. I have Written something but it didn't work for me as i expected. My code is just like this,

private void btnPrevious_Click(object sender, EventArgs e)
{
for (int i = 0; i < pnlSubCat.Controls.Count; i++)
{
Button btn = pnlSubCat.Controls[i] as Button;
if (i > 0)
{


Button prebtn = pnlSubCat.Controls[i - 1] as Button;
int x = btn.Location.X - 172;
btn.Location = new Point(x, 7);
}
if (i == 0)
{
int x = btn.Location.X - 172;
btn.Location = new Point(x, 7);
}
}
}

private void btnNext_Click(object sender, EventArgs e)
{
for (int i = pnlSubCat.Controls.Count; i > 0; i--)
{

if (i <= pnlSubCat.Controls.Count)
{
Button btn = pnlSubCat.Controls[i-1] as Button;

Button prebtn = pnlSubCat.Controls[i - 1] as Button;
int x = btn.Location.X + 172;
btn.Location = new Point(x, 7);
}
}
}
While writing this it works But it wont stop changing position if There is no controls in the panel. I want to stop the position changing at the first and the last.

推荐答案

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



结构:



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.


这篇关于如何使用按钮单击在面板中添加滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆