如何在“多行文本框”中删除选中的行

本文关键字:删除 多行文本框 文本 | 更新日期: 2023-09-27 18:08:41

下面的代码我使用"SelectedLines"来查找用户当前选择的行,AllLines来查找多行文本框中的总行数。在最后一个for循环中,当i=1时,它运行成功但当i= +1 = 2时,它给出错误

Error: "Index out of range. "必须非负且小于集合的大小。参数名称:index?"

 // Retrieve selected lines
            List<string> SelectedLines = Regex.Split(txtNewURLs.SelectedText, @"'r'n").ToList();
            // Check for nothing, Regex.Split returns empty string when no text is inputted
            if (SelectedLines.Count == 1)
            {
                if (String.IsNullOrWhiteSpace(SelectedLines[0]))
                {
                    SelectedLines.Remove("");
                }
            }
            // Retrieve all lines from textbox
            List<string> AllLines = Regex.Split(txtNewURLs.Text, @"'r'n").ToList();
            // Check for nothing, Regex.Split returns empty string when no text is inputted
            if (AllLines.Count == 1)
            {
                if (String.IsNullOrWhiteSpace(AllLines[0]))
                {
                    AllLines.Remove("");
                }
            }
            string SelectedMessage = "The following lines have been selected";
            int numSelected = 0;
            // Find all selected lines
            foreach (string IndividualLine in AllLines)
            {
                if (SelectedLines.Any(a => a.Equals(IndividualLine)))
                {
                    SelectedMessage += "'nLine #" + AllLines.FindIndex(a => a.Equals(IndividualLine));
                   // changing the status of the selected lene from 0 to 1
                    AddURL objAddURL = new AddURL();
                    objAddURL.Where.SURL.Value = IndividualLine;
                    objAddURL.Where.ILicenseID.Value = CommonMethods.iLicenseID;
                    objAddURL.Query.Load();
                    if (objAddURL.RowCount > 0)
                    {
                        AddURL objaddurl1 = new AddURL();
                        objaddurl1.LoadByPrimaryKey(objAddURL.IAddURLID);
                        if (objaddurl1.RowCount > 0)
                        {
                            objaddurl1.IStatus = 1;
                            objaddurl1.Save();
                        }
                      //  AllLines.Remove(IndividualLine);
                    }
                    numSelected++;
                }
            }
            // int[] lineNo = new int[AllLines.Count];
            int linesNO = SelectedLines.Count;
            for (int i = 1; i <= linesNO; i++)
            {
                SelectedLines.RemoveAt(i);
            }
           // MessageBox.Show((numSelected > 0) ? SelectedMessage : "No lines selected.");

有没有人可以帮我解决这个问题或者建议我新的代码?

如何在“多行文本框”中删除选中的行

c#中的索引是从零开始的。

for (int i = 0; i < linesNO; i++)
    SelectedLines.RemoveAt(i);

但是如果你想从你的文本框中删除选定的行,你的代码应该看起来像

 List<string> SelectedLines = new List<string> { "b", "c" };
 textBox1.Lines = textBox1.Lines.ToList().Except(SelectedLines).ToArray();