Click here to Skip to main content
15,900,530 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have page's buttons in flowlayoutpanel`1,2,3,4 and next and previous buttons,if i'm on second page and i click on next button it will moved on 3 page, and if i click on button previous it will moved 1 page.


Who can help me, I think i must declare any variable on top for example int index,then it write in my next and previous functions.i tried it but it doesn't work.

What I have tried:

Suggested by Phil.o`


int currentPage;
int pageCount;

public Gallery()
       {
           InitializeComponent();
           for (int i = 1; i <= Controller.books.Count / 4 + 1; i++)
           {
               Button btn = new Button();
               btn.Text = i.ToString();
               flowLayoutPanel2.Controls.Add(btn);
               btn.Click += new EventHandler(show);
           }
       }


public void show(object sender, EventArgs e)
        {
            Button ays = sender as Button;
            int a = (int.Parse(ays.Text) - 1) * 4;
            int b = a + 4;
            this.Sharel(a, b);
        }


and my next and previous buttons.

Button button = new Button();
           button.Text = "previous";
           flowLayoutPanel2.Controls.Add(button);
           button.Click += new EventHandler(prev);
           Button bt = new Button();
           bt.Text = "next";
           flowLayoutPanel2.Controls.Add(bt);
           bt.Click += new EventHandler(next);


 public void prev(object sender,EventArgs e)
        {
            int a = Math.Max(0, currentPage - 1); 
            int b = Math.Min(pageCount, a + 4);   
            Sharel(a, b);
        }
public void next(object sender, EventArgs e)
        {
           int a = Math.Min(pageCount, currentPage + 1);        
            int b = Math.Min(pageCount, a + 4);           
            Sharel(a, b);
        }


public void Sharel(int start = 0, int end = 0)
       {
           currentPage = start;
           flowLayoutPanel1.Controls.Clear();
           for (int i = start; i < end; i++)
           {
               if (i >= Controller.books.Count)
               {
                   break;
               }
               if (end == 0)
               {
                   end = Controller.books.Count;
               }
               Book item = Controller.books[i];
               FlowLayoutPanel flp = new FlowLayoutPanel();
               flp.BackColor = Color.PaleGoldenrod;
               flp.AutoSize = true;
               flp.MinimumSize = new Size(10, 100);
               Label lb = new Label();
               lb.Text = "Title: " + item.title + "\n" + "Autor: " + item.author + "\n" + "Count: " + item.count + "\n" + "Price: " + item.price;
               lb.ForeColor = Color.Black;
               lb.AutoSize = true;
               flp.Controls.Add(lb);
               if (item.image != String.Empty)
               {
                   PictureBox pb = new PictureBox();
                   pb.Location = new Point(20, 80);
                   pb.Image = Image.FromFile("Nkarners/" + item.image);
                   pb.SizeMode = PictureBoxSizeMode.StretchImage;
                   flp.Controls.Add(pb);
               }
               Button bt = new Button();
               bt.Location = new Point(30, 30);
               bt.MinimumSize = new Size(10, 40);
               bt.Text = "Delete";
               flp.Controls.Add(bt);
               bt.Click += new EventHandler(delete);
               flowLayoutPanel1.Controls.Add(flp);
               bt.Tag = item.id.ToString();
               Button b = new Button();
               b.AutoSize = true;
               b.MinimumSize = new Size(10, 40);
               b.Text = "Edit";
               flp.Controls.Add(b);
               b.Click += new EventHandler(edit);
               b.Tag = item.id.ToString();
               button2.Tag = b.Tag;
               Button button = new Button();
               button.AutoSize = true;
               button.MinimumSize = new Size(10, 40);
               button.Text = "Order";
               flp.Controls.Add(button);
               button.Click += new EventHandler(order);
               button.Tag = item.id.ToString();

           }
       }
Posted
Updated 2-Mar-18 0:34am
v4
Comments
phil.o 1-Mar-18 8:30am    
The only FlowLayoutPanel class that I found is in System.Windows.Forms namespace, and does not seem to be fit to display pages. Could it be possible you need a TabControl instead?
Suren97 1-Mar-18 8:34am    
no, i need flowlayoutpanel, my pages are working, for example when i click on button 1 page it shows me datas of 1 page,when i click on button 2 page it shows me datas of 2 page and so on.Now i need add prev and next buttons
phil.o 1-Mar-18 8:37am    
Then, maybe showing the code which executes when you are clicking on button 1, for example, would help us understand how to help you :)
Suren97 1-Mar-18 8:43am    
I have written code of Pages`1,2,3,4,5 and so on, in every page has 4 datas,for example if i have 10 datas it will be 3 page`4+4+2, i just need write function for prev and next buttons.i must say when i click on next button or previous, it will moved on next page or previous.
phil.o 1-Mar-18 8:50am    
More specificaly, could you show us the code in the Sharel(int a, int b) method, please?

1 solution

Ok, the way things are organized, the Sharel displays 4 pages.
There are several things you need:
- first, you need a way to remember which page is actually displayed. Since existing buttons deal with static values, you are using these values as the start parameter. But, with previous and next buttons, you do not have static data; you need a value so that the compiler know which previous or next page to deal with.
You could add a private member variable in your form, something like
C#
int currentPage;

- second, when you create this member variable, you must then affect it a useful value when a page is actually displayed. For example, you could add:
C#
currentPage = start;
somewhere in the Sharel method.
- finally, from here you ahev an actual value which you can use in the next and previous methods. But, you also have to take care of the edge cases where you are displaying the first and last pages. For the first page, this is simple since its index is zero. For the last page, again you need a way to have an actual, valid value for the number of pages which you are actually displaying. This could be declared this way
C#
int pageCount;
You will have to give it a meaningful value yourself, at the time you are loading the pages in the control; because you will be able to get this value at that time. Then you could write the methods this way:
C#
public void prev(object sender,EventArgs e)
{
   int a = Math.Max(0, currentPage - 1); // Lower limit of start to zero
   int b = Math.Min(pageCount, a + 4);   // Upper limit of end to page count
   Sharel(a, b);
}

public void next(object sender, EventArgs e)
{
   int a = Math.Min(pageCount, currentPage + 1); // Upper limit of start
                                                 // to page count
   int b = Math.Min(pageCount, a + 4);           // Upper limit of end
                                                 // to page count
   Sharel(a, b);
}
 
Share this answer
 
v2
Comments
Suren97 1-Mar-18 9:33am    
But i need also create like this in function`
Button prev = sender as Button;
phil.o 1-Mar-18 9:36am    
Why? Are there two buttons in a glocal space, or do you have two previous and next buttons in each page?
Suren97 1-Mar-18 9:38am    
how can i send you picture?
phil.o 1-Mar-18 9:42am    
You cannot, sorry.
Suren97 1-Mar-18 9:43am    
don't you have fb?

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