我的问题如下:我想以编程方式向 RichTextBox 添加文本,但是当我这样做时,我的 RichTextBox 保持为空(但是,当我一步步执行时,我会看到我在行中添加的文本)。
我检查了 RichTextBox 的所有属性都正常(MultiLines、Visible、WordWrap,...)。我尝试在 AppendText 之后调用以下方法:ScrollToCaret、Focus、Invalidate。
但它没有改变任何东西,我什至删除了我的 RichTextBox 来尝试另一个同样的问题。
这是相关方法的代码:
public void checkBox_Click(object sender, EventArgs e)
{
CheckBox checkBox = sender as CheckBox;
string attribut = AttributsController.GetAttributByName(checkBox.Name.Substring(4));
if(checkBox.Checked)
{
// FR : Devrait ajouter le texte
// EN : Should append text
rchAttributs.AppendText(attribut + Environment.NewLine); // Problem here
}
else if(!checkBox.Checked)
{
// FR : Récupération de l'index de la ligne à supprimer
// EN : Retrieve the index of the line to be deleted
int indexToDelete = Utils.GetLineNumberToDelete(attribut, rchAttributs);
// FR : On récupère toutes les lignes sous la forme d'une liste
// EN : All rows are retrieved in the form of a list
List<string> lines = new List<string>(rchAttributs.Lines);
// FR : On supprime la ligne où l'on a trouvé le texte correspondant
// EN : On supprime la ligne où l'on a trouvé le texte correspondan
lines.RemoveAt(indexToDelete);
// FR : On réattribue les nouvelles lignes à celles de la RichTextBox
// EN : Reassign the new lines to those in the RichTextBox
rchAttributs.Lines = lines.ToArray();
}
}
GetLineNumberToDelete 方法似乎没有按预期工作,并且返回了错误的索引。同时您可以尝试使用此代码来删除该行:
lines.Remove(attribut);