Here is the search method for strings in two forms:

private void findWordInVoc_Click(object sender,EventArgs e) { int index = 0; string text = ""; string temp = ""; string foundWord = Properties.Settings.Default.foundWord; RichTextBox rtx = new RichTextBox(); for (int i = 0; i < 2; i++) { if (i == 0) //форма вывода сгенерированных слов { rtx = rtxtbox_output; text = rtxtbox_output.Text; temp = text; text = ""; text = temp; } else if (i == 1)//форма загрузки с файла словаря { rtx = rtxtbox_loadFromVocFile; text = rtxtbox_loadFromVocFile.Text; temp = text; text = ""; text = temp; } if (!string.IsNullOrEmpty(foundWord)) { while (index < text.LastIndexOf(foundWord)) { rtx.Find(foundWord,index, rtx.TextLength,RichTextBoxFinds.None); rtx.SelectionBackColor = SystemColors.Control; index = text.IndexOf(foundWord,index) + 1; } } index = 0; while (index < text.LastIndexOf(txtbox_input.Text)) { rtx.Find(txtbox_input.Text,index, rtx.TextLength,RichTextBoxFinds.None); rtx.SelectionBackColor = Color.Yellow; index = text.IndexOf(txtbox_input.Text,index) + 1; } } rtx.ScrollToCaret(); Properties.Settings.Default.foundWord = txtbox_input.Text; } 

The problem is that the first word does not stand out in the richtextbox - neither in the first nor in the second. Starting from the second, everything is going fine. What did I miss. Maybe there is a problem in the code that writes the string to the form? Here is the method that generates the string:

  public string Main( string txtbox_input_text, string txtbox_comment_text, int cbb_grammarType_index) { string sylStructure = ""; string wordStr = null; string wordType = null; if (cbb_grammarType_index == 3) { sylStructure = SylStructRandom(true); } else if (cbb_grammarType_index == 0 || cbb_grammarType_index == 1 || cbb_grammarType_index == 2) { sylStructure = SylStructRandom(false); if (sylStructure == "cv") { int p = rGen.Next(1,4);//шанс 1 из 3, что согласная будет j if (p == 1) sylStructure += "jv"; } else { if (DefineFullMeaningWordsSylAmount() != 1) sylStructure = SylStructRandom(false); } } sylStructure = DeleteThirdCons(sylStructure); wordStr = ConvertSylStructToWordStr(sylStructure); switch (cbb_grammarType_index) { case 0: wordType = "n"; break; case 1: wordType = "v"; break; case 2: wordType = "a"; break; case 3: wordType = "p"; break; } if (!string.IsNullOrEmpty(txtbox_comment_text)) txtbox_comment_text = " (" + txtbox_comment_text + ")"; return txtbox_input_text + " " + wordStr + " " + wordType + txtbox_comment_text + "\r\n"; } 

But what causes the previous one and writes the result in the form:

 void GenerateAndOutput() { WGen wg = new WGen(); rtxtbox_output.Text+= wg.Main( txtbox_input.Text, txtbox_comment.Text, cbb_grammarType.SelectedIndex); txtbox_input.Clear(); txtbox_comment.Clear(); txtbox_input.Focus(); } 

Please tell me what I missed. It seems to be a trifle, but very striking

Examples: The first word in the richtextbox is not highlighted

the second stands out

third too

  • in general, this is not a question, but a veiled request for you to debug the code. Do it yourself, put a breakpoint in the right place and go through the steps of the execution code, see the values ​​of the desired variables and find where it is wryly written. - Bulson
  • And thanks for that - Alexander Berezyuk

0