第一次C#界面编程备忘总结

第一次C#界面编程备忘总结

1、给一个按钮“浏览”增加save/open-Dialog对话框

“浏览”中的程序:

//仅仅是把对话框读到的路径显示到openPath.Text中去

if(openFileDialog1.ShowDialog() == DialogResult.OK)

   openPath.Text = openFileDialog1.FileName;

//通过open-Dialog的FileOk事件(点击"保存"或双击文件)打开某种文件

usingSystem.Diagnostics

{

System.IO.FileInfooppen1 =

   newSystem.IO.FileInfo(openFileDialog1.FileName);

Processpro = new Process();

pro.StartInfo.FileName= openFileDialog1.FileName;

pro.StartInfo.WorkingDirectory= oppen1.DirectoryName;

pro.Start();

}

2、MessageBox

MessageBox.Show(this,"串口已被占用!", "提示",MessageBoxButtons.OK, MessageBoxIcon.Information);

“串口...+Environment.NewLine + ..." (Environment.NewLine 表示回车)

Buttons还有别的组合

返回值的使用:  //显示信息框并得到返回值

System.Windows.Forms.DialogResultrt = MessageBox.Show(...);

返回类型有:

 DialogResult.Yes//DialogResult.No//DialogResult.Cancel//DialogResult.No

3、倒计时

添加个interval=1000(默认单位ms)的定时器:timer_timeDisplay

定义个时间的数组timeDisplay[],从界面获取倒计时的总时间timeSum

int[] timeDisplay= new int[3];

Int timeSum;

//"开始"中初步处理代码:

...

timeDisplay[0] =(int)timeSum;

timeDisplay[1] =(int)(60 * (timeSum - timeDisplay[0]));

timeDisplay[2] =(int)(60*(60*(timeSum - timeDisplay[0])

                 - timeDisplay[1]));

if (timeDisplay[2] ==0 && timeDisplay[1] != 0)

{

     timeDisplay[2]= 60;

     timeDisplay[1]--;

}

else if (timeDisplay[2] == 0 &&timeDisplay[1] == 0)

{

     timeDisplay[2]= 60;

     timeDisplay[1]= 59;

     timeDisplay[0]--;

 }

timer_timeDisplay.Start();

...

//倒计时显示

privatevoid timer_timeDisplay_Tick(object sender, EventArgs e)

