1、Access数据库
指纹打卡机采用的是加密了的Access数据库。
我就在想,办公室电脑如果不安Access软件是否也能操作到这个文件,回答是肯定的。从网上载录:
Access不是一种存储格式,是一种软件。 请大家严格区分 Access 软件与 MDB 格式。以下的语句是错误的:“我用VB来开发MIS系统,数据存储在ACCESS中。”。VB下调用 MDB 格式(或 JET DB)只是调用一种格式的文件而已,并没有调用到 Access,其实官方说法叫 “VB 的 JET 应用”。另外,MDB 格式的文件也可以被其他开发工具,比如 DELPHI / PB / C / PHP / ASP / CGI .... 调用。但是被调用的只是 MDB 文件,很多 ACCESS 软件所具有的功能,这些开发工具是无法调用的。
也正因为如此,类似这样的问题“我的系统中没有安装 OFFICE ACCESS,是否能用 ASP 调用 ACCESS 数据库呢?”,答案是:当然可以,其实你根本没有使用 ACCESS 这个软件,只是使用了 MDB 这种数据库格式,而这种格式只要系统中有 OLE DB 引擎就可以支持。WINDOWS 98 以及以上版本的 WINDOWS 操作系统都内置支持 OLE DB 引擎。
2、开始研究C#
首先,遇到的一个问题是,ListBox如何得到多选的内容,最后我自己试验成功:
- private void button1_Click(object sender, EventArgs e)
- {
- string foo = "";
- /*
- for (int i = 0; i
- {
- foo += listBox1.Items[i].ToString();
- }
- */
- foreach(object item in listBox1.SelectedItems)
- {
- foo += item.ToString();
- }
- MessageBox.Show(foo, "提示", MessageBoxButtons.OK);
- }
使用for和foreach都可以,关键在于foreach里的object item!还有就是listbox1不能直接使用
SelectedValue
,因为这个属性不属于ListBox,而属于listControl。在Visual Studio 2010中,没有直接能画出ListControl的控件。
3、主窗口隐藏与再次显示
登录时,有个欢迎界面,不能关闭,在主窗口出现之前提前出现。然后主窗口调用form2,此时把自己隐藏掉:
- public Form1()
- {
- CfrmSplash frmSplash = new CfrmSplash();
- frmSplash.Show();
-
- InitializeComponent();
-
- for (int i = 0; i = 100; i+=1)
- {
- frmSplash.label2.Text = i.ToString()+"%";
- frmSplash.progressBar1.Value = i;
- frmSplash.Refresh();
- }
- frmSplash.Close();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- //form1.Close();
- Form2 form2 = new Form2(this);
- form2.Show();
- this.Hide();
- }
form2里的关闭按钮,点击关闭form2,而显示出form1来。
- public partial class Form2 : Form
- {
- private Form showForm;
- public Form2(Form mainform)
- {
- InitializeComponent();
- showForm = mainform;
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- this.Close();
- showForm.Show();
- }
-
- }
我用的是超级笨的方式,把form1对象作为参数传入form2的构造函数。网上还有很多好方法,一是通过订阅form2的关闭事件:
- public class Form2:Form
- {
- private Form1 form1;
- private TextBox textBox1=new TextBox();
- public Form2(Form1 form1)
- {
- this.form1=form1;
- this.Closed+=new EventHandler(this.Form2_Closed);//订阅Form的Closed事件
- }
-
- private void Form2_Closed(object sender,EventArgs e)//Closed事件处理程序
- {
- this.form1.textBox1.Text=this.textBox1.Text;
- }
-
- }
-
- public class Form1:Form
- {
- public TextBox textBox1=new TextBox();
- Form2 form2=new Form2(this);
- ...
- }
这种方式我没有最终实现,但感到总体来说还是按照自己摸索出来的方式执行的。
4、获取日期/时间控件的值
考勤机一定要统计某段时间内的考勤次数,所以必须取得控件里的日期。
- string n = this.dateTimePicker1.Value.ToString().Substring(0,10);
- MessageBox.Show((n.Length).ToString(), "呵呵", MessageBoxButtons.OK);
5、对MessageBox.Show()值的判断
C#推荐采用DialoguResult类进行判断,该类有几个属性:OK,Yes,No,Cancel等。
- DialogResult result = new DialogResult();
- if (richTextBox1.Modified)
- {
- result = MessageBox.Show("文件"+this.Text+"内容已经更改,是否需要保存?","保存提示",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Asterisk);
- }
- if (result == DialogResult.Yes)
- {
- this.保存ToolStripMenuItem_Click(sender,e);
- }else if (result == DialogResult.Cancel)
- {
- return false;
- }
6、文本文件的读入
把一份文本文件读入至RichTextBox中,过程如下:
- OpenFileDialog openfile1 = new OpenFileDialog();
- openfile1.DefaultExt = "*.txt";//默认要打开的文件类型
- openfile1.Filter = "txt.File(*.txt)|*.txt|All File(*.*)|*.*";//过滤目录下的文件类型
-
- if(openfile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openfile1.FileName.Length > 0)//使用文件打开对话框,注意判断的时候采用S.W.F.DialogResult进行
- {
- //使用richTextBox对象的载入文件方法,但是第二个参数可以没有,但有的话更安全,用来指定加载或输出至本控件的输入流或输出流的类型
- richTextBox1.LoadFile(openfile1.FileName,RichTextBoxStreamType.RichText);
- this.Text = Path.GetFileName(openfile1.FileName) + "--文本编辑器";//GetFileName就是只获得带路径的文件名的名字,而去掉路径
- fileAdress = openfile1.FileName;
- richTextBox1.Modified = false;
- }
网上出现读入文件乱码的现象,大家建议通过判断扩展名来选择不同的打开参数类型:
- OpenFileDialog fileone = new OpenFileDialog();
- fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
- fileone.FilterIndex = 1;
- if (fileone.ShowDialog() == DialogResult.OK)
- {
- string filename = openfiledialog.FileName;
- string fileext = new System.IO.FileInfo(filename).Extension; //获得文件的扩展名
- switch (fileext.ToLower())
- {
-
- case ".txt":newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);break;
- case ".rtf":newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);break;
- }
7、文件的保存
和读入一样,使用一个对话框,只不过此时使用的是SaveFileDialog。
- SaveFileDialog sf = new SaveFileDialog();
- sf.Title = "保存";
- sf.FileName = "新建文本文档.txt";
- sf.Filter = "txt.File(*.txt)|*.txt|All File(*.*)|*.*";
- sf.DefaultExt = ".txt";
- if(sf.ShowDialog() == System.Windows.Forms.DialogResult.OK && sf.FileName.Length > 0)
- {
- richTextBox1.SaveFile(fileAdress, RichTextBoxStreamType.PlainText);
- richTextBox1.Modified = false;
- this.Text = Path.GetFileName(sf.FileName+"文本编辑器");
- fileAdress = sf.FileName;
- }
其实,文件“另存为”也是用的同样的代码只是FileName为空。
8、设置数据源绑定到表格,并且直接在报表中显示
- 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;
- using IrisSkin2;
-
- namespace WindowsFormsApplication4
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- this.skinEngine1.SkinFile = "Vista2_color6.ssk";
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- // TODO: 这行代码将数据加载到表“dBDataSet.教师”中。您可以根据需要移动或删除它。
- this.教师TableAdapter.Fill(this.dBDataSet.教师);
-
- this.reportViewer1.RefreshReport();
- }
-
- private void fillByToolStripButton_Click(object sender, EventArgs e)
- {
- try
- {
- this.教师TableAdapter.FillBy(this.dBDataSet.教师);
- }
- catch (System.Exception ex)
- {
- System.Windows.Forms.MessageBox.Show(ex.Message);
- }
-
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- string start, end;
- start = dateTimePicker1.Value.ToString().Substring(0, 10);
- end = dateTimePicker1.Value.ToString().Substring(0, 10);
- MessageBox.Show(start+"***"+end);
- }
-
-
- private void toolStripButton1_Click(object sender, EventArgs e)
- {
- try
- {
- this.教师TableAdapter.FillBy1(this.dBDataSet.教师);
- reportViewer1.RefreshReport();
- }
- catch (System.Exception ex)
- {
- System.Windows.Forms.MessageBox.Show(ex.Message);
- }
- }
-
- private void toolStripButton2_Click(object sender, EventArgs e)
- {
- try
- {
- this.教师TableAdapter.FillBy(this.dBDataSet.教师);
- reportViewer1.RefreshReport();
- }
- catch (System.Exception ex)
- {
- System.Windows.Forms.MessageBox.Show(ex.Message);
- }
- }
-
- private void fillBy1ToolStripButton_Click(object sender, EventArgs e)
- {
-
- }
-
- }
- }
9、如何设置toolstrip的高度
简单,放到一个容器中,设置它的Dock为Fill,然后重要的是把其中的Button的autosize设置成false。