C#winform自定义翻页控件详解-创新互联

C#  winform中自定义的翻页控件,自己设计,供大家参考,具体内容如下

成都创新互联公司主营雨花台网站建设的网络公司,主营网站建设方案,app开发定制,雨花台h5重庆小程序开发搭建,雨花台网站营销推广欢迎雨花台等地区企业咨询

1.主要是使用控件绑定点击事件  

C# winform自定义翻页控件详解

用到的控件分别为picturebox   lable  上一页pbPage_Prev    下一页 pbPage_Next  首页 pbPage_Begin   尾页pbPage_End  是picturebox控件加背景图 

“第  页/ 共  页” 是一个lable “labPageInfo”    在lable上面加了一个隐藏的textbox 控件 “txtPageInfo”

2.将这个翻页的功能单独写在用户控件 ucPageTurn 里面  然后在每个页面直接应用就可以了 

下面只是把ucPageTurn写了出来

还需要在winform页面上应用上

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
 public partial class ucPageTurn : UserControl, IMessageFilter
 {
  private const int SIZE_HEIGHT = 40;
  private const int SIZE_MINWIDTH = 84;
  private const int SIZE_INFO_MINWIDTH = 188;

  public ucPageTurn()
  {
   Application.AddMessageFilter(this); 

   InitializeComponent();
   this.BorderStyle = System.Windows.Forms.BorderStyle.None;
   this.MinimumSize = new Size(SIZE_MINWIDTH, 0);
   this.Disposed += new EventHandler(ucPages_Disposed);
   
   //this.MouseClick += new MouseEventHandler(ucKeyboard_Close);

   PageChanged += new PageChangedHandle(new PageChangedHandle((oldPage, newPage, e) => { }));
   InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 
   //InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));

   InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 
   InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));

   InputMouseDown += new InputMouseHandle(new InputMouseHandle((sender, e) => { }));
   InputMouseUp += new InputMouseHandle(new InputMouseHandle((sender, e) => { }));
   InputTextClick += new EventHandler(new EventHandler((sender, e) => { }));
   InputKeyDown += new InputKeyHandle(new InputKeyHandle((sender, e) => { }));
   InputKeyUp += new InputKeyHandle(new InputKeyHandle((sender, e) => { }));
   InputKeyPress += new InputKeyPressHandle(new InputKeyPressHandle((sender, e) => { }));
   InputTextChanged += new EventHandler(new EventHandler((sender, e) => { }));

   this.BackColor = Color.White;
   labPageInfo.BackColor = this.BackColor;

   this.Resize += new EventHandler(ucPages_Resize);

   //labPageInfo.MouseDoubleClick += new MouseEventHandler(labPageInfo_MouseDoubleClick);

   pbPage_Prev.MouseDown += new MouseEventHandler(pbPage_Prev_MouseDown);
   pbPage_Prev.MouseUp += new MouseEventHandler(pbPage_Prev_MouseUp);

   pbPage_Next.MouseDown += new MouseEventHandler(pbPage_Next_MouseDown);
   pbPage_Next.MouseUp += new MouseEventHandler(pbPage_Next_MouseUp);

   pbPage_Begin.MouseDown += new MouseEventHandler(pbPage_Begin_MouseDown);
   pbPage_Begin.MouseUp += new MouseEventHandler(pbPage_Begin_MouseUp);

   pbPage_End.MouseDown += new MouseEventHandler(pbPage_End_MouseDown);
   pbPage_End.MouseUp += new MouseEventHandler(pbPage_End_MouseUp);

   txtPageInfo.TextChanged += new EventHandler(txtPageInfo_TextChanged);
   txtPageInfo.GotFocus += new EventHandler(txtPageInfo_GotFocus);
   txtPageInfo.Click += new EventHandler(txtPageInfo_Click);
   txtPageInfo.Text = m_strText;
   txtPageInfo.Visible = m_blnShowTxtPageInfo;

   m_blnIsAutoJump = false;
   m_timerAutoPage.Enabled = false;
   m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP;
   m_timerAutoPage.Tick += new EventHandler(timerAutoPage_Tick);
  }

  private void ucPages_Load(object sender, EventArgs e)
  {
   setStatus();
  }

  private void ucPages_Disposed(object sender, EventArgs e)
  {
   Application.RemoveMessageFilter(this);
  }

  public bool PreFilterMessage(ref System.Windows.Forms.Message MyMessage)
  {
   if (MyMessage.Msg == 0x204 || MyMessage.Msg == 0x205)
   {
    return true;
   }
   return false;
  }

  //设置控件的自适应大小
  private void ucPages_Resize(object sender, EventArgs e)
  {
   this.Height = SIZE_HEIGHT;

   pbPage_Begin.Location = new Point(0, 0);
   pbPage_Begin.Size = new Size(SIZE_HEIGHT, SIZE_HEIGHT);

   pbPage_Prev.Location = new Point(pbPage_Begin.Width + 2, pbPage_Begin.Top);
   pbPage_Prev.Size = pbPage_Begin.Size;

   pbPage_End.Location = new Point(this.Width - pbPage_End.Width, pbPage_Begin.Top);
   pbPage_End.Size = pbPage_Begin.Size;

   pbPage_Next.Location = new Point(this.Width - pbPage_Next.Width - pbPage_End.Width - 2, pbPage_Begin.Top);
   pbPage_Next.Size = pbPage_Begin.Size;

   if (this.Width < SIZE_INFO_MINWIDTH)
   {
    labPageInfo.Visible = false;
    txtPageInfo.Visible = false;
   }
   else
   {
    labPageInfo.Location = new Point(pbPage_Prev.Width + pbPage_Prev.Width + 3, 2);
    labPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left - 3, pbPage_Prev.Height);
    
    txtPageInfo.Location = new Point(pbPage_Prev.Left + pbPage_Prev.Width + 5, 11);
    //txtPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left , 15);

    if (m_blnShowLabel && !labPageInfo.Visible) labPageInfo.Visible = true;
    if (m_blnShowLabel && !txtPageInfo.Visible) txtPageInfo.Visible = true;
   }

   if (m_blnShowTxtPageInfo)
   {
    txtPageInfo.Size = new Size(79, labPageInfo.Height);
   }
   else
   {
    txtPageInfo.Size = new Size(0, labPageInfo.Height);
   }
  }

  //点击lablelabPageInfo 显示txtPageInfo
  private void labPageInfo_Click(object sender, EventArgs e)
  {
   if (!txtPageInfo.Visible)
   {
    showJumpPageStatus(true);
    InputTextClick(txtPageInfo, new EventArgs());
   }
   else {
    showJumpPageStatus(false);
   }
  }

  public void showJumpPageStatus(Boolean isShow)
  {
   if (isShow)
   {
    txtPageInfo.Visible = true;
    txtPageInfo.Text = string.Empty;
    txtPageInfo.Focus();
   }
   else
   {
    txtPageInfo.Visible = false;
   }
  }

  //上一页
  private void pbPage_Prev_MouseDown(object sender, MouseEventArgs e)
  {
   pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_D;
   m_blnIsPrevDown = true;

   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_Prev_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_N;
   m_blnIsPrevDown = false;

   if (m_blnIsAutoJump)
   {
    leaveAutoJumpMode();
    return;
   }
   m_timerAutoPage.Enabled = false;

   if (1 == m_intCurPageIndex) return;
   int intOldPage = m_intCurPageIndex;
   m_intCurPageIndex--;
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  }

  //下一页
  private void pbPage_Next_MouseDown(object sender, MouseEventArgs e)
  {
  
   pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_D;
   m_blnIsNextDown = true;

   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_Next_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_N;
   m_blnIsNextDown = false;

   if (m_blnIsAutoJump)
   {
    leaveAutoJumpMode();
    return;
   }
   m_timerAutoPage.Enabled = false;

   if (m_intPageCount == m_intCurPageIndex) return;
   int intOldPage = m_intCurPageIndex;
   m_intCurPageIndex++;
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  }

  //首页
  private void pbPage_Begin_MouseDown(object sender, MouseEventArgs e)
  {
   
   pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_D;
   m_blnIsBeginDown = false;
   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_Begin_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_N;
   m_blnIsBeginDown = false;

   int intOldPage = m_intCurPageIndex;

   if (1 == m_intCurPageIndex) return;
   m_intCurPageIndex = 1;

   
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

   m_blnIsAutoJump = false;
   m_timerAutoPage.Stop();
  }

  //尾页
  private void pbPage_End_MouseDown(object sender, MouseEventArgs e)
  {
   
   pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_N;
   m_blnIsEndDown = true;

   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_End_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_D;
   m_blnIsEndDown = false;

   int intOldPage = m_intCurPageIndex;

   if (m_intCurPageIndex == m_intPageCount)return;
   m_intCurPageIndex = m_intPageCount;
   
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

   m_blnIsAutoJump = false;
   m_timerAutoPage.Stop();
   
  }
  
  //翻页按钮的状态
  private void setStatus()
  {
   //如果页数为0,翻页的按钮全部不显示
   if (m_intPageCount <= 0)
   {
    labPageInfo.Text = "";
    pbPage_Prev.Visible = false;
    pbPage_Next.Visible = false;
    pbPage_Begin.Visible = false;
    pbPage_End.Visible = false;
    txtPageInfo.Visible = false;
   }
   else
   {
    //如果页数为1,翻页的按钮不显示
    if (1 == m_intPageCount)
    {
     labPageInfo.Text = "";
     pbPage_Prev.Visible = false;
     pbPage_Next.Visible = false;
     pbPage_Begin.Visible = false;
     pbPage_End.Visible = false;
     txtPageInfo.Visible = false;
    }
    else
    {
     //只显示下一页和最后一页的按钮
     if (m_intCurPageIndex <= 1)
     {
      m_intCurPageIndex = 1;

      pbPage_Prev.Visible = false;
      pbPage_Next.Visible = true;
      pbPage_Begin.Visible = false;
      pbPage_End.Visible = true;
      txtPageInfo.Visible = false;

     }
     //只显示上一页和首页的按钮
     else if (m_intCurPageIndex >= m_intPageCount)
     {
      m_intCurPageIndex = m_intPageCount;

      pbPage_Prev.Visible = true;
      pbPage_Next.Visible = false;
      pbPage_Begin.Visible = true ;
      pbPage_End.Visible = false;
      txtPageInfo.Visible = false;

     }
      //否则按钮全部显示
     else
     {
      pbPage_Prev.Visible = true;
      pbPage_Next.Visible = true;
      pbPage_Begin.Visible = true;
      pbPage_End.Visible = true;
      txtPageInfo.Visible = false;
     }

     labPageInfo.Text = String.Format("第{0}页 /共{1}页", m_intCurPageIndex, m_intPageCount);
     txtPageInfo.Text = String.Format("{0}", m_intCurPageIndex);
    }
   }
  }

  private void timerAutoPage_Tick(object sender, EventArgs e)
  {
   if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1) || (m_blnIsEndDown && m_intCurPageIndex >=m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex<=1))
   {
    leaveAutoJumpMode();
    return;
   }

   if (!m_blnIsAutoJump) m_timerAutoPage.Interval = AUTOJUMP_INV;

   int intOldPage = m_intCurPageIndex;

   if (m_blnIsNextDown) m_intCurPageIndex++;
   if (m_blnIsPrevDown) m_intCurPageIndex--;

   setStatus();

   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

   if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1)||(m_blnIsEndDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex <= 1))
   {
    leaveAutoJumpMode();
   }
   else
   {
    m_blnIsAutoJump = true;
   }
  }

  private void leaveAutoJumpMode()
  {
   m_blnIsAutoJump = false;
   m_timerAutoPage.Stop();
   m_timerAutoPage.Enabled = false;
   m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP;
  }

  private int m_intCurPageIndex = 0;

  //当前页面
  public int CurPageIndex
  {
   get { return m_intCurPageIndex; }
   set
   {
    if (value < 0 || (m_intPageCount> 0 && value > m_intPageCount)) return;

    int intOldPage = m_intCurPageIndex;
    m_intCurPageIndex = value;

    setStatus();
    if (!m_blnIgnoreChange) PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

    m_blnIsAutoJump = false;
    m_timerAutoPage.Stop();
    m_timerAutoPage.Enabled = false;
   }
  }

  //计算总页数
  public int PageCount
  {
   get { return m_intPageCount; }
   set 
   { 
    m_intPageCount = value;

    if (m_intPageCount > 0)
    {
     if (m_intCurPageIndex <= 0) m_intCurPageIndex = 0;

     if (m_intCurPageIndex > m_intPageCount)
     {
      m_intCurPageIndex = m_intPageCount;
     }
    }
    else
    {
     m_intCurPageIndex = 0;
    }

    setStatus();
   }
  }

  private Boolean m_blnIgnoreChange = false;
  public Boolean IgnoreChange
  {
   get { return m_blnIgnoreChange; }
   set { m_blnIgnoreChange = value; }
  }

  private Boolean m_blnShowLabel = true;
  public Boolean ShowLabel
  {
   get { return m_blnShowLabel; }
   set { m_blnShowLabel = value; labPageInfo.Visible = value; }
  }

  private void txtPageInfo_TextChanged(object sender, EventArgs e)
  {
   if (m_blnIgnTextChange) return;
   m_blnIgnTextChange = true;
   InputTextChanged(sender, e);
  }

  private void txtPageInfo_GotFocus(object sender, EventArgs e)
  {
   InputGotFocus(this, e);
  }

  private void txtPageInfo_LostFocus(object sender, EventArgs e)
  {
   InputLostFocus(this, e);
  }

  private void txtPageInfo_MouseDown(object sender, MouseEventArgs e)
  {
   InputMouseDown(sender, e);
  }

  private void txtPageInfo_MouseUp(object sender, MouseEventArgs e)
  {
   InputMouseUp(sender, e);
  }

  private void txtPageInfo_Click(object sender, EventArgs e)
  {
   InputTextClick(sender, e);
  }

  private void txtPageInfo_KeyDown(object sender, KeyEventArgs e)
  {
   InputKeyDown(sender, e);
  }

  private void txtPageInfo_KeyUp(object sender, KeyEventArgs e)
  {
   InputKeyUp(sender, e);
  }

  private void txtPageInfo_KeyPress(object sender, KeyPressEventArgs e)
  {
   InputKeyPress(sender, e);
  }

  private string m_strText = "";
  public String Text
  {
   get { return m_strText; }
   set { m_strText = value; }
  }

  private System.Windows.Forms.Timer m_timerAutoPage = new Timer();
  private bool m_blnIsPrevDown = false;
  private bool m_blnIsNextDown = false;

  private bool m_blnIsBeginDown = false;
  private bool m_blnIsEndDown = false;

  private bool m_blnIsAutoJump = false;
  public delegate void PageChangedHandle(int oldPage, int newPage, EventArgs e);
  public event PageChangedHandle PageChanged;

  private const int WAIT_FOR_AUTOJUMP = 500;
  private const int AUTOJUMP_INV = 500;

  private int m_intPageCount = 0;
  protected String m_strErrorText = "";
  public String ErrorText
  {
   get { return m_strErrorText; }
   set { m_strErrorText = value; }
  }

  /// 
  /// 键盘控件的父对象
  /// 
  private Control m_keyboardParent = null;
  public Control KeyboardParent
  {
   get { return m_keyboardParent; }
   set { m_keyboardParent = value; }
  }
  
  /// 
  /// 是否显示输入翻页框按钮
  /// 
  private Boolean m_blnShowTxtPageInfo = true;
  public Boolean ShowTxtPageInfo
  {
   get { return m_blnShowTxtPageInfo; }
   set { m_blnShowTxtPageInfo = value; txtPageInfo.Visible = value; }
  }

  //public bool IsPages { get; set; }

  private bool m_blnIgnTextChange = false;
  private Boolean m_blnIsInputFocus = false;
  
  //public event EventHandler InputEnterPressed;
  //public event EventHandler InputClearPressed;

  public delegate void InputFocusHandle(object sender, EventArgs e);
  public event InputFocusHandle InputGotFocus;
  public event InputFocusHandle InputLostFocus;

  public delegate void InputMouseHandle(object sender, MouseEventArgs e);
  public event InputMouseHandle InputMouseDown;
  public event InputMouseHandle InputMouseUp;

  public event EventHandler InputTextClick;

  public delegate void InputKeyHandle(object sender, KeyEventArgs e);
  public event InputKeyHandle InputKeyDown;
  public event InputKeyHandle InputKeyUp;
  //public event InputKeyHandle InputTextKeyBoardEnter;

  public delegate void InputKeyPressHandle(object sender, KeyPressEventArgs e);
  public event InputKeyPressHandle InputKeyPress;

  public event EventHandler InputTextChanged;
  

  public TextBox InputTextBox
  {
   set { txtPageInfo=value; }
   get { return txtPageInfo; }
  }

  public String InputText
  {
   get
   {
    if (m_strText == txtPageInfo.Text || String.IsNullOrWhiteSpace(txtPageInfo.Text))
    {
     return String.Empty;
    }
    else
    {
     return txtPageInfo.Text;
    }
   }
   set
   {
    if (m_blnIsInputFocus)
    {
     txtPageInfo.Text = value;
    }
    else
    {
     if (String.IsNullOrWhiteSpace(value))
     {
      m_blnIgnTextChange = true;
      txtPageInfo.Text = m_strText;
      m_blnIgnTextChange = false;
     }
    }
   }
  }

  public void setInputText(String text)
  {
   txtPageInfo.Text = text;
  }
 }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


标题名称:C#winform自定义翻页控件详解-创新互联
标题链接:http://myzitong.com/article/cdoshi.html