C# winform自定义翻页控件详解
更新时间:2020年6月25日 11:18 点击:2219
C# winform中自定义的翻页控件,自己设计,供大家参考,具体内容如下
1.主要是使用控件绑定点击事件
用到的控件分别为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; } } /// <summary> /// 键盘控件的父对象 /// </summary> private Control m_keyboardParent = null; public Control KeyboardParent { get { return m_keyboardParent; } set { m_keyboardParent = value; } } /// <summary> /// 是否显示输入翻页框按钮 /// </summary> 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; } } }
3. 你在资源管文件里面创建的control 文件夹里面,创建了一个用户控件ucPageTurn ,那么在工具栏里面会自动显示你刚刚写的ucPageTurn组件,把它拖到页面上 ,将这个组件的名字叫做ucPages,这样ucpageturn里面的控件就可以进行编辑
//在initControls里面添加textbox输入框的事件 private void initControls() { ucPages.InputTextBox.KeyPress +=new KeyPressEventHandler(ucPages_KeyPress); ucPages.PageChanged += new Pku.CFM.Controls.ucPageTurn.PageChangedHandle(ucPages_PageChanged); } private void ucPages_KeyPress(object sender, KeyPressEventArgs e) { if (13 == e.KeyChar) { int intReturn = SysDefine.NOTHING; String strInputText = ucPages.Text.ToUpper(); if (SysDefine.FAILED == intReturn) { MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); } String pageInputText = ucPages.InputText; int page = 0; try { page = int.Parse(pageInputText); } catch { page = 1; } ucPages.CurPageIndex = page; if (SysDefine.FAILED == refreshList(page, strInputText)) { MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void ucPages_PageChanged(int oldPage, int newPage, EventArgs e) { int intReturn = SysDefine.NOTHING; String strInputText = ucPages.Text.ToUpper(); intReturn = refreshList(newPage, strInputText); if (SysDefine.FAILED == intReturn) { MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25