2013-04-11 22 views
2

我在我的應用程序中有兩個多行文本框和一個箭頭按鈕,我想要的是當用戶從多行文本框1中選擇任意一行或多行時,它應該將該行的狀態從0更新爲1,然後我想將狀態爲1的行加載到Multi-Line文本框2.我已經嘗試過,但不知道接下來應該做什麼?如何檢查C#中多行文本框中的選定文本?

代碼

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

    } 
} 

任何機構可以幫我或給一些suggession做這個任務?

+2

您可以使用列表框在這種情況下 – Arshad 2013-04-11 11:39:01

+0

是你的0和1的虛擬更好地解釋你想要什麼或者你有應被設置爲0和1各行中的變量? – 2013-04-11 11:45:21

+0

這是一個SelectionChanged甚至會很方便的地方,但我敢肯定,文本框不存在。不過,您可以創建一個自定義控件來執行此操作。 – metalhead 2013-04-11 12:16:31

回答

1

假設您使用與MSDNS的How to: Create a Multiline TextBox Control類似的多行文本框,可以使用SelectedText屬性檢索用戶選擇的文本。該線將通過\r\n

分開即

如果我有以下(插圖中的頁面線):


TEST0

test1的


我選定的行test0test1,那麼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."); 
+0

我只是評論這行// LineStatus [AllLines.FindIndex(a => a.Equals(IndividualLine));] = 1;我將其狀態從數據庫從0改爲1.感謝bob解決了我目前的問題 – 2013-04-12 05:52:03

相關問題