国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

登陸框控件

2024-07-21 02:24:11
字體:
供稿:網(wǎng)友
  • 本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
  • 好久以前作的東西,最近拿出來溜溜,最近一直沒有作應(yīng)用程序方面的東西了,有點生疏! 希望我的這篇東西能對您有所幫助,大家共同探討共同進步,此文章比較簡單!是作一個通用的登陸框控件!

    首先建一解決項目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;
    ///
    /// 必需的設(shè)計器變量。
    ///
    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()
    {
    // 該調(diào)用是 windows.forms 窗體設(shè)計器所必需的。
    initializecomponent();

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

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

    #region 組件設(shè)計器生成的代碼
    ///
    /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器
    /// 修改此方法的內(nèi)容。
    ///
    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;
    }
    }

    ///
    /// 在用戶數(shù)組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;
    ///
    /// 必需的設(shè)計器變量。
    ///
    private system.componentmodel.container components = null;

    private users objusers=new users(); //實例化自定義的登陸用戶集合類
    private user m_objuser=new user(); //實例化自定義的登陸用戶類

    public frmtest()
    {
    //
    // windows 窗體設(shè)計器支持所必需的
    //
    initializecomponent();

    //
    // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
    //
    }

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

    #region windows 窗體設(shè)計器生成的代碼
    ///
    /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
    /// 此方法的內(nèi)容。
    ///
    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 = "系統(tǒng)登陸";
    this.load += new system.eventhandler(this.form1_load);
    this.resumelayout(false);

    }
    #endregion

    ///
    /// 應(yīng)用程序的主入口點。
    ///
    [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);
    }
    }
    }
    }
    }



    運行程序

    如果登陸失敗就會出現(xiàn)





    如果登陸成功





    就這樣,小小的一個登陸控件就作好了,希望我的這篇文章能對于剛學(xué)沒有多久的.net程序員有所幫助
    如果有什么不足的地方,希望您能指正,謝謝



    發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 上栗县| 呼和浩特市| 肇东市| 昔阳县| 宕昌县| 阜南县| 岚皋县| 乐昌市| 渑池县| 忻城县| 衡东县| 平潭县| 龙井市| 恩施市| 凤山县| 通州市| 清镇市| 辉南县| 汝南县| 吴忠市| 利津县| 祁阳县| 莱阳市| 姜堰市| 体育| 望江县| 涡阳县| 华安县| 阿拉善右旗| 沅陵县| 巴彦淖尔市| 兴城市| 遂溪县| 察哈| 阆中市| 平山县| 沙坪坝区| 门源| 家居| 沙坪坝区| 胶南市|