winforms 如何在自定义组合框中添加分页按钮

pdkcd3nj  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(241)

我想从一个互联网连接的服务器上检索大量数据(100000行),并在一个自定义组合框中输入数据的ID。大量数据会导致数据检索花费很长时间,这是低效的。
为了克服这个问题,我想在自定义组合框的底部添加一个分页按钮,以便根据需要显示某些数据(可能是100行)。
在C# WinForm中打开组合框时,如何在自定义组合框底部显示分页按钮?
谢谢

自定义组合框

namespace DiscountCard.View.CustomControl
{
    public partial class CtechComboBox : ComboBox
    {
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        }

        public CtechComboBox()
        {
            InitializeComponent();

            DoubleBuffered = true;
            DropDownStyle = ComboBoxStyle.DropDownList;
            DrawMode = DrawMode.OwnerDrawFixed;
            Font = new Font("Consolas", 11F, FontStyle.Regular, GraphicsUnit.Point);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();

            if (e.Index < 0 || e.Index >= Items.Count)
            {
                return;
            }
            var item = Items[e.Index];
            String? text = item == null ? "(null)" : item.ToString();

            TextRenderer.DrawText(e.Graphics, text, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);

            base.OnDrawItem(e);
        }

    }
}

分页按钮

using CardData.Ext.Mod.Logger;
using DiscountCard.Ext.Mod.General;

namespace DiscountCard.View.CustomControl
{
    public partial class CtechPagination : UserControl
    {
        public class Page
        {
            public int MinIndex { get; set; }
            public int MaxIndex { get; set; }
        }

        public enum PaginationPosition
        {
            FirstPage,
            PreviousPage,
            NextPage,
            LastPage,
            CurrrentPage
        }

        public CtechPagination()
        {
            InitializeComponent();

            FirstPaginationButton.Click += OnFirstPaginationButton_Click;
            PrevPaginationButton.Click += OnPrevPaginationButton_Click;
            NextPaginationButton.Click += OnNextPaginationButton_Click;
            LastPaginationButton.Click += OnLastPaginationButton_Click;
        }

        public void SetInitialize(IQueryable<int> source, bool newContent, PaginationPosition selectedPagination)
        {
            PageSize = Common.DEFAULT_PAGE_SIZE;

            TotalCount = source.Count();
            if (TotalCount <= 0)
            {
                return;
            }

            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            if (TotalPages <= 0)
            {
                return;
            }

            if (newContent)
            {
                Pages = new List<Page>();
                for (int i = 0; i < TotalPages; i++)
                {
                    IQueryable<int> indexes = (IQueryable<int>)source.Skip(i * PageSize).Take(PageSize);
                    if (indexes != null)
                    {
                        Page page = new Page();
                        page.MinIndex = 1 + indexes.Min();
                        page.MaxIndex = 1 + indexes.Max();
                        Pages.Add(page);
                    }
                }
            }

            if (Pages != null)
            {
                switch (selectedPagination)
                {
                    case PaginationPosition.FirstPage:
                        if (newContent)
                        {
                            PageIndex = 0;
                        }

                        Page? firstPage = Pages[PageIndex];
                        if (firstPage != null)
                        {
                            PagesLabel.Text = String.Format("{0:N0} of {1:N0}", firstPage.MinIndex, firstPage.MaxIndex);
                            FirstPage = firstPage;

                            CurrentPage = firstPage;
                        }
                        break;

                    case PaginationPosition.PreviousPage:
                        if (newContent)
                        {
                            --PageIndex;
                        }
                        if (PageIndex < 0)
                        {
                            PageIndex = 0;
                        }

                        Page? prevPage = Pages[PageIndex];
                        if (prevPage != null)
                        {
                            PagesLabel.Text = String.Format("{0:N0} of {1:N0}", prevPage.MinIndex, prevPage.MaxIndex);
                            PreviousPage = prevPage;

                            CurrentPage = prevPage;
                        }
                        break;

                    case PaginationPosition.NextPage:
                        if (newContent)
                        {
                            ++PageIndex;
                        }
                        if (PageIndex > Pages.Count - 1)
                        {
                            PageIndex = Pages.Count - 1;
                        }

                        Page? nextPage = Pages[PageIndex];
                        if (nextPage != null)
                        {
                            PagesLabel.Text = String.Format("{0:N0} of {1:N0}", nextPage.MinIndex, nextPage.MaxIndex);
                            NextPage = nextPage;

                            CurrentPage = nextPage;
                        }
                        break;

                    case PaginationPosition.LastPage:
                        if (newContent)
                        {
                            PageIndex = Pages.Count - 1;
                        }
                        if (PageIndex < 0)
                        {
                            PageIndex = 0;
                        }

                        Page? lastPage = Pages[PageIndex];
                        if (lastPage != null)
                        {
                            PagesLabel.Text = String.Format("{0:N0} of {1:N0}", lastPage.MinIndex, lastPage.MaxIndex);
                            LastPage = lastPage;

                            CurrentPage = lastPage;
                        }
                        break;

                    case PaginationPosition.CurrrentPage:
                        if (PageIndex < 0)
                        {
                            PageIndex = 0;
                        }
                        if (PageIndex > Pages.Count - 1)
                        {
                            PageIndex = Pages.Count - 1;
                        }

                        Page? curentPage = Pages[PageIndex];
                        if (curentPage != null)
                        {
                            PagesLabel.Text = String.Format("{0:N0} of {1:N0}", curentPage.MinIndex, curentPage.MaxIndex);
                            CurrentPage = curentPage;
                        }
                        break;
                }
            }
        }

