2016-09-14 12 views
0

이 코드를 사용하여 richtextbox에서 줄을 하나씩 지우지 만 빈 줄 (공백)은 남겨 둡니다.Richtextbox에서 줄을 하나씩 제거하는 방법 C#

var text = "";//Holds the text of current line being looped. 
var startindex = 0;//The position where selection starts. 
var endindex = 0;//The length of selection. 

for (int i = 0; i < richtextbox1.Lines.Length; i++)//Loops through each line of text in RichTextBox 
{ 
    text = richtextbox1.Lines[i]; //Stores current line of text. 

    startindex = richtextbox1.GetFirstCharIndexFromLine(i); 
    endindex = text.Length; 
    richtextbox1.Select(startindex, endindex); 
    MessageBox.Show(richtextbox1.SelectedText); 
    richtextbox1.SelectedText = "";    
} 

빈 줄 (공백)없이 하나씩 줄을 어떻게 지울 수 있습니까?

+0

샘플 출력과 동일한 샘플 입력을 제공 할 수 있습니까? 나는 네가 무엇을 요구하는지 확신하지 못한다. – KSib

+0

@KSib 샘플 입력 : email1dotcom email2dotcom email3dotcom 샘플 출력 : 이 (공간) (공간) (공간) –

+0

아, 내가 지금 무슨 말을하는지 참조하십시오. 신경 쓰지 마. – KSib

답변

0
// Gets the number of newline characters in your rich text box 
var numberOfNewLines = richTextBox1.Text.Count(r => r == '\n'); 

for (var i = 0; i < numberOfNewLines; i++) 
{ 
    // Finds the first occurance of the newline character 
    var newlineCharacterIndex = richTextBox1.Text.IndexOf('\n') + 1; 

    // Replaces the rich textbox text with everything but the above line 
    richTextBox1.Text = richTextBox1.Text.Substring(newlineCharacterIndex); 

    MessageBox.Show("OK!"); 
} 

// Removes the final line. 
richTextBox1.Text = string.Empty; 

나는 당신이 올바른 길을 가고 있다고 생각하지만, 당신이하는 방식은 단지 선 자체가 아니라 선의 내용을 제거하는 것이 었습니다.

+0

이 코드는 작동하지만 공백없이 richtextbox1.SelectedText를 삭제하려고합니다. –

관련 문제