简体   繁体   中英

How to check selected text in multi line text Box in C#?

I have two Multi-Line Text Boxes and one arrow button in my application and what I want is that when a user selects any one or many lines from Multi-Line textbox 1 ,it should update the status of that line from 0 to 1 and then I want to load the rows whose status is 1 into Multi-Line textbox 2.I have tried but didn't know what should I do next?

Code :

for (int i = 0; i < txtNewURLs.Lines.Length; i++)
{
    if (txtNewURLs.Lines[i].Select)
    {

    }
}

Can any body please help me or give some suggession to do this task?

Assuming you are using a Multiline TextBox similar to MSDNS's How to: Create a Multiline TextBox Control , you can utilize the SelectedText property to retrieve the text that the user has selected. The lines will be separated by \\r\\n

ie

If I have the below (inbetween the page lines):


test0

test1


And I selected lines test0 and test1 , then SelectedText would be test0\\r\\ntest1 .

You could then split on the \\r\\n and retrieve each selected line.

// 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));
        // Assuming you store each line status in an List, change status to 1
        LineStatus[AllLines.FindIndex(a => a.Equals(IndividualLine));] = 1;
       numSelected++;
    }
}

MessageBox.Show((numSelected > 0) ? SelectedMessage : "No lines selected.");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM