using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication26
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string cw; string wc;
public void button1_Click(object sender, EventArgs e)
{
readtextfromfile();
}
void readtextfromfile()
{
OpenFileDialog o = new OpenFileDialog();
if (o.ShowDialog() == DialogResult.OK)
{
string file = o.FileName;
string ext = file.Substring(file.LastIndexOf("."));
if (ext.ToLower() != ".txt")
{
MessageBox.Show("bushiwenjian");
return;
}
else
{
richTextBox1.Text = File.ReadAllText(o.FileName, Encoding.Default);
}
}
this.cw = richTextBox1.Text;
}
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
int start = richTextBox2.SelectionStart;
int m = richTextBox1.TextLength > richTextBox2.TextLength ? richTextBox2.TextLength : richTextBox1.TextLength;
for (int i = 0; i < m; i++)
{
if (richTextBox1.Text[i] != richTextBox2.Text[i])
{
richTextBox2.Select(i, 1);
richTextBox2.SelectionColor = Color.Red;
}
}
if (richTextBox2.TextLength > richTextBox1.TextLength)
{
richTextBox2.Select(richTextBox1.TextLength, richTextBox2.TextLength - richTextBox1.TextLength);
richTextBox2.SelectionColor = Color.Red;
}
richTextBox2.SelectionStart = start;
richTextBox2.SelectionColor = richTextBox1.ForeColor;
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox2.SelectAll();
this.wc = richTextBox2.Text;
string[] cc; string[] ww;
cc = cw.Split(' ', '.', ',');
ww = wc.Split(' ', '.', ',');
int j = 0; double t;
for (int i = 0; i < 10; i++)
{
bool result
;result = Equals(cc[i], ww[i]);
if (result==true)
{
j++;
}
}
t = (double)j / 10;
string tText = t.ToString("0.0%");
label1.Text = tText;
}
}
}
红色部分运行出现索引超出数组界限