我对C#
很新。我通过实验学到了最好的东西,但当然,有时候我会完全陷入困境。我将尝试用我目前掌握的编程语言知识来尽我所能地解释我的问题。
我一直在尝试创建一个简单的工具来编辑/添加文本行到text file
。我做了很多研究,特别是在这个网站上,所有的信息都非常有用。我的问题是,在multi-line textbox
内的单行文本中向两侧添加文本。
所以我想说我有textbox
现有2条线;我想在一行中的一行旁边添加一些文本,并对下一行执行相同的操作。下面是一个按钮被击中之前和之后文本的示例:
之前
与
不同后
A与B不同于B
“Before”中的两行将在textBox1(多行)中,并将作为“After”插入richTextBox1。
希望我已经清楚地解释了它,我不知道从哪里开始。
谢谢。
答案 0 :(得分:2)
如果您知道必须更新文本index
,那么您应该可以使用insert
类
string
函数直接插入值
示例:强> 的
//Get the text box value
var formatedTextboxString = this.textbox1.Text;
formatedTextboxString = formatedTextboxString.Insert(0, "A ");
formatedTextboxString = formatedTextboxString.Insert(21, "B");
//Place the formated text back to the richTextBox
this.richTextBox1.Text = formatedTextboxString;
答案 1 :(得分:1)
尝试
"{0} is not the same as {1} {2} is different than {3}"
在textbox1中。然后使用:
textbox2.Text = String.Format(textbox1.Text, A, B, A, B);
答案 2 :(得分:0)
如果您有多行文本框:
var list = new List<string>(textBox1.Lines);
for (int i = 0; i < list.Count; ++i)
{
list[i] = "A" + list[i] + "B";
}
textBox1.Lines = list.ToArray();
答案 3 :(得分:0)
string[] arr = textBox1.Text.Split(Environment.Newline);
//then loop over each line and add whatever you want :-
foreach (string s in arr)
{
//add here
}
我想这是一个很好的提示,从以下开始:)