首页 > 开发 > 综合 > 正文

登陆框控件

2024-07-21 02:24:11
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 好久以前作的东西,最近拿出来溜溜,最近一直没有作应用程序方面的东西了,有点生疏! 希望我的这篇东西能对您有所帮助,大家共同探讨共同进步,此文章比较简单!是作一个通用的登陆框控件!

    首先建一解决项目logincontrol,添加一个类库的项目logincontrol,然后添加用户控件ctllogin



    然后在ctllogin.cs文件中写代码

    //ctllogin.cs

    using system;
    using system.collections;
    using system.componentmodel;
    using system.drawing;
    using system.data;
    using system.windows.forms;
    using system.text;

    namespace logincontrol
    {
    ///
    /// ctllogin 的摘要说明。
    ///
    public class ctllogin : system.windows.forms.usercontrol
    {
    private system.windows.forms.panel panel1;
    private system.windows.forms.label lheader;
    private system.windows.forms.label ltext;
    private system.windows.forms.button btnlogin;
    private system.windows.forms.label lpassword;
    private system.windows.forms.label lusername;
    private system.windows.forms.textbox txtpassword;
    private system.windows.forms.textbox txtusername;
    private system.windows.forms.picturebox picturebox1;
    private system.windows.forms.errorprovider errorprovider;
    ///
    /// 必需的设计器变量。
    ///
    private system.componentmodel.container components = null;

    public event system.eventhandler successful;
    public event system.eventhandler failed;

    private arraylist strusername=new arraylist();
    private arraylist strpassword=new arraylist();
    private int index=-1;

    private string errmsg = "请输入正确的用户名和密码!";

    public ctllogin()
    {
    // 该调用是 windows.forms 窗体设计器所必需的。
    initializecomponent();

    // todo: 在 initializecomponent 调用后添加任何初始化
    strusername.clear();
    strpassword.clear();
    }

    ///
    /// 清理所有正在使用的资源。
    ///
    protected override void dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.dispose();
    }
    }
    base.dispose( disposing );
    }

    #region 组件设计器生成的代码
    ///
    /// 设计器支持所需的方法 - 不要使用代码编辑器
    /// 修改此方法的内容。
    ///
    private void initializecomponent()
    {
    system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(ctllogin));
    this.panel1 = new system.windows.forms.panel();
    this.picturebox1 = new system.windows.forms.picturebox();
    this.ltext = new system.windows.forms.label();
    this.lheader = new system.windows.forms.label();
    this.btnlogin = new system.windows.forms.button();
    this.lpassword = new system.windows.forms.label();
    this.lusername = new system.windows.forms.label();
    this.txtpassword = new system.windows.forms.textbox();
    this.txtusername = new system.windows.forms.textbox();
    this.errorprovider = new system.windows.forms.errorprovider();
    this.panel1.suspendlayout();
    this.suspendlayout();
    //
    // panel1
    //
    this.panel1.backcolor = system.drawing.systemcolors.activecaptiontext;
    this.panel1.controls.add(this.picturebox1);
    this.panel1.controls.add(this.ltext);
    this.panel1.controls.add(this.lheader);
    this.panel1.location = new system.drawing.point(4, 4);
    this.panel1.name = "panel1";
    this.panel1.size = new system.drawing.size(480, 52);
    this.panel1.tabindex = 0;
    //
    // picturebox1
    //
    this.picturebox1.image = ((system.drawing.image)(resources.getobject("picturebox1.image")));
    this.picturebox1.location = new system.drawing.point(432, 12);
    this.picturebox1.name = "picturebox1";
    this.picturebox1.size = new system.drawing.size(40, 32);
    this.picturebox1.tabindex = 6;
    this.picturebox1.tabstop = false;
    //
    // ltext
    //
    this.ltext.location = new system.drawing.point(36, 24);
    this.ltext.name = "ltext";
    this.ltext.size = new system.drawing.size(360, 20);
    this.ltext.tabindex = 5;
    this.ltext.text = "请输入您的用户名和密码.";
    this.ltext.textalign = system.drawing.contentalignment.middleleft;
    //
    // lheader
    //
    this.lheader.font = new system.drawing.font("tahoma", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(0)));
    this.lheader.location = new system.drawing.point(12, 4);
    this.lheader.name = "lheader";
    this.lheader.size = new system.drawing.size(384, 20);
    this.lheader.tabindex = 4;
    this.lheader.text = "登陆";
    this.lheader.textalign = system.drawing.contentalignment.middleleft;
    //
    // btnlogin
    //
    this.btnlogin.flatstyle = system.windows.forms.flatstyle.popup;
    this.btnlogin.font = new system.drawing.font("tahoma", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(0)));
    this.btnlogin.location = new system.drawing.point(376, 292);
    this.btnlogin.name = "btnlogin";
    this.btnlogin.size = new system.drawing.size(70, 20);
    this.btnlogin.tabindex = 14;
    this.btnlogin.text = "登 陆";
    this.btnlogin.click += new system.eventhandler(this.btnlogin_click);
    //
    // lpassword
    //
    this.lpassword.font = new system.drawing.font("tahoma", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(0)));
    this.lpassword.location = new system.drawing.point(76, 252);
    this.lpassword.name = "lpassword";
    this.lpassword.size = new system.drawing.size(80, 20);
    this.lpassword.tabindex = 16;
    this.lpassword.text = "密 码";
    this.lpassword.textalign = system.drawing.contentalignment.middleright;
    //
    // lusername
    //
    this.lusername.font = new system.drawing.font("tahoma", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(0)));
    this.lusername.location = new system.drawing.point(76, 212);
    this.lusername.name = "lusername";
    this.lusername.size = new system.drawing.size(80, 20);
    this.lusername.tabindex = 15;
    this.lusername.text = "用户名";
    this.lusername.textalign = system.drawing.contentalignment.middleright;
    //
    // txtpassword
    //
    this.txtpassword.borderstyle = system.windows.forms.borderstyle.fixedsingle;
    this.txtpassword.location = new system.drawing.point(176, 252);
    this.txtpassword.name = "txtpassword";
    this.txtpassword.passwordchar = '*';
    this.txtpassword.size = new system.drawing.size(270, 21);
    this.txtpassword.tabindex = 13;
    this.txtpassword.text = "";
    this.txtpassword.keypress += new system.windows.forms.keypresseventhandler(this.txtpassword_keypress);
    this.txtpassword.leave += new system.eventhandler(this.control_leave);
    this.txtpassword.enter += new system.eventhandler(this.control_enter);
    //
    // txtusername
    //
    this.txtusername.borderstyle = system.windows.forms.borderstyle.fixedsingle;
    this.txtusername.location = new system.drawing.point(176, 212);
    this.txtusername.name = "txtusername";
    this.txtusername.size = new system.drawing.size(270, 21);
    this.txtusername.tabindex = 12;
    this.txtusername.text = "";
    this.txtusername.keypress += new system.windows.forms.keypresseventhandler(this.txtusername_keypress);
    this.txtusername.leave += new system.eventhandler(this.control_leave);
    this.txtusername.enter += new system.eventhandler(this.control_enter);
    //
    // errorprovider
    //
    this.errorprovider.containercontrol = this;
    //
    // ctllogin
    //
    this.controls.add(this.btnlogin);
    this.controls.add(this.lpassword);
    this.controls.add(this.lusername);
    this.controls.add(this.txtpassword);
    this.controls.add(this.txtusername);
    this.controls.add(this.panel1);
    this.name = "ctllogin";
    this.size = new system.drawing.size(484, 348);
    this.load += new system.eventhandler(this.ctllogin_load);
    this.panel1.resumelayout(false);
    this.resumelayout(false);

    }
    #endregion

    private void ctllogin_load(object sender, system.eventargs e)
    {
    // strusername.add(txtusername.text.trim());
    // strpassword.add(txtpassword.text.trim());
    }

    private void txtpassword_keypress(object sender, system.windows.forms.keypresseventargs e)
    {
    if(e.keychar==(char)13)
    {
    btnlogin_click(sender,new eventargs());
    }
    }

    private void btnlogin_click(object sender, system.eventargs e)
    {
    bool b = checkpassword(username, password);
    if (b)
    {
    successful(this, new system.eventargs());
    }
    else
    {
    failed(this, new system.eventargs());
    }
    }

    ///
    /// 判断用户名和密码是否正确
    ///
    /// 所有用户
    /// 所有用户密码
    ///
    private bool checkpassword(arraylist aluser,arraylist alpwd)
    {
    string struser="";
    string strpwd="";
    if(aluser.count>0 && alpwd.count>0)
    {
    for(int i=0;i {
    struser=aluser[i].tostring();
    strpwd=alpwd[i].tostring().trim();
    if(struser.trim()==txtusername.text.trim() && strpwd.trim()==txtpassword.text.trim())
    {
    index_username=i;
    errorprovider.dispose();
    return true;
    }
    if(i == aluser.count-1)
    {
    errorprovider.seterror(btnlogin, errmsg);
    return false;
    }
    }
    }
    return false;
    }

    private void control_enter(object sender, system.eventargs e)
    {
    control ctl=(control)sender;
    ctl.backcolor=color.lemonchiffon;
    }

    private void control_leave(object sender, system.eventargs e)
    {
    control ctl=(control)sender;
    ctl.backcolor=color.white;
    }

    private void txtusername_keypress(object sender, system.windows.forms.keypresseventargs e)
    {
    if(e.keychar==(char)13)
    {
    txtpassword.focus();
    }
    }


    ///
    /// 所有用户
    ///
    public arraylist username
    {
    get
    {
    return strusername;
    }
    set
    {
    strusername = value;
    }

    }


    ///
    /// 所有用户密码
    ///
    public arraylist password
    {
    get
    {
    return strpassword;
    }
    set
    {
    strpassword = value;
    }
    }

    ///
    /// 在用户数组username中的索引
    ///
    public int index_username
    {
    get
    {
    return index;
    }
    set
    {
    index=value;
    }
    }

    ///
    /// 错误信息
    ///
    public string errormessage
    {
    get
    {
    return errmsg;
    }
    set
    {
    errmsg = value;
    }
    }
    }
    }




    接着,您就可以编译成logincontrol.dll

    再在解决项目中添加一个测试项目test,然后在工具箱中添加/移除项中添加刚才编译的logincontrol.dll,添加一测试界面为frmtest,然后在工具箱中把ctllogin控件拖到frmtest页面中

    然后在frmtest.cs中来测试刚才的控件

    //frmtest.cs

    using system;
    using system.drawing;
    using system.collections;
    using system.componentmodel;
    using system.windows.forms;
    using system.data;
    using informationclass; //自己写的一个测试类

    namespace test
    {
    ///
    /// form1 的摘要说明。
    ///
    public class frmtest : system.windows.forms.form
    {
    private logincontrol.ctllogin ctllogin;
    ///
    /// 必需的设计器变量。
    ///
    private system.componentmodel.container components = null;

    private users objusers=new users(); //实例化自定义的登陆用户集合类
    private user m_objuser=new user(); //实例化自定义的登陆用户类

    public frmtest()
    {
    //
    // windows 窗体设计器支持所必需的
    //
    initializecomponent();

    //
    // todo: 在 initializecomponent 调用后添加任何构造函数代码
    //
    }

    ///
    /// 清理所有正在使用的资源。
    ///
    protected override void dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.dispose();
    }
    }
    base.dispose( disposing );
    }

    #region windows 窗体设计器生成的代码
    ///
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    ///
    private void initializecomponent()
    {
    system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(frmtest));
    this.ctllogin = new logincontrol.ctllogin();
    this.suspendlayout();
    //
    // ctllogin
    //
    this.ctllogin.errormessage = "请输入正确的用户名和密码!";
    this.ctllogin.index_username = -1;
    this.ctllogin.location = new system.drawing.point(0, 0);
    this.ctllogin.name = "ctllogin";
    this.ctllogin.size = new system.drawing.size(484, 348);
    this.ctllogin.tabindex = 0;
    this.ctllogin.failed += new system.eventhandler(this.ctllogin_failed);
    this.ctllogin.load += new system.eventhandler(this.form1_load);
    this.ctllogin.successful += new system.eventhandler(this.ctllogin_successful);
    //
    // frmtest
    //
    this.autoscalebasesize = new system.drawing.size(6, 14);
    this.clientsize = new system.drawing.size(484, 345);
    this.controls.add(this.ctllogin);
    this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle;
    this.icon = ((system.drawing.icon)(resources.getobject("$this.icon")));
    this.maximizebox = false;
    this.minimizebox = false;
    this.name = "frmtest";
    this.text = "系统登陆";
    this.load += new system.eventhandler(this.form1_load);
    this.resumelayout(false);

    }
    #endregion

    ///
    /// 应用程序的主入口点。
    ///
    [stathread]
    static void main()
    {
    application.run(new frmtest());
    }

    private void ctllogin_failed(object sender, system.eventargs e)
    {
    messagebox.show("登陆失败!请输入正确的用户名和密码!!","信息提示",messageboxbuttons.ok,messageboxicon.error);
    }

    private void ctllogin_successful(object sender, system.eventargs e)
    {
    m_objuser=objusers[ctllogin.index_username];
    messagebox.show("登陆成功!","信息提示",messageboxbuttons.ok,messageboxicon.information);
    }

    private void form1_load(object sender, system.eventargs e)
    {
    objusers=new readdata().getusers();
    if(objusers.count>0)
    {
    foreach(user objuser in objusers)
    {
    ctllogin.username.add(objuser.name);
    ctllogin.password.add(objuser.password);
    }
    }
    }
    }
    }



    运行程序

    如果登陆失败就会出现





    如果登陆成功





    就这样,小小的一个登陆控件就作好了,希望我的这篇文章能对于刚学没有多久的.net程序员有所帮助
    如果有什么不足的地方,希望您能指正,谢谢



    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表