2017-08-09 57 views
1

我都開在RichTextBox.I .CSV文件中添加的每一行的第一個字CombobBox項目替換它。我想編輯一個特定的單詞,然後將其保存迴文件。讀取文本文件並在richtextbox中顯示它。顯示自RichTextBox的一個字,組合框,然後發現它在的文字與TextBox.text

這是我打開文件richTextBox1。

 OpenFileDialog ofd = new OpenFileDialog(); 
     ofd.Filter = "Csv files (.csv)|*.csv"; 
     ofd.Title = "Open a file..."; 
     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      StreamReader sr = new StreamReader(ofd.FileName); 
      richTextBox1.Text = sr.ReadToEnd(); 
     } 

現在我想的是我發現在comboBox1.Text RichTextBox的和與txtbox.Text取代它的按鈕。

我的按鈕看起來是這樣的:

 private void button1_Click(object sender, EventArgs e) 
     { 
      using (TextReader reader = new StringReader(richTextBox1.Text)) 
      { 
       string str = reader.ReadToEnd(); 
       string cbtxt = comboBox1.Text; 
       string tbtxt = txtbox.Text; 
       str = str.Replace(cbtxt, tbtxt); 
      } 
     } 

我的方法添加到該按鈕會從RichTextBox中保存文本回到我的.csv文件結束,但這種替代方法在犯規取代我的東西RichTextBox的。

我。CSV文件(在RichTextBox中)看起來是這樣的:

somestring,somenumber,somespecialcharacters;
somestring2,somenumber2,somespecialcharacters2;

它有大約50行,我的組合框中填滿了每行的第一個單詞,如:「somestring」「somestring2」。 當我點擊somestring(然後它的我的combobox.text)然後我寫「newstring」到我的txtbox.text。當我點擊我的按鈕時,它應該在richtxt中找到comboBox.text並將其替換爲我的txtbox.text。

任何想法,爲什麼它不工作?

+1

你將來自海峽文成richTextBox1.Text?我在代碼中看不到這一點。 –

+0

替換不行嗎?如果不是我如何用保留不變的文本替換文本? – Tamas555

回答

0

爲了更換或使用流閱讀器中刪除的東西,你將需要刪除的每一行,並用新的(臨時)文件將其替換。

var tempFile = Path.GetTempFileName(); //Creates temporary file 
      List<string> linesToKeep = new List<string>(); //Creates list of all the lines you want to keep (everything but your selection) 
      using (FileStream fs = new FileStream(path(), FileMode.Open, FileAccess.Read)) //(this opens a new filestream (insert your path to your file there) 
      { 
       using (StreamReader sr = new StreamReader(fs)) //here is starts reading your file line by line. 
       { 
        while (!sr.EndOfStream) //as long it has not finished reading 
        { 
         string currentline = sr.ReadLine().ToString(); //this takes the current line it has been reading 
         string splitline = currentline; //this is a temporary string because you are going to have to split the lines (i think you have 3 so split in "," and then index the first line (your identifier or name))) 
         if (splitline.Split(';')[0] != ID) //split the line and add your personal identifier so it knows what to keep and what to delete. 
         { 
          linesToKeep.Add(currentline); //adds the line to the temporary file list of line that you want to keep 
         } 
        } 
       } 
      } 
      File.WriteAllLines(tempFile, linesToKeep); //writes all the lines you want to keep back into a file 
      File.Delete(path()); //deletes the old file 
      File.Move(tempFile, path()); //moves temporary file to old location of the old file. 
+0

這對我來說有點chineese ..我與編程begginer。 但是代碼中的替換方法在哪裏?這個ID是什麼? 順便說一句我犯了一個錯誤,我行不會結束「」他們只用輸入結束。此外,我想讀的richTextBox.text不是文件,因爲文件在RichTextBox中已經打開,這將是對我來說更容易,由於我的編程語言是有點壞的呢。 – Tamas555

+0

添加了一些評論,至於標識符(或ID),它通常是主鍵,通常是數據庫。所以你應該有一些東西來比較一條線,所以它知道它是什麼線。它也可以是一個屬性。 (例如日期或年齡)。編輯:這是我的一些舊的代碼,但我決定將它張貼在其全部,所以當你想刪除它可能會幫助你在後面沿線(因爲更換就簡單多了)。這種方法可以兼得。 –

+0

只有richTextBox.Text沒有提供文件路徑,我可以更容易嗎?因爲該文件路徑是不固定的,因爲你可以SE我anothed方法使用打開文件對話框打開file.I希望在RichTextBox中打開該文件,編輯RichTextBox的再救回來。我已經可以打開一個文件,我可以將它從richtextbox中保存下來,但我無法完成它的編輯部分。 – Tamas555

1

您寫道:

但這種替代方法犯規取代我的RichTextBox的東西。

這是因爲在C#中的字符串是不可變的?無論你如何處理字符串,原始字符串都不會改變。結果總是在一個新的字符串中。

See Why .NET String is immutable?

所以,雖然你的代碼更改的str的價值,你的RichTextBox中顯示不改變原來的海峽。

string str = reader.ReadToEnd(); 
string cbtxt = comboBox1.Text; 
string tbtxt = txtbox.Text; 
str = str.Replace(cbtxt, tbtxt); 

str指的是一個新的字符串。 RichTextBox1.Text仍然引用原始字符串。

解決方案:指定新的字符串到豐富的文本框:

this.RichTextBox1.Text = str; 

如果你想保存在一個文件中的文本,你必須創建一個FileWriter將寫入新的字符串(不改變字符串,字符串不能改變!)。

取決於它是多麼的重要,你不會失去舊的文件問題的情況下,可以考慮使用TMPFILE寫,刪除原始和移動TMPFILE

+0

所以,現在我添加了您的解決方案代碼,但它仍然不會更改richTextBox中的文本。 – Tamas555

+0

你真的在調試器中檢查了更改的字符串,並且你看到它被正確更改了嗎? –

+0

是的,我檢查過它。 – Tamas555

0

在第二次通知,看看這個代碼:

private void btnLoad_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    if (ofd.ShowDialog() == DialogResult.OK) 
    { 
     richTextBox1.LoadFile(ofd.FileName); 
    } 
} 

private void btnSave_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    if (ofd.ShowDialog() == DialogResult.OK) 
    { 
     richTextBox1.SaveFile(ofd.FileName); 
    } 
} 

我覺得這是更符合你腦子裏有什麼?

+0

您的保存按鈕剛剛將此文件保存在其中: 「{\ rtf1 \ ansi \ ansicpg1250 \ deff0 \ deflang1038 {\ fonttbl {\ f0 \ fnil \ fcharset238 Microsoft Sans Serif;}} \ viewkind4 \ uc1 \ pard \ F0 \ fs17 \相提並論 } NULL 「 – Tamas555

+0

它加載它沒有,雖然(文本其格式)。如果您嘗試像使用數據庫一樣使用文本文件,請改爲使用下面的代碼。 –

相關問題