{

      timeSum--;

    if (timeDisplay[2] != 0)

    {

        timeDisplay[2]--;

        timeDisplay.Text = timeDisplay[0] +"时" + timeDisplay[1] + "分" +timeDisplay[2] + "秒";

     }

    if (timeDisplay[2] == 0 &&timeDisplay[1] != 0)            

    {

        timeDisplay.Text = timeDisplay[0] +"时" + timeDisplay[1] + "分" + 00+ "秒";

        timeDisplay[1]--;

        timeDisplay[2] = 60;

     }

     if (timeDisplay[0] != 0 &&timeDisplay[1] == 0 && timeDisplay[2] == 0)

     {

         timeDisplay.Text = timeDisplay[0] +"时" + 00 + "分" + timeDisplay[2]+ "秒";

         timeDisplay[0]--;

         timeDisplay[1] = 59;

         timeDisplay[2] = 60;

      }

      if (timeDisplay[0] == 0 &&timeDisplay[1] == 0 && timeDisplay[2] == 0)

      {

         timer_timeDisplay.Stop();

         MessageBox.Show(this, "时间到,测试结束  " + Environment.NewLine+ "您可选择其他操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

         timeDisplay[0] = (int)timeSum;

         timeDisplay[1] = (int)(60 * (timeSum- timeDisplay[0]));

         timeDisplay[2] = (int)(60*(60*(timeSum -timeDisplay[0]) - timeDisplay[1]));

       }

}

4、获取路径文件名

 private string GetFileName(string path)

       {

           int start, end;

           start =path.LastIndexOf(@"\");

           end = path.LastIndexOf(".");//如果有扩展名时

           if (end < start) path =path.Substring(start + 1);

           else path = path.Substring(start +1, end - start - 1);

           return path;

       }

5、连接已有模版数据库文件

usingSystem.Data.OleDb;//对数据库操作时要添加

using System.Runtime.InteropServices;

publicOleDbConnection connData = new OleDbConnection

                ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +

                System.Windows.Forms.Application.StartupPath + @"\test.mdb");

public OleDbCommandcmd = new OleDbCommand();

//连接数据库,并写入通过串口收到的数据

private voidDataToAccess(int times1, string voltage1, string dB_dBm1)

{

cmd.CommandText= "Insert Into testdata(num,voltage,dB_dBm)

           Values('" + times1 +"','" + voltage1 + "','" + dB_dBm1 + "')";

cmd.Connection= connData;

connData.Open();

cmd.ExecuteNonQuery();

cmd.Dispose();

connData.Close();

}

6、拷贝模版数据库文件到用户指定path

try

{

System.IO.File.Copy(System.Windows.Forms.Application.StartupPath+ @"\test.mdb", path.Text);

connData.ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path.Text;

}

catch(System.IO.IOException)//如果目标文件已经存在

{

System.IO.File.Copy(System.Windows.Forms.Application.StartupPath+ @"\test.mdb", path.Text, true);

connData.ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.O;Data Source=" + path.Text;

}

7、将Access文件导入Excel文件

要先添加引用/COM/Microsoft Exccel 11.0 ObjectLibrary

usingMicrosoft.Office.Interop.Excel;


public boolDataToExcel(System.Data.DataTable mytb, string savepath)

{

string[]header = new string[3] { "num", "voltage","dB_dBm" };

//建立Excel对象

Microsoft.Office.Interop.Excel.Applicationmyexcel;

Microsoft.Office.Interop.Excel._Workbookmybook;

Microsoft.Office.Interop.Excel._Worksheetmysheet = null;

myexcel =new Microsoft.Office.Interop.Excel.ApplicationClass();

mybook =myexcel.Workbooks.Add(true);

mysheet =(Microsoft.Office.Interop.Excel._Worksheet)mybook.ActiveSheet;


//添加表头

myexcel.Cells[1,1] = "采样时间间隔:";

myexcel.Cells[1,3] = ts.Text + " s";

myexcel.Cells[2,1] = "采样总时间:";

myexcel.Cells[2,3] = th.Text + " h";

for (intcolomn = 0; colomn < 3; colomn++)

myexcel.Cells[4,colomn + 1] = header[colomn];


//填充数据到Excel(从Access第二行开始导入)

for (int i= 1; i < mytb.Rows.Count; i++)

{

for (int j= 0; j < 3; j++)

myexcel.Cells[i+ 4, j + 1] = mytb.Rows[i][j].ToString();

}

mybook.SaveCopyAs(savepath);

mybook.Saved= true;


//退出excel

myexcel.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(mybook);

System.Runtime.InteropServices.Marshal.ReleaseComObject(myexcel);

System.Runtime.InteropServices.Marshal.ReleaseComObject(mysheet);

mybook =null;

myexcel =null;

mysheet =null;

GC.Collect();

MessageBox.Show(this,"导出EXCEL成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

returntrue;

}


{

OleDbConnectionconnData1 = new OleDbConnection

            ("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" +

            System.Windows.Forms.Application.StartupPath + @"\test.mdb");

OleDbCommand cmd1 = new OleDbCommand("SELECT[num],[voltage],[dB_dBm] from testdata ", connData1);

OleDbDataAdapter data1 = new OleDbDataAdapter(cmd1);        //数据适配器

dataTab.Clear();                                            //释放表空间

data1.Fill(dataTab);

DataToExcel(dataTab, saveFileDialog2.FileName);

}



你可能感兴趣的:(C#)