Reputation: 75
I have this code and it's applied to a form. For every word in the richtextbox it looks if it's present in a txt file that i'm using as a dictionary and if not it changes the color of that word to red. I know the code opens and closes the stream for every word and I'm fixing that soon.
private void sottolinea_errori_Click(object sender, EventArgs e)
{
string line;
string[] linea = TextBox_stampa_contenuto.Lines;
if (TextBox_stampa_contenuto.Text != "")
{
foreach (string k in linea)
{
string[] parole = k.Split(new Char[] { ' ', ',', '.', ':', '\t' });
foreach (string s in parole)
{
Regex rgx = new Regex(@"\d");
if (!rgx.IsMatch(s))
{
if (s.Trim() != "")
{
s.Trim();
string path = @"280000parole.txt";
bool esito = true;
StreamReader file = new StreamReader(path);
while ((line = file.ReadLine()) != null && esito == true)
if (string.Compare(line, s) == 0)
esito = false; // i put this false when I find the word in the dictionary file
file.Close();
if (esito) //if true means that the word wasn't found
{
int inizioParola=0, indice; //indice means index, inizio parola is for selectionStart
while ((indice = TextBox_stampa_contenuto.Text.IndexOf(s, inizioParola)) != -1)
{
TextBox_stampa_contenuto.Select(indice, s.Length);
inizioParola = indice + s.Length;
}
TextBox_stampa_contenuto.SelectionStart = inizioParola - s.Length;
TextBox_stampa_contenuto.SelectionLength = s.Length;
TextBox_stampa_contenuto.SelectionColor = Color.Red;
}
TextBox_stampa_contenuto.SelectionLength = 0;
}
}
}
}
}
}
The problems are:
If you can help me I'll really appreciate!
Upvotes: 0
Views: 862
Reputation: 2734
Here is a working example, added caching of the words so you don't read the file each time, trimmed all the words, put all the words to lower case and optimized the logic. I have added the comments to make the code easy to read and understand.
WordList.txt Contains
Apple
Banana
Computer
private void sottolinea_errori_Click(object sender, EventArgs e)
{
// Get all the lines in the rich text box
string[] textBoxLines = this.TextBox_stampa_contenuto.Lines;
// Check that there is some text
if (this.TextBox_stampa_contenuto.Text != string.Empty)
{
// Create a regular expression match
Regex rgx = new Regex(@"\d");
// Create a new dictionary to hold all the words
Dictionary<string, int> wordDictionary = new Dictionary<string, int>();
// Path to the list of words
const string WordListPath = @"WordList.txt";
// Open the file and read all the words
StreamReader file = new StreamReader(WordListPath);
// Read each file into the dictionary
int i = 0;
while (!file.EndOfStream)
{
// Read each word, one word per line
string line = file.ReadLine();
// Check if the line is empty or null and not in the dictionary
if (!string.IsNullOrEmpty(line) && !wordDictionary.ContainsKey(line))
{
// Add the word to the dictionary for easy lookup add the word to lower case
wordDictionary.Add(line.ToLower(), i);
// Incrament the counter
i++;
}
}
// Close the file
file.Close();
// For each line in the text box loop over the logic
foreach (string textLine in textBoxLines)
{
// Split the text line so we get individual words, remove empty entries and trim all the words
string[] words = textLine.Split(new char[] { ' ', ',', '.', ':', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim().ToLower()).ToArray();
// For each word that does not contain a digit
foreach (string word in words.Where(x => !rgx.IsMatch(x)))
{
// Check if the word is found, returns true if found
if (!wordDictionary.ContainsKey(word))
{
// Initialize the text modification variables
int wordStartPosition, seachIndex = 0;
// Find all instances of the current word
while ((wordStartPosition = this.TextBox_stampa_contenuto.Text.IndexOf(word, seachIndex, StringComparison.InvariantCultureIgnoreCase)) != -1)
{
// Select the word in the text box
this.TextBox_stampa_contenuto.Select(wordStartPosition, word.Length);
// Set the selection color
this.TextBox_stampa_contenuto.SelectionColor = Color.Red;
// Increase the search index after the word
seachIndex = wordStartPosition + word.Length;
}
}
}
}
}
}
Upvotes: 1