2013-04-11 3 views
2

응용 프로그램에 두 개의 멀티 라인 텍스트 상자와 하나의 화살표 버튼이 있습니다. 사용자가 멀티 라인 텍스트 박스 1에서 하나 또는 여러 개의 라인을 선택하면, 그 라인의 상태가 0에서 1을 누른 다음 상태가 1 인 행을 다중 행 텍스트 상자에로드하려고합니다. 2. 시도했지만 다음에 무엇을해야하는지 몰랐습니까?C#의 여러 줄 텍스트 상자에서 선택한 텍스트를 확인하는 방법?

코드 :

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

    } 
} 

어떤 몸이 좀 도와 또는이 작업을 수행하는 몇 가지 suggession을주지하시기 바랍니다 수 있습니까?

+2

당신은 – Arshad

+0

이 0과 더 나은 당신이 원하는 또는 각 라인 0과 1로 설정해야합니다 변수를해야합니까 무엇인지 설명에만 가상 1이 시나리오에서 목록 상자를 사용할 수 있습니까? 인 selectionchanged도 편리 할 것입니다 곳 –

+0

이다, 그러나 나는 텍스트 상자에 존재하지 않는 확신 해요. 이 작업을 수행 할 수있는 사용자 지정 컨트롤을 만들 수 있습니다. 이 목록 상자 컨트롤을 사용해보십시오 – metalhead

답변

1

MSDNS의 How to: Create a Multiline TextBox Control과 유사한 다중 텍스트 상자를 사용한다고 가정하면 SelectedText 속성을 사용하여 사용자가 선택한 텍스트를 검색 할 수 있습니다. 내가 (페이지 라인 inbetween) 아래의 경우 선은

\r\n

로 구분됩니다


test0

TEST1


그리고 선택한 라인 test0 및(3210), 다음 SelectedTexttest0\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로 변경했습니다. 감사합니다. 현재 문제를 해결했습니다. –

관련 문제