2021/12/7
写了个简单聊天软件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StudentManage
{
public partial class main2 : Form
{
private string Sid;
public main2(string id)
{
Sid = id;
InitializeComponent();
}
//注销用户
private void cancel_Click(object sender, EventArgs e)
{
string strSQL = "DELETE FROM User1 WHERE id = '" + Sid + "'";
int count = SQLHelper.CommandSQL(strSQL);
if (count == 1)
{
MessageBox.Show("注销成功","消息");
Application.Exit();
}
else
{
MessageBox.Show("注销失败","消息");
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void statusUser_Load(object sender, EventArgs e)
{
String name;
string strSQL = "SELECT UserName FROM User1 WHERE id = '" + Sid + "'";
DataTable dt = SQLHelper.GetDataTable(strSQL);
name = dt.Rows[0]["UserName"].ToString();
this.statusUser.Text = "当前用户名:" + name;
}
private void statusTime_Click(object sender, EventArgs e)
{
this.statusTime.Text = "当前时间:" + DateTime.Now.ToString();
}
private void menuBaseManage_Click(object sender, EventArgs e)
{
string SQL = "SELECT ID,UserNAME FROM User1 WHERE ID<>" + Sid;
DataTable dt = SQLHelper.GetDataTable(SQL);
dataGridView1.DataSource = dt;
}
private void menuStockManage_Click(object sender, EventArgs e)
{
string goID = dataGridView1.CurrentRow.Cells[0].Value.ToString();
if (goID == null)
{
MessageBox.Show("请选择发信人", "消息");
Application.Exit();
}
string strSQL = "SELECT UserName FROM User1 WHERE id = '" + Sid + "'";
DataTable dt = SQLHelper.GetDataTable(strSQL);
String name = dt.Rows[0]["UserName"].ToString();
news ne = new news(Sid,goID,name);
ne.Owner = this;
ne.ShowDialog();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StudentManage
{
public partial class Form1 : Form
{
private string id;
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
//用户名
string strUserName = tbID.Text;
//密码
string strPassword = tbPWD.Text;
//判断空值
if (strUserName == "")
{
MessageBox.Show("用户名不能空!", "信息");
return;
}
if (strPassword == "")
{
MessageBox.Show("密码不能空!", "信息");
return;
}
string strSQL = "SELECT * FROM User1 WHERE UserName = '" + strUserName + "' AND UserPassword = '" + strPassword + "'";
DataTable dt = SQLHelper.GetDataTable(strSQL);
if (dt.Rows.Count == 1)
{
this.DialogResult = DialogResult.OK;
id = dt.Rows[0]["ID"].ToString();
}
else
{
MessageBox.Show("用户名或密码错误");
}
}
public string ID
{
get
{
return id;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
register re = new register();
re.ShowDialog();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StudentManage
{
public partial class news : Form
{
private string Sid;
private string goID;
private string name;
public news(String id,String tID,String nam)
{
goID = tID;
Sid = id;
name = nam;
InitializeComponent();
}
private void btnrefresh_Click(object sender, EventArgs e)
{
string toID = Sid;
string sql = "SELECT TBMsg.ID, UserName, Msg, SendTime FROM User1 JOIN TBMsg ON User1.ID = TBMsg.FromID "
+ "WHERE FromID = '" + goID + "' and ToID = '" + toID + "' and IsRead = 0";
DataTable dt = SQLHelper.GetDataTable(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
string time = dt.Rows[i]["SendTime"].ToString();
string username = dt.Rows[i]["UserName"].ToString();
string ms = dt.Rows[i]["Msg"].ToString();
string stms = time + ": " + username + ": " + ms + "\r\n";
textBox2.AppendText(stms);
}
}
private void btnsend_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textsend.Text))
{
MessageBox.Show(goID);
return;
}
string fromID = Sid;
string toID = goID;
string ms = textsend.Text;
string st = DateTime.Now.ToString();
textsend.Text = goID;
string sql = "INSERT TBMsg (FromID, ToID, Msg, SendTime, IsRead) values (" + Sid + "," + toID + ", '" + ms + "','" + st + "',0)";
int i = SQLHelper.CommandSQL(sql);
if (i == 1)
{
MessageBox.Show("发送成功!","消息");
string strms = st + ": "+name+": " + ms + "\r\n";
textBox2.AppendText(strms);
textsend.Clear();
}
}
private void btnexit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace StudentManage
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 login = new Form1();
DialogResult result = login.ShowDialog();
string id = login.ID;
if (result == DialogResult.OK)
{
Application.Run(new main2(id));
}
Form1 log = new Form1();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StudentManage
{
public partial class register : Form
{
public register()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void btnAdd_Click(object sender, EventArgs e)
{
string userName = tbID.Text;
string pass = password.Text;
string pass2= password2.Text;
//判断不能有空值
if (userName == "")
{
MessageBox.Show("用户名不能空!", "信息");
return;
}
if (pass == "")
{
MessageBox.Show("密码不能空!", "信息");
return;
}
if (pass == "")
{
MessageBox.Show("重复密码不能空!", "信息");
return;
}
//用户是否存在
string SQLUser = "SELECT * FROM User1 WHERE UserName = '" + userName + "'";
DataTable dt = SQLHelper.GetDataTable(SQLUser);
if (dt.Rows.Count > 0)
{
MessageBox.Show("学生信息已存在", "信息");
}
else
{
if (pass == pass2)
{
string SQLAddUser = "INSERT INTO User1 (UserName, UserPassword) VALUES ('" + userName + "','" + pass + "');";
int count = SQLHelper.CommandSQL(SQLAddUser);
if (count == 1)
{
MessageBox.Show("注册成功", "信息");
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("注册失败", "信息");
}
}
else
{
MessageBox.Show("密码不一致", "信息");
}
}
}
}
}

浙公网安备 33010602011771号