        private EventHandler? _firstPaginationButtonEventHandler;
        public event EventHandler FirstPaginationButton_Click
        {
            add
            {
                _firstPaginationButtonEventHandler += value;
            }
            remove
            {
                _firstPaginationButtonEventHandler -= value;
            }
        }

        private EventHandler? _prevPaginationButtonEventHandler;
        public event EventHandler PrevPaginationButton_Click
        {
            add
            {
                _prevPaginationButtonEventHandler += value;
            }
            remove
            {
                _prevPaginationButtonEventHandler -= value;
            }
        }

        private EventHandler? _nextPaginationButtonEventHandler;
        public event EventHandler NextPaginationButton_Click
        {
            add
            {
                _nextPaginationButtonEventHandler += value;
            }
            remove
            {
                _nextPaginationButtonEventHandler -= value;
            }
        }

        private EventHandler? _lastPaginationButtonEventHandler;
        public event EventHandler LastPaginationButton_Click
        {
            add
            {
                _lastPaginationButtonEventHandler += value;
            }
            remove
            {
                _lastPaginationButtonEventHandler -= value;
            }
        }

        protected void OnFirstPaginationButton_Click(object? sender, EventArgs e)
        {
            try
            {
                EventHandler? handler = _firstPaginationButtonEventHandler;
                if (handler != null)
                {
                    if (Pages == null)
                    {
                        return;
                    }
                    if (Pages.Count == 0)
                    {
                        return;
                    }

                    PageIndex = 0;

                    Page? firstPage = Pages[PageIndex];
                    if (firstPage != null)
                    {
                        PagesLabel.Text = String.Format("{0:N0} of {1:N0}", firstPage.MinIndex, firstPage.MaxIndex);
                        FirstPage = firstPage;

                        CurrentPage = firstPage;

                        handler(sender, e);
                    }
                }
            }
            catch (Exception ex)
            {
                ExLogger.LogException(ex, "");
            }
        }

        protected void OnPrevPaginationButton_Click(object? sender, EventArgs e)
        {
            try
            {
                EventHandler? handler = _prevPaginationButtonEventHandler;
                if (handler != null)
                {
                    if (Pages == null)
                    {
                        return;
                    }
                    if (Pages.Count == 0)
                    {
                        return;
                    }

                    --PageIndex;
                    if (PageIndex < 0)
                    {
                        PageIndex = 0;
                    }

                    Page? prevPage = Pages[PageIndex];
                    if (prevPage != null)
                    {
                        PagesLabel.Text = String.Format("{0:N0} of {1:N0}", prevPage.MinIndex, prevPage.MaxIndex);
                        PreviousPage = prevPage;

                        CurrentPage = prevPage;

                        handler(sender, e);
                    }
                }
            }
            catch (Exception ex)
            {
                ExLogger.LogException(ex, "");
            }
        }

        protected void OnNextPaginationButton_Click(object? sender, EventArgs e)
        {
            try
            {
                EventHandler? handler = _nextPaginationButtonEventHandler;
                if (handler != null)
                {
                    if (Pages == null)
                    {
                        return;
                    }
                    if (Pages.Count == 0)
                    {
                        return;
                    }

                    ++PageIndex;
                    if (PageIndex > Pages.Count - 1)
                    {
                        PageIndex = Pages.Count - 1;
                    }

                    Page? nextPage = Pages[PageIndex];
                    if (nextPage != null)
                    {
                        PagesLabel.Text = String.Format("{0:N0} of {1:N0}", nextPage.MinIndex, nextPage.MaxIndex);
                        NextPage = nextPage;

                        CurrentPage = nextPage;

                        handler(sender, e);
                    }
                }
            }
            catch (Exception ex)
            {
                ExLogger.LogException(ex, "");
            }
        }

        protected void OnLastPaginationButton_Click(object? sender, EventArgs e)
        {
            try
            {
                EventHandler? handler = _lastPaginationButtonEventHandler;
                if (handler != null)
                {
                    if (Pages == null)
                    {
                        return;
                    }
                    if (Pages.Count == 0)
                    {
                        return;
                    }

                    PageIndex = Pages.Count - 1;

                    Page? lastPage = Pages[PageIndex];
                    if (lastPage != null)
                    {
                        PagesLabel.Text = String.Format("{0:N0} of {1:N0}", lastPage.MinIndex, lastPage.MaxIndex);
                        LastPage = lastPage;

                        CurrentPage = lastPage;

                        handler(sender, e);
                    }
                }
            }
            catch (Exception ex)
            {
                ExLogger.LogException(ex, "");
            }
        }

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
        public List<Page>? Pages { get; private set; }

        public Page? CurrentPage { get; private set; }
        public Page? FirstPage { get; private set; }
        public Page? PreviousPage { get; private set; }
        public Page? NextPage { get; private set; }
        public Page? LastPage { get; private set; }

    }
}
aiqt4smr

aiqt4smr1#

using System.Diagnostics;
using static DiscountCard.View.CustomControl.CtechPagination;

namespace DiscountCard.View.CustomControl
{
    public partial class CtechComboBoxPagination : ComboBox
    {
        public CtechPagination? Pagination { get; set; }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        }

        public CtechComboBoxPagination()
        {
            InitializeComponent();

            DoubleBuffered = true;
            DropDownStyle = ComboBoxStyle.DropDownList;
            DrawMode = DrawMode.OwnerDrawFixed;
            Font = new Font("Consolas", 11F, FontStyle.Regular, GraphicsUnit.Point);
            
            Pagination = new CtechPagination();
            Pagination.SetInitialize(Enumerable.Range(0, 100).AsQueryable(), newContent: true, PaginationPosition.LastPage);

            Pagination.FirstPaginationButton_Click += Pagination_FirstPaginationButton_Click;
            Pagination.PrevPaginationButton_Click += Pagination_PrevPaginationButton_Click;
            Pagination.NextPaginationButton_Click += Pagination_NextPaginationButton_Click;
            Pagination.LastPaginationButton_Click += Pagination_LastPaginationButton_Click;
        }

        private void Pagination_FirstPaginationButton_Click(object? sender, EventArgs e)
        {
            if (Pagination == null || Pagination.FirstPage == null) {
                return;
            }
            Items.Clear();
            for (int i = Pagination.FirstPage.MinIndex; i <= Pagination.FirstPage.MaxIndex; i++)
            {
                Items.Add(i);
            }
            if (!DroppedDown)
            {
                DroppedDown = true;
            }
        }
        private void Pagination_PrevPaginationButton_Click(object? sender, EventArgs e)
        {
            if (Pagination == null || Pagination.PreviousPage == null)
            {
                return;
            }
            Items.Clear();
            for (int i = Pagination.PreviousPage.MinIndex; i <= Pagination.PreviousPage.MaxIndex; i++)
            {
                Items.Add(i);
            }
            if (!DroppedDown)
            {
                DroppedDown = true;
            }
        }
        private void Pagination_NextPaginationButton_Click(object? sender, EventArgs e)
        {
            if (Pagination == null || Pagination.NextPage == null)
            {
                return;
            }
            Items.Clear();
            for (int i = Pagination.NextPage.MinIndex; i <= Pagination.NextPage.MaxIndex; i++)
            {
                Items.Add(i);
            }
            if (!DroppedDown)
            {
                DroppedDown = true;
            }
        }
        private void Pagination_LastPaginationButton_Click(object? sender, EventArgs e)
        {
            if (Pagination == null || Pagination.LastPage == null)
            {
                return;
            }
            Items.Clear();
            for (int i = Pagination.LastPage.MinIndex; i <= Pagination.LastPage.MaxIndex; i++)
            {
                Items.Add(i);
            }
            if (!DroppedDown)
            {
                DroppedDown = true;
            }
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();

            if (e.Index < 0 || e.Index >= Items.Count)
            {
                return;
            }
            var item = Items[e.Index];
            String? text = item == null ? "(null)" : item.ToString();

            TextRenderer.DrawText(e.Graphics, text, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);

            base.OnDrawItem(e);
        }

        protected override void OnDropDown(EventArgs e)
        {
            base.OnDropDown(e);

            if (Pagination == null)
            {
                return;
            }

            Pagination.Margin = new Padding(0);

            ToolStripControlHost host = new ToolStripControlHost(Pagination);
            Pagination.MinimumSize = new Size(this.Width, 30);
            host.Padding = new Padding(0);

            ToolStripDropDown dropdown = new ToolStripDropDown();
            dropdown.Padding = new Padding(0);
            dropdown.Items.Add(host);
            if (this.Items.Count > 0)
            {
                dropdown.Show(this, 0, 3 + this.Height + (this.Items.Count * this.ItemHeight));
            }
        }

    }
}

产出

相关问题