项目中的心得
登录窗体的设计:
1.1用户名的输入采用ComboBox控件来实现输入
ComboBox下拉实现图片的显示和用户名的现示,效果如下
1.1.1新建一个类,该类保存用户登录名(唯一标识),用户图片代码如下:
1 public class userinfo 2 5 { 6 7 8 9 10 11 12 13 public string userName; 14 15 16 17 public Image userPic; 18 19 20 21 public userinfo(string username,Image userpic) 22 23 24 25 { 26 27 28 29 this.userName = username; 30 31 32 33 this.userPic = userpic; 34 35 36 37 } 38 39 40 41 public override string ToString() 42 43 44 45 { 46 47 48 49 return userName; 50 51 52 53 } 54 55 56 57 } 58 59
1.1.2更改ComboBox的属性DrawMode如下:
属性 赋值
1.1.3在窗体的构造函数里将从本地Access数据库里读出来的每一行的记录创建一个对象,然后加到ComboBox里的Iitems里,代码如下:
userinfo[] userinfos;
public FormLogin()
{
InitializeComponent();
con.ConnectionString = CommonClass.Setting.ConStr;
userInfoOleDbConnection.ConnectionString = CommonClass.Setting.accessConStr;
userinfos = new userinfo[3];
try
{
userInfoOleDbConnection.Open();
blueHillDataSet1.Clear();
userInfoOleDbDataAdapter.Fill(blueHillDataSet1.userInfo);
if (blueHillDataSet1.userInfo != null)
{
for (int i = 0;i<blueHillDataSet1.userInfo.Rows.Count; i++)
{
Image userPic;
DataRow dr = blueHillDataSet1.userInfo.Rows[i];
if (dr["userPic"] != DBNull.Value || dr["userPic"].ToString().Trim() != "")
{
Byte[] userImg = (Byte[])dr["userPic"];
MemoryStream ms = new MemoryStream(userImg);
userPic = new Bitmap(ms);
}
else
{
userPic = new Bitmap(AppDomain.CurrentDomain.BaseDirectory+@"\Single_16.ico");
}
userinfo user = new userinfo(dr["userName"].ToString(), userPic);
userinfos[i] = user;
userNamecomboBox.Items.Add(user);
}
userNamecomboBox.SelectedIndex = 0;
}
}
catch { }
finally { userInfoOleDbConnection.Close(); }
1.1.4在ComboBox控件里的DrawItem事件里,代码如下:
private void userNamecomboBox_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;//新建一个画布
e.DrawBackground();
Rectangle rtg = new Rectangle(e.Bounds.X + 2, e.Bounds.Top + 1, e.Bounds.Height, e.Bounds.Height);
g.DrawImage(userinfos[e.Index].userPic, rtg);
SizeF sf = g.MeasureString(e.Index.ToString(), e.Font);
g.DrawString(userinfos[e.Index].userName, e.Font, new SolidBrush(Color.Red), e.Bounds.Left + e.Bounds.Height + 2, e.Bounds.Top + (e.Bounds.Height - sf.Height) / 2);
}
1.2用到知识点:
(1)从电脑本地的图片文件读取成字节数
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (System.IO.File.Exists(openFileDialog1.FileName))
{
textBox1.Text = "";
textBox1.Text = openFileDialog1.FileName;
picPhoto.Image = new Bitmap(textBox1.Text);
FileStream fs =File.OpenRead(textBox1.Text);
photoArray = new byte[fs.Length];
fs.Read(photoArray, 0, (int)fs.Length);
fs.Close();
}
else
{
textBox1.Text = "上传相片";
MessageBox.Show("文件不存在!");
}
}
pictureBox的Image属性可以显示本地的图片:
pictureBox1.Image=new Bitmap(本地图片文件路径);
(2)把字节数组显示出图片(图片在数据库中的字节数组为 bt)
1.先申明内存流(System.IO名称空间下)
MemoryStream ms=new MemoryStream(bt);
2.然后新建一个图片对象
Image img=new Bitmap(ms);
(3)用哈希算法对密码加密
UnicodeEncoding UE = new UnicodeEncoding();
byte[] psw = UE.GetBytes(txtPassword.Text);
SHA1Managed sm = new SHA1Managed();//名称空间为System.Security.Cryptography;
byte[] hashpsw = sm.ComputeHash(psw);
(4)数据库连接字符串使用和文件读写操作
a.Access数据库的连接字符串
public static string accessConStr
{
get
{
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+AppDomain.CurrentDomain.BaseDirectory+"\\userInfo.mdb";
}
}
获取当前程序运行的文件夹下的userInfo.mdb数据库,如果是网页的话,当前文件夹就是解决方案下的文件夹,而应用程序的话,当前文件夹就是bin文件
b.sql server数据库的连接字符串
public static string ConStr
{
get
{
using (System.IO.StreamReader sr = new System.IO.StreamReader("connectionString.txt"))
{
return sr.ReadToEnd();
}
}
}
c.文件的读写操作
新建一个System.IO.StreamReader对象,然后读取当前文件(bin)文件下的connectionString.txt文件,ReadToEnd()方法,读取整个记事本里面的内容,另外还有写操作
Using (System.IO.StreamWrite sw=new System.IO.StreamWrite(文件夹路径名,存在与不存在是否操作)
using (StreamWriter sr = new StreamWriter("lj.txt", false))
{
string s1=@"Data Source=.\SQLEXPRESS;AttachDbFilename=";
string s2 = ";Integrated Security=True;User Instance=True";
string s3 = txtFilesName.Text;
sr.WriteLine(string.Format("{0}{1}{2}", s1, s3, s2));
}
(5)打开窗体的方法
在要打开的窗体类里写一个静态的(有返回或无返回值)的方法,然后在这个方法里写一个打开窗体的语句,ShowDialog()方法来完成,代码如下
public static int getEmployid()
{
FormEmpList fe = new FormEmpList();
fe.ShowDialog();
int id = (int)fe.lbEmp.SelectedValue;
return id;
}
(6)打开主窗体,弹出登录窗体.
在主窗体中的load事件中写上下面代码
private void FormMain_Load(object sender, EventArgs e)
{
Security.FormLogin flg = new Security.FormLogin();
if (flg.ShowDialog(this) != DialogResult.OK)
{
this.Dispose();
Application.Exit();
}
}
在登录窗体成功之后写上窗体的返回值
This.DialogReasult=DialogReasult.OK;//this指登录窗体
部门窗体的设计及用到的方法和技巧
2.1 DataGrid、DataGridView使用
2.1.1 DataSource数据源
这两种控件里的DataSource可以直接将DataTable给它,也可以拖一个BindinSource控件,然后将BindinSource与DataSet绑定,将DataSource的属性设为BindinSource
2.1.2 获得鼠标选中的一列的索引
a.DataGrid的属性CurrentRowIndex可以获得当前鼠标选中的一列
int selectIndex=this.grdEmployee.CurrentRowIndex;
DataRow dr = ds.spwinGetEmpbyDeptName[selectIndex];
b.DataGridView中用事件来获取当前选中的一行
c.更改标题,可在设计器中更改,也可以用代码更改
dataGridView1.Columns["ID"].HeaderText = "用户名";
2.2 新建一个树,并实现拖的功能,代码如下
private void createDptTree()
{
tvDept.Nodes.Clear();
fillDs();
foreach (DataRow dptRow in blueHillDataSet1.tblDepartment.Rows)
{
//TreeNode dptNode = new TreeNode(dptRow["DeptName"].ToString());//新建一个部门结点
DeptNode dptNode = new DeptNode(dptRow["DeptName"].ToString().Trim(), dptRow,0,0);
DataRow[] childRow =blueHillDataSet1.viwwinEmployeeList.Select(string.Format("DeptName='{0}'", dptRow["DeptName"].ToString()));
//DataRow[] childRow = dptRow.GetChildRows("tblDepartment_viwwebEmpCommonInfo");//方法二根据两表的关系,得到这一部门的员工的子表
tvDept.Nodes.Add(dptNode);
foreach (DataRow emleRow in childRow)
{
//TreeNode emleNode = new TreeNode(emleRow[""].ToString());
EmleNode emleNode = new EmleNode(emleRow["Name"].ToString().Trim(), emleRow,1,1);
if (dptNode.ManagerID.Trim() == emleNode.employeeID.Trim())
{
dptNode.ManagerName = emleNode.Name;
emleNode.ForeColor = Color.Red;
}
dptNode.Nodes.Add(emleNode);
}
}
}

浙公网安备 33010602011771号