[英]Updating a section of RichTextBox C#
我正在處理一個非常簡單的定單,它將通過標簽打印機進行打印。 我有一個richTextBox
來顯示已添加的項目以及有多少項目,但是我不知道是如何編輯文本框的“部分”,即添加了同一項目的多個項目。
我目前有2個按鈕,其中1個用於雞肉,一個用於鮭魚。
private void button1_Click(object sender, EventArgs e)
{
chickenCount++;
richTextBox1.Text = richTextBox1.Text + "\nChicken " + chickenCount + "x";
}
和
private void button3_Click(object sender, EventArgs e)
{
salmonCount++;
richTextBox1.Text = richTextBox1.Text + "\nSalmon " + salmonCount + "x";
}
假設您先添加1只雞,再添加1條三文魚,然后再添加另一只雞。
使用此代碼,輸出將是:
Chicken 1x
Salmon 1x
Chicken 2x
現在我知道這段代碼只是不斷在richTextBox
的下一行上添加文本
但是我想要的輸出是:
Chicken 2x
Salmon 1x
但是我不太確定如何在richTextBox
內聯編輯一些richTextBox
您可以使用SelectedText
屬性和Select
方法替換文本框中的一行。 例如:
int line = 0; //The line that we want to replace is the first one
string newstr = "Chicken x2"; //We want to replace the line with this string
int charindex = this.GetFirstCharIndexFromLine(line); //Get the index of the first character of the line
richTextBox1.Select(charindex, richTextBox1.Lines[line].Length); //Select the line
richTextBox1.SelectedText = newstr; //Replace its text with the string
如果您不知道要替換的Lines
,請遍歷Lines
數組以搜索文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.