⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 .net2003 C# 基于pda的winsock通讯
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;
using System.Data.SqlClient ;
using System.Threading;
namespace SmartDeviceApplication5
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		protected System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.TextBox tb_bh;
		private System.Windows.Forms.TextBox tb_name;
		private System.Windows.Forms.TextBox tb_pw;
		private System.Windows.Forms.Button bt_denglu;
		private System.Windows.Forms.Button bt_tc;
		private System.Windows.Forms.MainMenu mainMenu1;

		Client client;
		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.bt_denglu = new System.Windows.Forms.Button();
			this.tb_bh = new System.Windows.Forms.TextBox();
			this.bt_tc = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.tb_name = new System.Windows.Forms.TextBox();
			this.tb_pw = new System.Windows.Forms.TextBox();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			// 
			// bt_denglu
			// 
			this.bt_denglu.Location = new System.Drawing.Point(64, 192);
			this.bt_denglu.Size = new System.Drawing.Size(48, 24);
			this.bt_denglu.Text = "登 录";
			this.bt_denglu.Click += new System.EventHandler(this.bt_denglu_Click);
			// 
			// tb_bh
			// 
			this.tb_bh.Location = new System.Drawing.Point(128, 62);
			this.tb_bh.Size = new System.Drawing.Size(88, 21);
			this.tb_bh.Text = "";
			this.tb_bh.TextChanged += new System.EventHandler(this.tb_bh_TextChanged);
			// 
			// bt_tc
			// 
			this.bt_tc.Location = new System.Drawing.Point(128, 192);
			this.bt_tc.Size = new System.Drawing.Size(48, 24);
			this.bt_tc.Text = "退出";
			this.bt_tc.Click += new System.EventHandler(this.bt_tc_Click);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("黑体", 15F, System.Drawing.FontStyle.Bold);
			this.label1.ForeColor = System.Drawing.Color.DarkTurquoise;
			this.label1.Location = new System.Drawing.Point(24, 16);
			this.label1.Size = new System.Drawing.Size(200, 24);
			this.label1.Text = "成都车辆段车辆预检";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(24, 48);
			this.pictureBox1.Size = new System.Drawing.Size(56, 136);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular);
			this.label2.Location = new System.Drawing.Point(88, 64);
			this.label2.Size = new System.Drawing.Size(40, 16);
			this.label2.Text = "编号";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular);
			this.label3.Location = new System.Drawing.Point(88, 98);
			this.label3.Size = new System.Drawing.Size(48, 16);
			this.label3.Text = "姓名";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular);
			this.label4.Location = new System.Drawing.Point(88, 130);
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.Text = "密码";
			// 
			// tb_name
			// 
			this.tb_name.BackColor = System.Drawing.Color.LavenderBlush;
			this.tb_name.Location = new System.Drawing.Point(128, 96);
			this.tb_name.ReadOnly = true;
			this.tb_name.Size = new System.Drawing.Size(88, 21);
			this.tb_name.Text = "";
			// 
			// tb_pw
			// 
			this.tb_pw.Location = new System.Drawing.Point(128, 128);
			this.tb_pw.PasswordChar = '*';
			this.tb_pw.Size = new System.Drawing.Size(88, 21);
			this.tb_pw.Text = "";
			// 
			// menuItem1
			// 
			this.menuItem1.Enabled = false;
			this.menuItem1.Text = "";
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.menuItem1);
			// 
			// Form1
			// 
			this.BackColor = System.Drawing.Color.AliceBlue;
			this.ClientSize = new System.Drawing.Size(242, 272);
			this.Controls.Add(this.tb_pw);
			this.Controls.Add(this.tb_name);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.bt_tc);
			this.Controls.Add(this.tb_bh);
			this.Controls.Add(this.bt_denglu);
			this.Menu = this.mainMenu1;
			this.Text = "main";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		/// 
		//client 为winsock连接的客户端
		//  mypw 为判断密码的基础值
		// mypreff 为发送winsock请求的具体id
		string mypw ="falsefalsefalse";
		string mypreff ="sockf101";

		static void Main() 
		{
			Application.Run(new Form1());
		}
         

		private void Form1_Load(object sender, System.EventArgs e)
		{
			//winsock连接的建立
			client = new  Client();
			client.StartClient(System.Net.IPAddress.Parse("192.168.1.178"),1001);
			// readuser() 从客户端读取默认用户
			readuser();
			//winsock连接付给全局变量,使得其他form可以共享它
			pubics.clients = client;
			client.MessageReceived += new SocketBase.MessageEventHandler(_server_MessageReceived);
		}


		private void _server_MessageReceived(object sender, SocketBase.MessageEventArgs e) 
		{
			string[] ls_reseve  = new string[2];
			string ls_preff;
			int    li_end;
			//接收信息 先判断前缀是否为用户信息,用户信息的第一个参数为 用户名称,第二个参数为 密码
			ls_preff = e.Message.MessageBody.Substring(0,4);
			if(ls_preff == mypreff)
			{
				li_end = e.Message.MessageBody.IndexOf(mypreff + "end");
				if (e.Message.MessageBody.Substring(4,li_end - 4) =="norows")
				{
					MessageBox.Show("没有该用户!");
					return;
				}
				ls_reseve= e.Message.MessageBody.Substring(4,li_end - 4).Split(new char[] {','});
				tb_name.Text = ls_reseve[0];
				mypw = ls_reseve[1];
			}
		}

	
		private void readuser()
		{
			try 
			{  
				string strSql = "select bh ,name,pw from buser";
				SqlCeConnection conn;
				conn = new SqlCeConnection (@"Data Source = \windows\程序\SmartD\yj.sdf");
				conn.Open();
				SqlCeCommand cmd =   new SqlCeCommand(strSql, conn); 
				SqlCeDataReader rdr = cmd.ExecuteReader();
				if (rdr.Read())
				{
					tb_bh.Text = rdr[0].ToString();
					tb_name.Text = rdr[1].ToString();
					mypw = rdr[2].ToString();
				}
				conn.Close();
			}
			catch (SqlCeException ee) 
			{
				MessageBox.Show(ee.Message.ToString());
			}
		}
		private void senduser(string as_bh)
		{
		  //从服务器上读取用户信息
			string strSql =mypreff +  "select sys_name,sys_code from b_dict_caiz_gh where sys_code ='"+ as_bh +"'";
			client.StartClient(System.Net.IPAddress.Parse("192.168.1.178"),1001);
			Message message = new Message(strSql);
			client.messageQueue.Add(message);	
		}

		
		private void updateuser()
		{
			//把用户信息保存到本地库
			try 
			{  
				string strSql = "update buser set bh ='"+ tb_bh.Text.Trim() +"' ,name ='"+ tb_name.Text.Trim() +"' ,pw ='"+ tb_pw.Text.Trim() +"'";
				SqlCeConnection conn;
				conn = new SqlCeConnection (@"Data Source = \windows\程序\SmartD\yj.sdf");
				conn.Open();
				SqlCeCommand cmd =   new SqlCeCommand(strSql, conn); 
				cmd.ExecuteNonQuery();
				conn.Close();
			}
			catch (SqlCeException ee) 
			{
				MessageBox.Show(ee.Message.ToString());
			}
		}

		private void tb_bh_TextChanged(object sender, System.EventArgs e)
		{
			senduser(tb_bh.Text.Trim());
		}

		private void bt_denglu_Click(object sender, System.EventArgs e)
		{
			Thread.Sleep(500);
			if (mypw == tb_pw.Text.Trim())
			{
				updateuser();
				pubics.gs_userid = tb_bh.Text.Trim();
				pubics.gs_user   = tb_name.Text.Trim();
				Form2 Form2 =new Form2();
				Form2.ShowDialog();
			}
			else
				MessageBox.Show("密码错误!"); 
		
		}

		private void bt_tc_Click(object sender, System.EventArgs e)
		{
			client.EndListenAndSend();
			Application.Exit();
			return ;
		
		}


		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -