C#笔记
# 1.数据结构
# 1.Dictionary的基本用法
Dictionary<int, string> dict = newDictionary<int, string>();
dict.Add(1, "111");
dict.Add(2, "222");
//删除键为“C”的元素
dict.Remove("C");
//判断是否存在相应的key并显示
if(dict.ContainsKey(2))
{
Console.WriteLine(dict[2]);
}
//遍历Keys
foreach(var item indict.Keys)
{
Console.WriteLine("Key:{0}", item);
}
//遍历Values
foreach(var item indict.Values)
{
Console.WriteLine("value:{0}", item);
}
//遍历整个字典
foreach(var item indict)
{
Console.WriteLine("key:{0} value:{1}", item.Key, item.Value);
}
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
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
# 2. 基础操作
# 1 string类型转换成double
string numStr = "33.44";
double numDouble1 = double.Parse(numStr);
double numDouble2 =Convert.ToDouble(numStr);
1
2
3
2
3
# 2 获取当前路径7种方法 (opens new window)
//获取模块的完整路径。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//获取和设置当前目录(该进程从中启动的目录)的完全限定目录
string path2 = System.Environment.CurrentDirectory;
//获取应用程序的当前工作目录
string path3 = System.IO.Directory.GetCurrentDirectory();
//获取程序的基目录
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//获取和设置包括该应用程序的目录的名称
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//获取启动了应用程序的可执行文件的路径
string path6 = System.Windows.Forms.Application.StartupPath;
//获取启动了应用程序的可执行文件的路径及文件名
string path7 = System.Windows.Forms.Application.ExecutablePath;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 3 double 保留小数点后几位
Double value = Math.Round(value,4); // 简单实用★★★★
Double salary1 = 10000.0;
Double salary2 = 10000.12345;
Double salary3 = 10000.1289;
Int salary4 = 10000;
string salary5 = "10000";
string salary6 = "10000.12345";
string salary7 = "10000.1289"
Convert.ToDouble(salary1).ToString("0.00");//保留小数点后两位,结果为10000.00
Convert.ToDouble(salary2).ToString("0.00");//保留小数点后两位,结果为10000.12
Convert.ToDouble(salary3).ToString("0.00");//保留小数点后两位,结果为10000.13
Convert.ToDouble(salary4).ToString("0.00");//保留小数点后两位,结果为10000.00
Convert.ToDouble(salary5).ToString("0.00");//保留小数点后两位,结果为10000.00
Convert.ToDouble(salary6).ToString("0.00");//保留小数点后两位,结果为10000.12
Convert.ToDouble(salary7).ToString("0.00");//保留小数点后两位,结果为10000.13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 4 项目更换DLL
- 在官网下载指定 .net 版本的 dll文件。这里以
System.Data.SQLite.dll
为例
网站如下:
http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki (opens new window)
2.放到指定工程的bin目录。比如这里的:
在工程里面右键 remake
# 3.各种控件
# 1 openfiledialog
属性:
InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*" FilterIndex 在对话框中选择的文件筛选器的索引,如果选第一项就设为1 RestoreDirectory 控制对话框在关闭之前是否恢复当前目录 FileName 第一个在对话框中显示的文件或最后一个选取的文件 Title 将显示在对话框标题栏中的字符 AddExtension 是否自动添加默认扩展名 CheckPathExists 在对话框返回之前,检查指定路径是否存在 DefaultExt 默认扩展名 DereferenceLinks 在从对话框返回前是否取消引用快捷方式
1
2
3
4
5
6
7
8
9
10
11
12模板:
private void openFileDialogBTN_Click(object sender, System.EventArgs e){ OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "c:\\\\";//注意这里写路径时要用c:\\\\而不是c:\\ openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog()==DialogResult.OK) // 做一些操作 { fName=openFileDialog.FileName; File fileOpen=new File(fName); isFileHaveName=true; richTextBox1.Text=fileOpen.ReadFile(); richTextBox1.AppendText(""); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 2.chart
chart1.Series[0].ChartType = SeriesChartType.Line; // 折线图 chart1.Series[0].Color = Color.Red; // 折线颜色 //chart1.Series[0].IsValueShownAsLabel = true; // 显示每个点的数值 //chart1.Series[0].MarkerColor = Color.Red; // 每个点高亮 //chart1.Series[0].MarkerStyle = MarkerStyle.Square; // 高亮形状 chart1.Series.Clear(); chart1.Titles.Clear(); ChartArea area = chart1.ChartAreas[0]; ChartHelper.SetStyle(chart1, Color.White, Color.Black); ChartHelper.SetLegend(chart1, Docking.Top, StringAlignment.Center, Color.Transparent, Color.Black); ChartHelper.SetMajorGrid(chart1, Color.Gray, 20, 2); area.AxisY.Title = "Amp"; area.AxisY.TitleAlignment = StringAlignment.Far; area.AxisY.TitleForeColor = Color.Black; area.AxisY.LabelStyle = new LabelStyle() { ForeColor = Color.Black }; area.AxisY.LineColor = Color.Black; area.AxisY.ArrowStyle = AxisArrowStyle.Triangle; area.AxisY.Interval = Convert.ToDouble(((showY.Max() - showY.Min()) / 6).ToString("0.00000")); area.AxisX.Title = "time"; area.AxisX.TitleAlignment = StringAlignment.Far; area.AxisX.TitleForeColor = Color.Black; area.AxisX.LabelStyle = new LabelStyle() { ForeColor = Color.Black }; area.AxisX.LineColor = Color.Black; area.AxisX.ArrowStyle = AxisArrowStyle.Triangle; //SharpTriangle; area.AxisX.TitleAlignment = StringAlignment.Center; area.AxisX.MajorGrid.Enabled = true; area.AxisX.Interval = AxisXInterva; area.AxisX.ScrollBar.Size = 12; area.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll; area.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All ^ ScrollBarButtonStyles.ResetZoom; area.AxisX.ScrollBar.IsPositionedInside = false; area.AxisX.ScrollBar.Enabled = true; area.AxisX.ScaleView.Size = ScaleViewSize; chart1.Series.Add("Current"); // chart1.Series[0].ChartType = SeriesChartType.SplineRange; chart1.Series[0].ChartType = SeriesChartType.Line; chart1.Series[0].Color = Color.Red; //chart1.Series[0].IsValueShownAsLabel = true; // 显示每个点的数值 //chart1.Series[0].MarkerColor = Color.Red; // 每个点高亮 //chart1.Series[0].MarkerStyle = MarkerStyle.Square; if (false) { chart1.Series[0].IsValueShownAsLabel = true; chart1.Series[0].MarkerStyle = MarkerStyle.Circle; chart1.Series[0].MarkerColor = Color.Red; chart1.Series[0].LabelForeColor = Color.Blue; chart1.Series[0].LabelAngle = -90; } chart1.Titles.Add($"time:{dateTimePicker2.Value.ToString()} {comboBox2.SelectedItem} Current"); chart1.Titles[0].Font = new Font("微软雅黑", 12); chart1.Titles[0].Docking = Docking.Bottom; chart1.Titles[0].ForeColor = Color.FromArgb(46, 199, 201);
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
编辑 (opens new window)
上次更新: 2022/04/21, 14:18:17