欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-11 18:17:40
c#  winform中自定义的翻页控件,自己设计,供大家参考,具体内容如下 1.主要是使用控件绑定点击事件   用到的控件分别为pi...

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

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; }
  }

  /// <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;
   }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: