Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4029259
  • 博文数量: 272
  • 博客积分: 7846
  • 博客等级: 少将
  • 技术积分: 6476
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 16:27
文章分类

全部博文(272)

分类: 嵌入式

2011-11-17 21:02:18


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如何得到多选的内容,最后我自己试验成功:
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             string foo = "";
  4.             /*
  5.             for (int i = 0; i < listBox1.Items.Count; i++)
  6.             {
  7.                 foo += listBox1.Items[i].ToString();
  8.             }
  9.              */
  10.             foreach(object item in listBox1.SelectedItems)
  11.             {
  12.                 foo += item.ToString();
  13.             }
  14.             MessageBox.Show(foo, "提示", MessageBoxButtons.OK);
  15.         }
    使用for和foreach都可以,关键在于foreach里的object item!还有就是listbox1不能直接使用SelectedValue,因为这个属性不属于ListBox,而属于listControl。在Visual Studio 2010中,没有直接能画出ListControl的控件。

3、主窗口隐藏与再次显示
    登录时,有个欢迎界面,不能关闭,在主窗口出现之前提前出现。然后主窗口调用form2,此时把自己隐藏掉:
  1. public Form1()
  2.         {
  3.             CfrmSplash frmSplash = new CfrmSplash();
  4.             frmSplash.Show();
  5.             
  6.             InitializeComponent();

  7.             for (int i = 0; i <= 100; i+=1)
  8.             {
  9.                 frmSplash.label2.Text = i.ToString()+"%";
  10.                 frmSplash.progressBar1.Value = i;
  11.                 frmSplash.Refresh();
  12.             }
  13.             frmSplash.Close();
  14.         }

  15.         private void button1_Click(object sender, EventArgs e)
  16.         {
  17.             //form1.Close();
  18.             Form2 form2 = new Form2(this);
  19.             form2.Show();
  20.             this.Hide();
  21.         }
    form2里的关闭按钮,点击关闭form2,而显示出form1来。
  1. public partial class Form2 : Form
  2.     {
  3.         private Form showForm;
  4.         public Form2(Form mainform)
  5.         {
  6.             InitializeComponent();
  7.             showForm = mainform;
  8.         }

  9.         private void button2_Click(object sender, EventArgs e)
  10.         {
  11.             this.Close();
  12.             showForm.Show();
  13.         }

  14.     }
    我用的是超级笨的方式,把form1对象作为参数传入form2的构造函数。网上还有很多好方法,一是通过订阅form2的关闭事件:
  1. public class Form2:Form
  2.    {
  3.         private Form1 form1;
  4.         private TextBox textBox1=new TextBox();
  5.         public Form2(Form1 form1)
  6.         {
  7.              this.form1=form1;
  8.              this.Closed+=new EventHandler(this.Form2_Closed);//订阅Form的Closed事件
  9.          }

  10.          private void Form2_Closed(object sender,EventArgs e)//Closed事件处理程序
  11.          {
  12.              this.form1.textBox1.Text=this.textBox1.Text;
  13.           }
  14.          
  15.    }

  16.    public class Form1:Form
  17.    {
  18.         public TextBox textBox1=new TextBox();
  19.         Form2 form2=new Form2(this);
  20.         ...
  21.     }
    这种方式我没有最终实现,但感到总体来说还是按照自己摸索出来的方式执行的。

4、获取日期/时间控件的值
    考勤机一定要统计某段时间内的考勤次数,所以必须取得控件里的日期。
  1.    string n = this.dateTimePicker1.Value.ToString().Substring(0,10);
  2.    MessageBox.Show((n.Length).ToString(), "呵呵", MessageBoxButtons.OK);

5、对MessageBox.Show()值的判断
    C#推荐采用DialoguResult类进行判断,该类有几个属性:OK,Yes,No,Cancel等。
  1. DialogResult result = new DialogResult();
  2.             if (richTextBox1.Modified)
  3.             {
  4.                 result = MessageBox.Show("文件"+this.Text+"内容已经更改,是否需要保存?","保存提示",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Asterisk);
  5.             }
  6.             if (result == DialogResult.Yes)
  7.             {
  8.                 this.保存ToolStripMenuItem_Click(sender,e);
  9.             }else if (result == DialogResult.Cancel)
  10.             {
  11.                 return false;
  12.             }

6、文本文件的读入
    把一份文本文件读入至RichTextBox中,过程如下:
  1. OpenFileDialog openfile1 = new OpenFileDialog();
  2.             openfile1.DefaultExt = "*.txt";//默认要打开的文件类型
  3.             openfile1.Filter = "txt.File(*.txt)|*.txt|All File(*.*)|*.*";//过滤目录下的文件类型

  4.             if(openfile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openfile1.FileName.Length > 0)//使用文件打开对话框,注意判断的时候采用S.W.F.DialogResult进行
  5.             {
  6. //使用richTextBox对象的载入文件方法,但是第二个参数可以没有,但有的话更安全,用来指定加载或输出至本控件的输入流或输出流的类型
  7.                 richTextBox1.LoadFile(openfile1.FileName,RichTextBoxStreamType.RichText);
  8.                 this.Text = Path.GetFileName(openfile1.FileName) + "--文本编辑器";//GetFileName就是只获得带路径的文件名的名字,而去掉路径
  9.                 fileAdress = openfile1.FileName;
  10.                 richTextBox1.Modified = false;
  11.             }

    网上出现读入文件乱码的现象,大家建议通过判断扩展名来选择不同的打开参数类型:
  1. OpenFileDialog fileone = new OpenFileDialog();
  2.   fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
  3.   fileone.FilterIndex = 1;
  4.   if (fileone.ShowDialog() == DialogResult.OK)
  5.   { 
  6.    string filename = openfiledialog.FileName;
  7.   string fileext = new System.IO.FileInfo(filename).Extension; //获得文件的扩展名
  8.   switch (fileext.ToLower())
  9.   {

  10.    case ".txt":newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);break;
  11.    case ".rtf":newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);break;
  12.   } 

7、文件的保存
    和读入一样,使用一个对话框,只不过此时使用的是SaveFileDialog。
  1. SaveFileDialog sf = new SaveFileDialog();
  2.                 sf.Title = "保存";
  3.                 sf.FileName = "新建文本文档.txt";
  4.                 sf.Filter = "txt.File(*.txt)|*.txt|All File(*.*)|*.*";
  5.                 sf.DefaultExt = ".txt";
  6.                 if(sf.ShowDialog() == System.Windows.Forms.DialogResult.OK && sf.FileName.Length > 0)
  7.                 {
  8.                     richTextBox1.SaveFile(fileAdress, RichTextBoxStreamType.PlainText);
  9.                     richTextBox1.Modified = false;
  10.                     this.Text = Path.GetFileName(sf.FileName+"文本编辑器");
  11.                     fileAdress = sf.FileName;
  12.                 }
    其实,文件“另存为”也是用的同样的代码只是FileName为空。

8、设置数据源绑定到表格,并且直接在报表中显示
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using IrisSkin2;

  10. namespace WindowsFormsApplication4
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             this.skinEngine1.SkinFile = "Vista2_color6.ssk";
  18.         }

  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             // TODO: 这行代码将数据加载到表“dBDataSet.教师”中。您可以根据需要移动或删除它。
  22.             this.教师TableAdapter.Fill(this.dBDataSet.教师);

  23.             this.reportViewer1.RefreshReport();
  24.         }

  25.         private void fillByToolStripButton_Click(object sender, EventArgs e)
  26.         {
  27.             try
  28.             {
  29.                 this.教师TableAdapter.FillBy(this.dBDataSet.教师);
  30.             }
  31.             catch (System.Exception ex)
  32.             {
  33.                 System.Windows.Forms.MessageBox.Show(ex.Message);
  34.             }

  35.         }

  36.         private void button1_Click(object sender, EventArgs e)
  37.         {
  38.             string start, end;
  39.             start = dateTimePicker1.Value.ToString().Substring(0, 10);
  40.             end = dateTimePicker1.Value.ToString().Substring(0, 10);
  41.             MessageBox.Show(start+"***"+end);
  42.         }


  43.         private void toolStripButton1_Click(object sender, EventArgs e)
  44.         {
  45.             try
  46.             {
  47.                 this.教师TableAdapter.FillBy1(this.dBDataSet.教师);
  48.                 reportViewer1.RefreshReport();
  49.             }
  50.             catch (System.Exception ex)
  51.             {
  52.                 System.Windows.Forms.MessageBox.Show(ex.Message);
  53.             }
  54.         }

  55.         private void toolStripButton2_Click(object sender, EventArgs e)
  56.         {
  57.             try
  58.             {
  59.                 this.教师TableAdapter.FillBy(this.dBDataSet.教师);
  60.                 reportViewer1.RefreshReport();
  61.             }
  62.             catch (System.Exception ex)
  63.             {
  64.                 System.Windows.Forms.MessageBox.Show(ex.Message);
  65.             }
  66.         }

  67.         private void fillBy1ToolStripButton_Click(object sender, EventArgs e)
  68.         {

  69.         }

  70.     }
  71. }

9、如何设置toolstrip的高度
    简单,放到一个容器中,设置它的Dock为Fill,然后重要的是把其中的Button的autosize设置成false。






阅读(4729) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~