private void Delete_last_Click(object sender, EventArgs e) { if(i==0) { MessageBox.Show("请至少写入一组数据再删除最后一行"); } else { int start = textBox7.GetFirstCharIndexFromLine(i - 1);//第i-1行第一个字符的索引 int end = textBox7.GetFirstCharIndexFromLine(i);//第i行第一个字符的索引 textBox7.Select(start, end);//选中第i-1行 textBox7.SelectedText = "";//设置第i-1行的内容为空 for (int k = 0; k < Obedient.GetLength(1); k++) { Obedient[i - 1, k] = 1; } i--; } } 如何修改这段代码 设置i-1、i-2、i-3这三行的内容为空
时间: 2024-03-26 10:39:06 浏览: 21
可以将代码中的`int start = textBox7.GetFirstCharIndexFromLine(i - 1);`改为`int start = textBox7.GetFirstCharIndexFromLine(i - n);`,其中n可以取1、2、3分别代表i-1、i-2、i-3。然后将`textBox7.SelectedText = "";`改为`textBox7.Select(start, textBox7.GetFirstCharIndexFromLine(i - n + 1) - start); textBox7.SelectedText = "";`即可。完整代码如下:
```
private void Delete_last_Click(object sender, EventArgs e)
{
if (i == 0)
{
MessageBox.Show("请至少写入一组数据再删除最后一行");
}
else
{
int n = 1; // 要删除的行数,可以取1、2、3
int start = textBox7.GetFirstCharIndexFromLine(i - n);
int end = textBox7.GetFirstCharIndexFromLine(i);
textBox7.Select(start, textBox7.GetFirstCharIndexFromLine(i - n + 1) - start);
textBox7.SelectedText = "";
for (int k = 0; k < Obedient.GetLength(1); k++)
{
Obedient[i - n, k] = 1;
}
i -= n;
}
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)