阶段总结

目录

打开文件对话框...3

c#中的using语句块...3

多边形面积...4

最近打开列表...5

操作Datagridview(以MainGridView为要操作的Datagridview...8

1.添加列...8

2.DataGridViewTextBoxColumn中添加项...9

3.DataGridViewComboBoxColumn 中添加项...9

添加CheckedListBox...9

字符串提取数字...10

读取简单CSV文件...10

计算程序运行时间...11

保存csv/保存文件对话框...11

实现List<T>的排序...12

PDF转图片...13

 

 

 

 

 

 

 

 

 

 

 

 

 


 

打开文件对话框

C# Code 

1
2
3
4
5
6
7
8
9
10
11
12

 

using (OpenFileDialog Dialog = new OpenFileDialog())//使用using可以在用完以后自己销毁
{
    Dialog .Title = 
"选择图片";//标题
    Dialog .InitialDirectory = "";//获取或设置对话开始时显示的目录
    Dialog .Filter = "图片文件|*.bmp;*.jpg;|音频文件|*.mp3;";
    
//如果 Filter 属性为 Empty,将显示所有文件。始终显示文件夹。多个筛选选项用竖线分隔。
    Dialog .FilterIndex = 1;//默认的筛选器,即为图片文件
    if (Dialog .ShowDialog() == DialogResult.OK)
    {
    }
}

 

c#中的using语句块

提供能确保正确使用 IDisposable 对象的方便语法。

File  Font 是访问非托管资源(本例中为文件句柄和设备上下文)的托管类型的示例 有许多其他类别的非托管资源和封装这些资源的类库类型。 所有这些类型都必须实现IDisposable 接口。按照规则,当使用 IDisposable 对象时,应在 using 语句中声明和实例化此对象。 using 语句按照正确的方式调用对象上的 Dispose 方法,并(在您按照前面所示方式使用它时)会导致在调用 Dispose 时对象自身离开作用域。  using 块中,对象是只读的并且无法修改或重新赋值。

using 语句确保即使在调用对象上的方法时发生异常Dispose方法也会被调用。 可通过将对象放入 try 块中并在finally块中调用Dispose来达到同样的结果;实际上,这就是编译器转换 using 语句的方式。 

通常最好是在 using 语句中实例化该对象并将其作用域限制到using块中。

 

 

多边形面积

 C# Code 

1
2
3
4
5
6
7
8
9
10
11

 

public int PolygonSquare(List<Point> onePolygon)//向量法求多边形面积
{
    
//onePolygon为包含多边形所有点的点集
    int area = 0;
    
for(int i = 0; i < onePolygon.Count - 1; i++)
    {
        area += (onePolygon[i].X * onePolygon[i + 
1].Y - onePolygon[i + 1].X * onePolygon[i].Y);
    }
    
return Math.Abs(area / 2);//返回的是多边形面积
}

 

 


最近打开列表

  C# Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

 

#region 局部变量
List<ToolStripDropDownItem> recentFileItem = 
new List<ToolStripDropDownItem>();//记录所有添加的最近打开菜单项,便于统一管理
List<string> recentFileList = new List<string>();//记录所有的最近打开记录
int MaxRecntFile = 3;//定义最大记录数
string INIAddress = "c:\\Menu.ini";
#endregion
public void refreshRecentFile(ToolStripMenuItem mainMenu)//没有打开文件时刷新最近列表(在Form_Load事件中调用)
{
    
try
    {
        StreamReader sr = 
new StreamReader(INIAddress);
        
while (sr.Peek() >= 0)//读取所有记录
        {
            
string path = sr.ReadLine();
            checkAndAddRecentFileList(path);
        }
        readRecentFile(mainMenu);
//调用readRecentFile将记录转换成菜单项
        sr.Dispose();
    }
    
catch { };
}

public void refreshRecentFile(ToolStripMenuItem mainMenu, string filename)//打开文件后刷新最近文件列表,在得到文件地址后调用
{
    checkAndAddRecentFileList(filename);
    
try
    {
        StreamWriter s = 
new StreamWriter(INIAddress, false);//打开INIAddress
        foreach (string path in recentFileList)//recentFileList中的所有记录写入
        {
            s.WriteLine(path);
        }
        s.Flush();
        s.Close();
        readRecentFile(mainMenu);
    }
    
catch { }
}

private void readRecentFile(ToolStripMenuItem mainMenu)//读取最近打开的文档列表
{
    
foreach (ToolStripDropDownItem recentItem in recentFileItem)//清除现有的所有最近打开
    {
        mainMenu.DropDownItems.Remove(recentItem);
    }
    
foreach (string recentPath in recentFileList)//将记录于“recentFileList”中的所有记录转成菜单形式并添加到主菜单下
    {
        ToolStripMenuItem recentFile = 
new ToolStripMenuItem();
        recentFile.Tag = recentPath;
//记录真实地址
        recentFile.Text = "&" + System.IO.Path.GetFileNameWithoutExtension((string)recentFile.Tag);//提取文件名
        recentFile.Click += new EventHandler(openRecentFile);//绑定动作
        mainMenu.DropDownItems.Add(recentFile);//将其添加到recentFileItem表中便于统一管理
        recentFileItem.Add(recentFile);//添加到菜单项中
    }
}

private void checkAndAddRecentFileList(string path)//只保存3个最近打开文件
{
    
if (recentFileList.Count == MaxRecntFile)//如果已达到最大记录数
    {
        
for (int i = 0; i < MaxRecntFile - 1; i++)//删除第一个最近文件,其他的记录前移一位
            recentFileList[i] = recentFileList[i + 1];
        recentFileList[MaxRecntFile - 
1] = path;//将新路径排在最后
    }
    
else
        recentFileList.Add(path);
//否则直接记录
}

private void openRecentFile(object sender, EventArgs e)//打开最近打开的文档
{
    ToolStripMenuItem tempButton = (ToolStripMenuItem)sender;
    
string filepath = (string)tempButton.Tag;//根据菜单项的Tag属性中记录的真实地址打开文件
}
#endregion

 

 

操作Datagridview(以MainGridView为要操作的Datagridview

1.添加列

 C# Code 

1
2
3
4
5

 

DataGridViewTextBoxColumn DGVTBC = new DataGridViewTextBoxColumn();//项为TextBox的列
DataGridViewComboBoxColumn DGVCBC = new DataGridViewComboBoxColumn();//项为ComboBox的列
DGVTBC.HeaderText = "xxxx";//列名称
DGVTBC .ReadOnly = true;//该列只读
MainGridView.Columns.Add(DGVTBC);//MainGridView这个DataGridView中添加该列

 

2.DataGridViewTextBoxColumn中添加项

 C# Code 

1
2

 

int index = MainGridView.Rows.Add();//获取新添加的roi序号
MainGridView.Rows[index].Cells[0].Value = “XXXX”;//设置第一列的值

 

 

3.DataGridViewComboBoxColumn 中添加项

 C# Code 

1
2
3
4

 

//假设第二列为ComboBox
DataGridViewComboBoxCell cell = MainGridView.Rows[index].Cells[1as DataGridViewComboBoxCell;
cell.DataSource = NameList;
//假设NameList为一List<string>
cell.Value = NameList[2];//设置值

 

 

添加CheckedListBox

 C# Code 

1
2

 

CheckedListBox1.Items.Clear();//先清理所有的项
CheckedListBox1.Items.AddRange(ListName.ToArray());//再根据名称列表更新项(假设ListName为一List<string>)

 

字符串提取数字

 C# Code 

1
2
3
4
5
6
7
8
9
10
11

 

public static int GetNumber(string str)//从字符串提取数字
{
    
int result = 0;
    
if (str != null && str !string.Empty)
    {
        str = Regex.Replace(str, @
"[^\d.\d]""");  // 正则表达式剔除非数字字符(不包含小数点.
        if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$")) // 如果是数字,则转换为int类型
            result = int.Parse(str);
    }
    
return result;
}

 

 

读取简单CSV文件

 C# Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

 

public void OpenCSV(string filename)//打开csv的函数
{
    FileStream fs = 
new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    StreamReader sr = 
new StreamReader(fs, System.Text.Encoding.Default);   //记录每次读取的一行记录
    string strLine = "";//记录每行记录中的各字段内容
    string[] aryLine;//标示列数
    while ((strLine = sr.ReadLine()) != null)
    {
        Point temp = 
new Point();//假设该CSV结构为每行只有两个数据xY
        aryLine = strLine.Split(',');//数据按","分割
        temp.x = Convert.ToInt32(aryLine[0]);//存储读取的数据
        temp.Y = Convert.ToInt32(aryLine[1]);
    }
    sr.Close();
    fs.Close();
}

 

计算程序运行时间

 C# Code 

1
2
3
4
5
6
7
8
9
10

 

using System.Diagnostics;
public float spanSecond()
{
    Stopwatch stopwatch = 
new Stopwatch();//记录运行时间

//要运行的函数
    stopwatch.Start();
    stopwatch.Stop(); 
//  停止监视
    TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间
    float seconds = (float)timespan.TotalSeconds;
    
return seconds;
}

 

保存csv/保存文件对话框

 C# Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

 

public float SaveCSV(string CSV)
{
    SaveFileDialog saveFileDialog = 
new SaveFileDialog();
    saveFileDialog.DefaultExt = 
"*.csv";//获取或设置默认文件扩展名
    saveFileDialog.AddExtension = true;//如果用户省略扩展名,对话框是否自动在文件名中添加扩展名
    saveFileDialog.Filter = "csv 文件|*.csv";
    saveFileDialog.OverwritePrompt = 
true;//指示如果用户指定的文件名已存在,Save As 对话框是否显示警告
    saveFileDialog.CheckPathExists = true;//指示如果用户指定不存在的路径,对话框是否显示警告
    saveFileDialog.ValidateNames = true;//指示对话框是否只接受有效的 Win32 文件名
    saveFileDialog.FileName = "XXXX";
    
if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.FileName != null//打开保存文件对话框
    {
        
string fileName = saveFileDialog.FileName;//文件名字
        StreamWriter sw = new StreamWriter(fileName, true);
        sw.WriteLine(CSV); 
//向创建的文件中写入内容,假设CSV格式正确
        sw.Close();//关闭当前文件写入流
        textBox1.Text = string.Empty;//清空CSV缓存框
    }
}

 

实现List<T>的排序

1)声明一个类

 C# Code 

1
2
3
4
5
6

 

public class Person
{
    
public int age;
}

 

2)声明一个继承了接口IComparer<T>的类

 C# Code 

1
2
3
4
5
6
7

 

class PersonCompare : IComparer<Person>
{
    
public int Compare(Person a, Person b)
    {
        
return a.age - b.age
    }
}

 

3)建立一个List,并使用刚建立的PersonComparer类中的规则对List进行排序

 C# Code 

1
2
3

 

List<Person> a = new List<Person>();
a.Add(....);
//添加元素
a.Sort(new PersonComparer());//进行排序

 

PDF转图片

 C# Code 

1
2
3
4
5
6
7
8
9
10
11

 

using O2S.Components.PDFView4NET;
using O2S.Components.PDFRender4NET;
public Bitmap GetPDFToBitmap(string path, int page)
{
    PDFFile pdfFile = PDFFile.Open(path);
//打开PDF
    pagecount = pdfFile.PageCount;//获取PDF总页数
    Bitmap pageImage
    
if(page > 0 && page < pagecount)
        pageImage = pdfFile.GetPageImage(page, 
200); //200为所获取图像的PPI
    rerturn pageImage
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值