您好,登錄后才能下訂單哦!
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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。