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

首頁 > 開發 > 綜合 > 正文

C#下WinForm編程:登錄窗體的設計

2024-07-21 02:27:02
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。

  • 我在csdn里搜索了很久,也沒有找到符合我要求的login文檔,我這次把自己的心得和自己做的成果拿出來和大家分享一下,希望對后來的人能有一些幫助。我初次做,可能代碼寫的不是很規范,思路也不是很清晰,但是它能達到我要的效果就行了'      希望哪位兄弟幫忙完善一下我的代碼。

    我在數據庫里有一個   users  的表,如下:

    id     username           userpasswd

    1        admin                    admin

    2        user                        user

    3        guest                     guest

    我準備這樣做,先判斷輸入的用戶名是否和表里的username相同,如果相同,再比較相同username下的userpasswd,如果這些都正確了,就可以進入系統了。


    全部代碼如下(我把我寫的部分用黑體):

    form1的代碼:

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

    namespace login
    {
     /// <summary>
     /// form1 的摘要說明。
     /// </summary>
     public class form1 : system.windows.forms.form
     {
      private system.windows.forms.label label1;
      private system.windows.forms.label label2;
      private system.windows.forms.textbox txtuser;
      private system.windows.forms.textbox txtpasswd;
      private system.windows.forms.button btnok;
      private system.windows.forms.button btncancel;
      /// <summary>
      /// 必需的設計器變量。
      /// </summary>
      private system.componentmodel.container components = null;

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

      
       //
       // todo: 在 initializecomponent 調用后添加任何構造函數代碼
       //
      }

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

      #region windows 窗體設計器生成的代碼
      /// <summary>
      /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
      /// 此方法的內容。
      /// </summary>
      private void initializecomponent()
      {
       this.label1 = new system.windows.forms.label();
       this.label2 = new system.windows.forms.label();
       this.txtuser = new system.windows.forms.textbox();
       this.txtpasswd = new system.windows.forms.textbox();
       this.btnok = new system.windows.forms.button();
       this.btncancel = new system.windows.forms.button();
       this.suspendlayout();
       //
       // label1
       //
       this.label1.font = new system.drawing.font("宋體", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
       this.label1.location = new system.drawing.point(40, 24);
       this.label1.name = "label1";
       this.label1.size = new system.drawing.size(64, 23);
       this.label1.tabindex = 0;
       this.label1.text = "用戶名:";
       //
       // label2
       //
       this.label2.font = new system.drawing.font("宋體", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
       this.label2.location = new system.drawing.point(40, 72);
       this.label2.name = "label2";
       this.label2.size = new system.drawing.size(56, 23);
       this.label2.tabindex = 1;
       this.label2.text = "密碼:";
       //
       // txtuser
       //
       this.txtuser.location = new system.drawing.point(152, 24);
       this.txtuser.name = "txtuser";
       this.txtuser.tabindex = 2;
       this.txtuser.text = "";
       //
       // txtpasswd
       //
       this.txtpasswd.location = new system.drawing.point(152, 72);
       this.txtpasswd.name = "txtpasswd";
       this.txtpasswd.passwordchar = '*';
       this.txtpasswd.tabindex = 3;
       this.txtpasswd.text = "";
       //
       // btnok
       //
       this.btnok.location = new system.drawing.point(40, 120);
       this.btnok.name = "btnok";
       this.btnok.size = new system.drawing.size(72, 23);
       this.btnok.tabindex = 4;
       this.btnok.text = "ok";
       this.btnok.click += new system.eventhandler(this.btnok_click);
       //
       // btncancel
       //
       this.btncancel.dialogresult = system.windows.forms.dialogresult.cancel;
       this.btncancel.location = new system.drawing.point(176, 120);
       this.btncancel.name = "btncancel";
       this.btncancel.tabindex = 5;
       this.btncancel.text = "cancel";
       this.btncancel.click += new system.eventhandler(this.btncancel_click);
       //
       // form1
       //
       this.acceptbutton = this.btnok;
       this.autoscalebasesize = new system.drawing.size(6, 14);
       this.cancelbutton = this.btncancel;
       this.clientsize = new system.drawing.size(298, 167);
       this.controls.add(this.btncancel);
       this.controls.add(this.btnok);
       this.controls.add(this.txtpasswd);
       this.controls.add(this.txtuser);
       this.controls.add(this.label2);
       this.controls.add(this.label1);
       this.formborderstyle = system.windows.forms.formborderstyle.fixeddialog;
       this.maximizebox = false;
       this.maximumsize = new system.drawing.size(304, 192);
       this.minimizebox = false;
       this.minimumsize = new system.drawing.size(304, 192);
       this.name = "form1";
       this.showintaskbar = false;
       this.text = "login";
       this.load += new system.eventhandler(this.form1_load);
       this.resumelayout(false);

      }
      #endregion

      /// <summary>
      /// 應用程序的主入口點。
      /// </summary>

      private void btncancel_click(object sender, system.eventargs e)
      {
       application.exit();
      }

      private void form1_load(object sender, system.eventargs e)
      {
       this.setdesktoplocation(280,180);

      
      }

      private void btnok_click(object sender, system.eventargs e)
      {
      
       form2 theowner = (form2)this.owner;

       if(this.txtuser.text == "")
       {
        messagebox.show("用戶名不能為空!","錯誤");
       }
       else if(this.txtpasswd.text == "")
       {
        messagebox.show("密碼不能為空!","錯誤");
       }
       else
       {
        if(isuser(this.txtuser.text))
        {
         if(this.txtpasswd.text == loginuser(this.txtuser.text))
         {
          this.dialogresult = dialogresult.ok;
          theowner.getusername = this.txtuser.text;
         }
         else
         {
          messagebox.show("用戶名或密碼錯誤!");
         }
        }
        else
        {
         messagebox.show("用戶名或密碼錯誤!");
        }
       }
      
      
      }
      private string loginuser(string user)
      {
       sqlconnection conn = new sqlconnection("xxxxxxxx");
       sqlcommand cmd = new sqlcommand();
       cmd.commandtype = commandtype.storedprocedure;
       cmd.commandtext = "login";
       cmd.connection = conn;
       conn.open();
      
       sqlparameter parname = new sqlparameter("@name",sqldbtype.varchar,50);
       parname.value = user;
       cmd.parameters.add(parname);

       cmd.executenonquery();
      
       conn.close();
       sqldataadapter da = new sqldataadapter();
       da.selectcommand = cmd;
       dataset ds = new dataset();
       da.fill(ds);

       return ds.tables[0].rows[0]["userpasswd"].tostring();
      }
      private bool isuser(string user)
      {
       sqlconnection conn = new sqlconnection("xxxxxxxx");
       sqlcommand cmd = new sqlcommand();
       cmd.commandtype = commandtype.storedprocedure;
       cmd.commandtext = "isuser";
       cmd.connection = conn;
       conn.open();
      
       sqlparameter parname = new sqlparameter("@username",sqldbtype.varchar,50);
       parname.value = user;
       cmd.parameters.add(parname);

       cmd.executenonquery();
      
       conn.close();
       sqldataadapter da = new sqldataadapter();
       da.selectcommand = cmd;
       dataset ds = new dataset();
       da.fill(ds);

       int n;
       n = ds.tables[0].rows.count;
       if(n > 0)
        return true;
       else
        return false;
      }
     }
    }

     


    在form2中我設計了一個label ,讓它接受從form1傳過來的username,這樣我們在以后的設計中好判斷用戶的權限的大小。

    form2的代碼:

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

    namespace login
    {
     /// <summary>
     /// form2 的摘要說明。
     /// </summary>
     public class form2 : system.windows.forms.form
     {
      private string username;
      private system.windows.forms.label label1;
      /// <summary>
      /// 必需的設計器變量。
      /// </summary>
      private system.componentmodel.container components = null;

      public form2()
      {
       //
       // windows 窗體設計器支持所必需的
       //
       initializecomponent();
       form1 myform = new form1();
       myform.showdialog(this);

      

       //
       // todo: 在 initializecomponent 調用后添加任何構造函數代碼
       //
      }

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

      #region windows 窗體設計器生成的代碼
      /// <summary>
      /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
      /// 此方法的內容。
      /// </summary>
      private void initializecomponent()
      {
       this.label1 = new system.windows.forms.label();
       this.suspendlayout();
       //
       // label1
       //
       this.label1.location = new system.drawing.point(128, 72);
       this.label1.name = "label1";
       this.label1.size = new system.drawing.size(152, 40);
       this.label1.tabindex = 0;
       //
       // form2
       //
       this.autoscalebasesize = new system.drawing.size(6, 14);
       this.clientsize = new system.drawing.size(520, 357);
       this.controls.add(this.label1);
       this.name = "form2";
       this.text = "form2";
       this.load += new system.eventhandler(this.form2_load);
       this.resumelayout(false);

      }
      #endregion
      [stathread]
      static void main()
      {
       application.run(new form2());
      }

      private void form2_load(object sender, system.eventargs e)
      {
       this.setdesktoplocation(200,180);
       this.label1.text = username;
      }
      public string getusername
      {
       get
       {
        return username;
       }
       set
       {
        username = value;
       }
      }
     }
    }

     


     

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 获嘉县| 赤峰市| 利川市| 合江县| 开远市| 灯塔市| 尚志市| 苍山县| 平远县| 宜都市| 舟山市| 和林格尔县| 东丰县| 百色市| 普陀区| 潼关县| 漳浦县| 石景山区| 清远市| 延边| 鄂伦春自治旗| 洞头县| 辽宁省| 任丘市| 嘉兴市| 保定市| 彰武县| 靖边县| 渝北区| 双鸭山市| 宜黄县| 安乡县| 杭锦后旗| 岢岚县| 平南县| 新龙县| 五台县| 阿瓦提县| 寿阳县| 独山县| 鹿邑县|