作业帮 > 综合 > 作业

帮我注释一下每行代码,就是解释下每行的意思,谢谢了

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/25 00:09:21
帮我注释一下每行代码,就是解释下每行的意思,谢谢了
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Text
{
public partial class Form1 : Form
{
private static readonly Regex _cRegex = new Regex("[\u4e00-\u9fa5]");

public Form1()
{
InitializeComponent();
Tb.TextChanged += Tb_TextChanged;
}

void Tb_TextChanged(object sender, EventArgs e)
{
var cCounter = 0;
foreach (var c in Tb.Text)
{
if (!isChineseChar(c)) continue;
cCounter++;
}
TbChineseCounter.Text = cCounter.ToString("n0");
TbTotalCounter.Text = Tb.Text.Length.ToString("n0");
}

static bool isChineseChar(char ch)
{
return _cRegex.IsMatch(ch.ToString(CultureInfo.InvariantCulture));
}
}
}
下面四行是引用系统的命名空间
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;
声明对象所在的命名空间
namespace Text
{
声明对象:对象名是Form1,继承基类Form
public partial class Form1 :Form
{
声明静态只读变量并实例化
private static readonly Regex _cRegex = new Regex("[\u4e00-\u9fa5]");
对象的构造方法
public Form1()
{
这个是系统生成的方法,一般不用理会
InitializeComponent();
声明控件对象Tb的TextChanged事件,事件调用方法Tb_TextChanged(在下面)
Tb.+= Tb_TextChanged;
}
控件Tb的TextChanged 事件所执行的方法
void Tb_TextChanged(object sender,EventArgs e)
{
定义整形变量
var cCounter = 0;
循环Tb对象的Text属性的每一个字符
foreach (var c in )
{
执行isChineseChar并对其返回值进行判断,如果返回false则执行下一次循环
if (!isChineseChar(c)) continue;
cCounter加1
cCounter++;
}
给TbChineseCounter控件的Text属性赋值
TbChineseCounter.Text = cCounter.ToString("n0");
给TbTotalCounter控件的Text属性赋值
TbTotalCounter.Text = Tb.Text.Length.ToString("n0");
}
定义静态方法
static bool isChineseChar(char ch)
{
调用 _cRegex对象的IsMatch方法并返回其值
return _cRegex.IsMatch(ch.ToString(CultureInfo.InvariantCulture));
}
}
}
亲,给分吧,这可是辛苦些 的,没地儿抄去