Reputation: 416
I have a standard panel which I add controls too, when the number of controls exceeds the panels size I want to clear the panel ready for the "next page" of controls which just is a clear and location reset of the buttons. Problem is when ever I clear the controls new ones will not add. Here is what I have:
// MDButList is the collection of controls
ButPosX = 2; // x position of button on panel
ButPosY = 2; // y position of button on panel
PageCount = 1; // page number
for (int i = 0; i <= MDButList.Count - 1; i++)
{
NewPOBut = MDButList[i]; // as cant ref a collection for some reason..
if (i % 14 == 0) // panel can only hold 14
{
if (i < 13) // for first item (0)
SetPOButPos(ref ButPosX, ref ButPosY, ref NewPOBut);
//sets the buttons point value and increments x & y
else
{
panMDItems.Controls.Clear();
ButPosX = 2;
ButPosY = 2;
PageCount++;
SetPOButPos(ref ButPosX, ref ButPosY, ref NewPOBut);
btnPrevPage.Visible = true;
btnNextPage.Visible = true;
labPageNum.Visible = true;
labPageNum.Text = PageCount.ToString() + " / " + PageCount.ToString();
}
}
else
{
SetPOButPos(ref ButPosX, ref ButPosY, ref NewPOBut);
}
}
Controls are being added to the collection and the code steps through as expected but after 14 controls I just get a blank panel nothing above a 14 control count will add? Ask if you need more info, thanks!
Upvotes: 0
Views: 58
Reputation: 7713
I was kind of bored, so i've made a sample of how i would do what you are asking for. This is my code, see if it fits you.
public partial class Form1 : Form
{
private int page = 1;
private int pageCount = 0;
List<Button> MDButList = new List<Button>();
public Form1()
{
InitializeComponent();
GenerateButtons(60);
SetButtons(page);
}
private void GenerateButtons(decimal number)
{
for (int i = 0; i < number; i++)
{
Button a = new Button();
a.Text = "But" + i;
MDButList.Add(a);
}
pageCount = Convert.ToInt32(Math.Ceiling(number / 14));
}
private void SetButtons(int page)
{
labPageNum.Text = page.ToString() + " / " + pageCount.ToString();
int ButPosX = 2;
int ButPosY = 2;
for (int i = panMDItems.Controls.Count - 1; i >= 0; --i)
panMDItems.Controls[i].Dispose();
int upperlimit=(page * 14) - 1;
if (upperlimit>MDButList.Count-1) upperlimit=MDButList.Count-1;
for (int i = (page-1) * 14; i <=upperlimit ; i++)
{
Button NewPOBut = MDButList[i];
SetPOButPos(ref ButPosX, ref ButPosY, ref NewPOBut);
if (i % 2 != 0)
{
ButPosX = 2;
ButPosY += NewPOBut.Height + 10;
}
else
{
ButPosX += NewPOBut.Width + 10;
}
}
}
private void SetPOButPos(ref int ButPosX, ref int ButPosY, ref Button NewPOBut)
{
NewPOBut.Location = new Point(ButPosX, ButPosY);
panMDItems.Controls.Add(NewPOBut);
}
private void btnPrevPage_Click(object sender, EventArgs e)
{
if (page > 1) page--;
SetButtons(page);
}
private void btnNextPage_Click(object sender, EventArgs e)
{
if (page < pageCount) page++;
SetButtons(page);
}
}
Upvotes: 1