Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
BillWoodruff 17-Nov-14 1:20am    
You don't want to use scrollbars ? You have one Panel on a Form, and all the Buttons, including Forward/Back Buttons are inside that Panel ?
victowork 17-Nov-14 1:27am    
i dont want to use scrollbars instead i have two buttons forward/backward. Scrolling should be made with these buttons
Sinisa Hajnal 17-Nov-14 2:29am    
What is that +172 shift? Are you intending for this panel to "page" through its width or you want linear scrolling like with a scrollbar? And besides, what is wrong with scrollbars? Is it touchscreen app?
victowork 17-Nov-14 3:17am    
yes its a touch screen app 172 is x axis of my first button
BillWoodruff 17-Nov-14 4:17am    
Would you like to see a way this can be done without moving the individual Buttons ?

1 solution

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:
C#
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 ...
}
The Button EventHandlers that control moving the 'innerPanel:
C#
// 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;
}
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900