C# winform自定義翻頁(yè)控件詳解
C# winform中自定義的翻頁(yè)控件,自己設(shè)計(jì),供大家參考,具體內(nèi)容如下
1.主要是使用控件綁定點(diǎn)擊事件
用到的控件分別為picturebox lable 上一頁(yè)pbPage_Prev 下一頁(yè) pbPage_Next 首頁(yè) pbPage_Begin 尾頁(yè)pbPage_End 是picturebox控件加背景圖
“第 頁(yè)/ 共 頁(yè)” 是一個(gè)lable “l(fā)abPageInfo” 在lable上面加了一個(gè)隱藏的textbox 控件 “txtPageInfo”
2.將這個(gè)翻頁(yè)的功能單獨(dú)寫(xiě)在用戶控件 ucPageTurn 里面 然后在每個(gè)頁(yè)面直接應(yīng)用就可以了
下面只是把ucPageTurn寫(xiě)了出來(lái)
還需要在winform頁(yè)面上應(yīng)用上
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; } //設(shè)置控件的自適應(yīng)大小 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); } } //點(diǎn)擊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; } } //上一頁(yè) 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()); } //下一頁(yè) 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()); } //首頁(yè) 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(); } //尾頁(yè) 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(); } //翻頁(yè)按鈕的狀態(tài) private void setStatus() { //如果頁(yè)數(shù)為0,翻頁(yè)的按鈕全部不顯示 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 { //如果頁(yè)數(shù)為1,翻頁(yè)的按鈕不顯示 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 { //只顯示下一頁(yè)和最后一頁(yè)的按鈕 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; } //只顯示上一頁(yè)和首頁(yè)的按鈕 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}頁(yè) /共{1}頁(yè)", 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; //當(dāng)前頁(yè)面 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; } } //計(jì)算總頁(yè)數(shù) 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> /// 鍵盤(pán)控件的父對(duì)象 /// </summary> private Control m_keyboardParent = null; public Control KeyboardParent { get { return m_keyboardParent; } set { m_keyboardParent = value; } } /// <summary> /// 是否顯示輸入翻頁(yè)框按鈕 /// </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. 你在資源管文件里面創(chuàng)建的control 文件夾里面,創(chuàng)建了一個(gè)用戶控件ucPageTurn ,那么在工具欄里面會(huì)自動(dòng)顯示你剛剛寫(xiě)的ucPageTurn組件,把它拖到頁(yè)面上 ,將這個(gè)組件的名字叫做ucPages,這樣ucpageturn里面的控件就可以進(jìn)行編輯
//在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; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C# WinForm遍歷窗體控件的3種方法
- C# Winform 實(shí)現(xiàn)控件自適應(yīng)父容器大小的示例代碼
- C# WinForm-Timer控件的使用
- c# Winform自定義控件-儀表盤(pán)功能
- C# WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能示例
- C# WinForm制作異形窗體與控件的方法
- C#中WinForm控件的拖動(dòng)和縮放的實(shí)現(xiàn)代碼
- C# WinForm控件對(duì)透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡(jiǎn)單解決方法
- C# Winform 子窗體訪問(wèn)父級(jí)窗體的控件和屬性
- c# Winform同一數(shù)據(jù)源多個(gè)控件保持同步
相關(guān)文章
C#中的Hangfire和Quartz.NET 任務(wù)調(diào)度的區(qū)別解析
Hangfire 和 Quartz.NET 是兩種常見(jiàn)的 C# 任務(wù)調(diào)度庫(kù),它們有不同的特點(diǎn)和使用場(chǎng)景,本文給大家介紹C#中的Hangfire和Quartz.NET 任務(wù)調(diào)度的區(qū)別,感興趣的朋友一起看看吧2024-08-08使用XmlSerializer序列化List對(duì)象成XML格式(list對(duì)象序列化)
這篇文章主要介紹了使用XmlSerializer序列化List對(duì)象成XML格式(list對(duì)象序列化),需要的朋友可以參考下2014-03-03C#三種方法獲取文件的Content-Type(MIME Type)
這篇文章介紹了C#獲取文件Content-Type(MIME Type)的三種方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01C#抓取網(wǎng)絡(luò)圖片保存到本地的實(shí)現(xiàn)方法
下面小編就為大家分享一篇C#抓取網(wǎng)絡(luò)圖片保存到本地的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01詳解如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#使用SQL DataReader訪問(wèn)數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例
今天小編就為大家分享一篇關(guān)于C#使用SQL DataReader訪問(wèn)數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10C#中Convert.ToDecimal()報(bào)錯(cuò)問(wèn)題的解決
這篇文章主要給大家介紹了關(guān)于C#中Convert.ToDecimal()報(bào)錯(cuò)問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08