Saeed Khan
Saeed Khan

Reputation: 537

How can I delete the selected Lines in Multi line Text Box?

My code below here I used "SelectedLines" to find the current selected lines by user, AllLines to find the no of total lines in Multi line text box. in last for loop when i=1 it runs successfully but when i is +1 = 2 then it gives me error

Error : "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: 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.");

Could any body please help me to solve this issue or suggest me new code?

Upvotes: 1

Views: 4398

Answers (1)

user1064248
user1064248

Reputation:

Indices in c# are zero based.

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

But if you want to delete the selected lines from your textBox your code should rather look like

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

Upvotes: 2

Related Questions