C#如何为flowlayoutpanel中的下一个和上一个按钮编写代码 [英] C# how to write code for next and previous buttons in flowlayoutpanel

查看:83
本文介绍了C#如何为flowlayoutpanel中的下一个和上一个按钮编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在flowlayoutpanel`1,2,3,4以及下一个和上一个按钮中有页面按钮,如果我在第二页上,我点击下一个按钮它将移动到3页,如果我点击按钮上一个它将移动1页。





谁能帮助我,我想我必须在顶部声明任何变量,例如int index,然后它写在我的下一个和以前的函数中。我尝试过但它不起作用。



我尝试过的事情:



由Phil.o建议。





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();

           }
       }

推荐答案

好的,事情的组织方式, Sharel 显示4页。

您需要几件事:

- 首先,您需要一种方法记住实际显示的页面。由于现有按钮处理静态值,因此您将这些值用作 start 参数。但是,使用之前的 next 按钮,您没有静态数据;你需要一个值,以便编译器知道要处理的是哪个上一页或下一页。

你可以在表单中添加一个私有成员变量,比如

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
int currentPage;



- 第二,当你创建这个成员变量时,你必须在实际显示页面时将其影响为有用的值。例如,您可以在 Sharel

currentPage = start;

>方法。

- 最后,从这里你可以得到一个实际值,你可以在 next 之前使用方法。但是,您还必须处理显示第一页和最后一页的边缘情况。对于第一页,这很简单,因为它的索引为零。对于最后一页,您还需要一种方法来为实际显示的页数提供实际有效的值。这可以通过这种方式声明

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

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:

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);
}


这篇关于C#如何为flowlayoutpanel中的下一个和上一个按钮编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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