假设您正在使用类似于 MSDNS 的如何:创建多行文本框控件的多行文本框,您可以利用SelectedText属性来检索用户选择的文本。这些行将由\r\n
IE
如果我有以下内容(在页面行之间):
测试0
测试1
我选择了行test0
和test1
,然后SelectedText
是test0\r\ntest1
。
然后,您可以拆分\r\n
并检索每个选定的行。
// 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.");