c# 开发语言 .NET Framework杂记 - zoukankan

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力网站平台 👁️ 339 次阅读

这篇博客主要记录在用C#编写上位机时,不会的知识点,随时更新,方便查阅。

C#语法操作杂记

c#中让textbox选中不选中C#无法使用实例引用来访问成员解决方法针对不同定义情况的引用解释

C# 字符串分割用字符串分割用多个字符串分割用单字符分割

C#中删除字符串最后一个字符串的几种方式使用Remove移除使用Substring进行截取

C#点击关闭按钮 弹出确认框方法一:使用代码方法二:设置窗体属性内容解释

c# progressBar进度条改变颜色及显示进度数字C#一个窗体读取另一个窗体上控件的内容C#的File 类C#文件流FileStream简介File和FileStream的区别常用构造函数介绍将一个数组写入文件

c#中让textbox选中不选中

https://blog.csdn.net/qq_18975227/article/details/119342576

将TextBox的TabStop属性设置为false;这样运行的时候就不显示蓝色选中状态了

C#无法使用实例引用来访问成员解决方法

https://jingyan.baidu.com/article/37bce2be35afe95102f3a28a.html

针对不同定义情况的引用

解释

静态方法应该是属于类的,它的作用范围是类,而不是实例,所以实例访问不到这个静态方法;静态方法在加载类时就已经被加载了,所以可以直接使用; 实例化类一般是要创建内存区域,而常量一创建就放在常量区,故不需要实例去访问它,也就是说不需要跟着实例进行实例化了。

C# 字符串分割

https://blog.csdn.net/caoguanghui0804/article/details/116584639

using System.Text.RegularExpressions;

用字符串分割

string str="aaajsbbbjsccc";

string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase); //分割结果为aaa bbb ccc

用多个字符串分割

string str="aaajbbbscccjdddseee";

string[] sArray=str.Split(new char[2]{'j','s'}); //分割结果为aaa bbb ccc ddd eee

用单字符分割

string str="aaajbbbjccc";

string[] sArray=str.Split('j'); //分割结果为 aaa bbb ccc

C#中删除字符串最后一个字符串的几种方式

http://t.zoukankan.com/hubbert123-p-10333732.html

使用Remove移除

string a = "a,b,c,d,e,f,";

a = a.Remove(a.Length-1, 1);

使用Substring进行截取

string a = "a,b,c,d,e,f,";

a = a.Substring(0, a.Length - 1);

C#点击关闭按钮 弹出确认框

https://blog.csdn.net/I_AM_KK/article/details/123106744?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-123106744-blog-126733132.pc_relevant_vip_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-123106744-blog-126733132.pc_relevant_vip_default&utm_relevant_index=2

方法一:使用代码

private void Form1_Load(object sender, EventArgs e)

{

this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);//注册窗体关闭事件 拼多多帮助力入口

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)//窗体关闭响应函数

{

//让用户选择点击

DialogResult result = MessageBox.Show("是否确认关闭?", "警告",

MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

if (result == DialogResult.No)//判断是否取消事件

{

e.Cancel = true;//取消退出

}

}

方法二:设置窗体属性

private void Form1_Load(object sender, EventArgs e)

{

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)//窗体关闭响应函数

{

//让用户选择点击

DialogResult result = MessageBox.Show("是否确认关闭?", "警告",

MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

if (result == DialogResult.No)//判断是否取消事件

{

e.Cancel = true;//取消退出

}

}

内容解释

MessageBoxButtons.YesNo是“是 否”按钮的类型

MessageBoxButtons.OKCancel是“确认 取消”

可随便写

MessageBoxIcon.Question是疑问的图标

MessageBoxIcon.Warning是警告的图标

可以随便改

c# progressBar进度条改变颜色及显示进度数字

https://blog.csdn.net/czcl123/article/details/72782961?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-72782961-blog-113663484.pc_relevant_vip_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-72782961-blog-113663484.pc_relevant_vip_default&utm_relevant_index=2

namespace WindowsFormsApp1

{

public partial class MyProgressBar : ProgressBar

{

public static SolidBrush brush = new SolidBrush(Color.White);

public MyProgressBar()

{

InitializeComponent();

base.SetStyle(ControlStyles.UserPaint, true);

}

//重写OnPaint方法

protected override void OnPaint(PaintEventArgs e)

{

Rectangle rec = e.ClipRectangle;

rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;

if (ProgressBarRenderer.IsSupported)

ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle);

rec.Height = rec.Height - 4;

e.Graphics.FillRectangle(brush, 2, 2, rec.Width, rec.Height);

}

}

}

MyProgressBar myProgressBar = new MyProgressBar();//重新定义的继承自ProgressBar的类,用于改变进度条的颜色

int Battery = 8300;

private void button1_Click(object sender, EventArgs e)

{

if (Battery >= 7900)

{

brush = new SolidBrush(Color.Green);

}

else if (Battery >= 7600)

{

brush = new SolidBrush(Color.Yellow);

}

else

{

brush = new SolidBrush(Color.Red);

}

this.myProgressBar.Value = Battery - 7300;

}

private void Form1_Load(object sender, EventArgs e)

{

myProgressBar.Parent = progressBar1;

myProgressBar.Maximum = 1000;

myProgressBar.Minimum = 0;

myProgressBar.Value = 0;

myProgressBar.Width = progressBar1.Width;

myProgressBar.Height = progressBar1.Height;

progressBar1.Controls.Add(myProgressBar);

}

相关代码我放在了CSDN的资源里面,《C# 改变progressBar进度条的颜色》

C#一个窗体读取另一个窗体上控件的内容

https://www.cnblogs.com/Fpack/p/15334859.html

在Form2中调用Form1的内容,则在Form2中改变以下代码:

private Form1 form1;

public Form2(Form1 parent)

{

InitializeComponent();

form1 = parent;

}

C#的File 类

C#的File 类使用说明

C#文件流FileStream

C#文件流FileStream的用法

C#文件操作之FileStream类详解

简介

FileStream 类:主要用于对文件进行读取、写入、打开和关闭操作,并对其他与文件相关的操作系统句柄进行操作,如管道、标准输入和标准输出。读写操作可以指定为同步或异步操作。FileStream 对输入输出进行缓冲,从而提高性能。——MSDN

简单点说:FileStream类可以对任意类型的文件进行读取操作,可根据需要来指定每次读取的字节长度(这是比File类优秀的地方之一),以此减少内存的消耗,提高读取效率。

File和FileStream的区别

File是一个静态类;FileStream是一个非静态类。

最直接区别:将读取文件比作是从A桶往B桶运水。使用File就是整个用桶倒进去,使用FileStream就是使用水管慢慢输送,应用场景不同,大文件推荐FileStream,不会炸内存。

File:是一个文件的类,对文件进行操作。其内部封装了对文件的各种操作(MSDN:提供用于创建、复制、删除、移动和打开单一文件的静态方法,并协助创建FileStream对象)。

FileStream:是一个文件流的类,处理文件的原始字节,即处理byte[]。对txt,xml,avi等任何文件进行内容写入、读取、复制…

常用构造函数介绍

最简单的构造函数只有两个参数,即文件名和FileMode枚举值

FileStream(fliename , FileMode.)

另一个常用构造函数则有三个参数,第三个参数是FileAccess枚举的一个成员,它指定了流的作用

FileStream(fliename , FileMode.,FileAccess.)

FileMode枚举成员:

成员文件存在文件不存在Append打开文件,流指向文件的末尾处,只能与枚举FileAccess.Write结合使用创建一个新文件。只能与枚举FileAccess.Write结合使用Create删除该文件,然后创建新文件创建新文件CreateNew抛出异常创建新文件Open打开文件,流指向文件开头处抛出异常OpenOrCreate打开文件,流指向文件开头处创建新文件Truncate打开文件,清除其内容。流指向文件开头处,保留文件的初始创建日期抛出异常

FileAccess枚举成员:

成员说明Read打开文件,用于只读Write打开文件,用于只写ReadWrite打开文件,用于读写

将一个数组写入文件

private void WriteDataToFile_L(int[] data, FileStream OutFileStream)//左腿数据写入函数

{

int i = 0;

for (; i < data.Length - 1; i++)

{

byte[] val = Encoding.UTF8.GetBytes(data[i].ToString() + ",");

if (OutFileStream != null)

OutFileStream.Write(val, 0, val.Length);

else

break;

}

byte[] lastVal = Encoding.UTF8.GetBytes(data[i].ToString() + "\r\n");

if (OutFileStream != null)

OutFileStream.Write(lastVal, 0, lastVal.Length);

}

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