通过下一个和上一个按钮解析的文本框值数组
本文关键字:文本 数组 下一个 上一个 按钮 | 更新日期: 2023-09-27 18:33:00
我在wform中有一个带有下一个和上一个按钮的表单。我有一个数组中存储的数字范围,并希望通过按钮浏览它们。我需要关于如何做到这一点的帮助和一个可能的例子。我尝试过并到处寻找,但无法得出结论,因为我现在已经撞到了一堵砖墙。
private nextButton_Clicked()
{
int counter;
if (counter == 0)
{
//content of first page
}
}
以上仅适用于第二页,但我需要一些功能性和非重复性的东西。
private void btnNext_Click(object sender, EventArgs e)
{
if (pos < someArray.Count() - 1)
{
pos++;
label1.Text = someArray[pos];
}
}
private void btnPrev_Click(object sender, EventArgs e)
{
if (pos > 0)
{
pos--;
label1.Text = someArray[pos];
}
}
'
如果您正在寻找一种可重用的方法来处理此问题,我会将分页逻辑分解为一个单独的类。 解释是内联的。 请务必注意,此实现假定可以安全地多次枚举正在分页的数据。 由于听起来您使用的数据结构是一个数组,因此这应该没问题。
public class Pageable<T>
{
//This is your array data
private readonly IEnumerable<T> _data;
//Number of items per page
private readonly int _pageSize;
//The current page (1-indexed)
private int _currentPage;
//Calculate Total number of pages based on number of items and user defined page size
public int TotalPages
{
get { return (int)Math.Ceiling((double)_data.Count() / _pageSize); }
}
//Constructor to load the data
public Pageable(IEnumerable<T> data, int pageSize = 10)
{
_currentPage = 1;
_pageSize = pageSize;
_data = data;
}
public IEnumerable<T> NextPage()
{
//increment the page
_currentPage++;
//make sure that the incremented value does not exceed total pages
if (_currentPage >= TotalPages) _currentPage = TotalPages;
return GetPage(_currentPage);
}
public IEnumerable<T> PreviousPage()
{
//decrement the page
_currentPage--;
//make sure the decremented value is not less than 0
if (_currentPage <= 0) _currentPage = 0;
return GetPage(_currentPage);
}
public IEnumerable<T> GetCurrentPage()
{
return GetPage(_currentPage);
}
public IEnumerable<T> GoToPage(int page)
{
if (page <= 0)
throw new Exception("Cannot be less than 1.");
if (page > TotalPages)
throw new Exception("Cannot be greater than total number of pages.");
_currentPage = page;
return GetPage(_currentPage);
}
//The page value is the value that the user would expect. (1-indexed)
private IEnumerable<T> GetPage(int page)
{
//Correct for 1-indexed page value;
var pageIndex = page - 1;
//Use Language Integrated Query (LINQ) to calculate the items we need
//Skip the first Nth items (pageIndex * _pageSize))
//Take the next _pageSize items
return _data.Skip(pageIndex * _pageSize).Take(_pageSize);
}
}
以下是一些使用 MSTest 的测试,显示了它是如何工作的:
[TestClass]
public class PageableTests
{
private int[] _data;
[TestInitialize]
public void SetUp()
{
var initialData = new List<int>();
for (var i = 1; i < 136; i++)
initialData.Add(i);
_data = initialData.ToArray();
}
[TestMethod]
public void CanPageNext()
{
var testData = new Pageable<int>(_data);
var firstPage = testData.GetCurrentPage().ToList();
Assert.AreEqual(1, firstPage.First());
Assert.AreEqual(10, firstPage.Last());
var secondPage = testData.NextPage().ToList();
Assert.AreEqual(11, secondPage.First());
Assert.AreEqual(20, secondPage.Last());
}
[TestMethod]
public void ReturnsFirstPageWhenPageIsNegative()
{
var testData = new Pageable<int>(_data);
var firstPage = testData.GetCurrentPage().ToList();
Assert.AreEqual(1, firstPage.First());
Assert.AreEqual(10, firstPage.Last());
var secondPage = testData.PreviousPage().ToList();
Assert.AreEqual(1, secondPage.First());
Assert.AreEqual(10, secondPage.Last());
var thirdPage = testData.PreviousPage().ToList();
Assert.AreEqual(1, thirdPage.First());
Assert.AreEqual(10, thirdPage.Last());
}
[TestMethod]
public void CanPreviousPage()
{
var testData = new Pageable<int>(_data);
var firstPage = testData.GetCurrentPage().ToList();
Assert.AreEqual(1, firstPage.First());
Assert.AreEqual(10, firstPage.Last());
var secondPage = testData.NextPage().ToList();
Assert.AreEqual(11, secondPage.First());
Assert.AreEqual(20, secondPage.Last());
testData.NextPage();
var thirdPage = testData.PreviousPage().ToList();
Assert.AreEqual(secondPage.First(), thirdPage.First());
Assert.AreEqual(secondPage.Last(), thirdPage.Last());
var fourthPage = testData.PreviousPage().ToList();
Assert.AreEqual(firstPage.First(), fourthPage.First());
Assert.AreEqual(firstPage.Last(), fourthPage.Last());
}
[TestMethod]
public void CannotExcceedMaxPages()
{
var testData = new Pageable<int>(_data);
for (var i = 0; i < 20; i++)
{
testData.NextPage();
}
var lastPage = testData.GetCurrentPage().ToList();
Assert.AreEqual(131, lastPage.First());
Assert.AreEqual(135, lastPage.Last());
var nextToLastPage = testData.PreviousPage();
Assert.AreEqual(121, nextToLastPage.First());
}
}