如题,怎么做到在多行textbox里把上一行的内容删除?C#windows窗体应用程序...
如题,怎么做到在多行textbox里把上一行的内容删除?
C# windows窗体应用程序
int record1,record2;
private void button1_Click(object sender, EventArgs e)
{
record1=textBox1.SelectionStart;
record3=textBox1.SelectionLength;
int index = CurrentLine()-1;
if (index >= 0)
{
record2 = textBox1.Lines[index].Length + 2;
int count = 0;
String[] lines = new String[textBox1.Lines.Length - 1];
for (int i = 0; i < textBox1.Lines.Length; i++)
{
if (i != index)
{
lines[count] = textBox1.Lines[i];
count++;
}
}
textBox1.Lines = lines;
}
else record2 = 0;
textBox1.Focus();
textBox1.SelectionStart = record1 - record2;
textBox1.SelectionLength = 0;
}
private int CurrentLine()
{
int position = textBox1.SelectionStart;
for (int i = 0; i < textBox1.Lines.Length; i++)
{
position -= textBox1.Lines[i].Length;
if (position == 0)
{
return i;
}
if (position >= 2 && position <= textBox1.Lines[i + 1].Length+2)
{
return i + 1;
}
position -= 2;
}
return textBox1.Lines.Length;
}
这个是把上一行彻底删除,包括回车符,CurrentLine函数用来获取当前光标在哪行,如果要删成空白,就把
if (i != index)
{
lines[count] = textBox1.Lines[i];
count++;
}
改成
if (i != index)
{
lines[count] = textBox1.Lines[i];
count++;
}
else
{
lines[count] = "";
count++;
}
就可以了