假設您使用與MSDNS的How to: Create a Multiline TextBox Control類似的多行文本框,可以使用SelectedText屬性檢索用戶選擇的文本。該線將通過\r\n
分開即
如果我有以下(插圖中的頁面線):
TEST0
test1的
我選定的行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.");
您可以使用列表框在這種情況下 – Arshad 2013-04-11 11:39:01
是你的0和1的虛擬更好地解釋你想要什麼或者你有應被設置爲0和1各行中的變量? – 2013-04-11 11:45:21
這是一個SelectionChanged甚至會很方便的地方,但我敢肯定,文本框不存在。不過,您可以創建一個自定義控件來執行此操作。 – metalhead 2013-04-11 12:16:31