2014-10-20 41 views
0

我迷路了。我想從文本文件中刪除一個值。該值是一個checkbox.Name。我想打開一個文本文件,在文本文件中找到相應的用戶名,將其刪除,然後根據點擊按鈕保存該文件。c#根據點擊按鈕刪除動態創建的複選框

這裏是我得到checkbox.Name

public static void getPermText(System.Windows.Forms.Form targetForm) 
{ 
    Stream fileStream = File.Open(dataFolder + PermFile, FileMode.Open); 
    StreamReader reader = new StreamReader(fileStream); 

    string line = null; 

    line = reader.ReadToEnd(); 


    string[] parts = line.Split('\n'); 

    string user = userNameOnly(); 
    try 
    { 

     int userCount; 

     userCount = parts.Length; 

     CheckBox[] chkPerm = new CheckBox[userCount]; 
     int height = 1; 
     int padding = 10; 

     for (int i = 0; i < userCount; i++) 
     { 
      chkPerm[i] = new CheckBox(); 

      string Perm = "Perm"; 

      chkPerm[i].Name = parts[i].Trim('\r') + Perm; 

      chkPerm[i].Text = parts[i]; 

      chkPerm[i].TabIndex = i; 

      chkPerm[i].AutoCheck = true; 

      chkPerm[i].Bounds = new Rectangle(15, 40 + padding + height, 100, 22); 

      //Assigns an eventHandler to the chkPerm.CheckBox that tells you if something is clicked, then that checkBox is selected/checked. 
      //Not currently in use. 
      chkPerm[i].Click += new EventHandler(checkChangedPerm); 


      targetForm.Controls.Add(chkPerm[i]); 

      height += 22; 

      //MessageBox.Show(chkPerm[i].Name); 

     } 



    } 
    catch 
    { 

    } 
    fileStream.Close(); 

} 

我可以訪問checkbox.Name基於點擊事件,所以我知道我得到了正確的checkbox.Name

public static void checkChangedPerm(Object sender, EventArgs e) 
{ 

    CheckBox c = sender as CheckBox; 

    if (c.Name != null && c.Name != "") 
    { 
     int count = c.Name.Count(); 

     string trimmed = c.Name.ToString(); 
     string outPut = trimmed.Remove(count - 4); 


    } 
    else 
    { 

    } 


} 

我一直在尋找這個大部分的早上和整個星期五。任何人都可以向我指出正確的方向,或者可能建議一些示例代碼。請請。先謝謝你。我真的很感激它。 :-D

+0

因此,它看起來像您是基於文件中的名稱動態創建複選框。您可以打開文件,查找字符串並刪除它,也可以使用您爲複選框拉取的文件重新創建文件,然後減去不需要的文件。我會親自去第二個選項,但我不知道該文件是什麼樣子。你不確定如何寫入文本文件或...?或者,根據最終目的使用序列化可能更容易,因爲您可以根據需要將對象反序列化/序列化爲文本文件。 – user1274820 2014-10-20 15:43:22

+1

我同意@ user1274820的建議。除此之外,請不要忽略所有使用空的'try..catch'的異常,將兩個'Stream'對象放在'using'子句中,以確保它們被正確釋放並查看'TextReader'類以逐行讀取文件,而不是首先將整個文件加載到內存中,這也將刪除手動修剪字符串中換行符的要求 – 2014-10-20 16:02:23

+0

@ user1274820我認爲是在試圖寫回文件時出現混亂。我可以得到我想要刪除的複選框的點擊事件,但是這並不能爲我提供我需要保留的頁面上的其他20個複選框。其中最令我困惑的就是我的困難。我想我理解解析字符串中的文本,但解析字符串或字符串數​​組中的字符串。我不明白。 – 2014-10-20 16:43:51

回答

1
StreamReader reader; 
StreamWriter writer; 

string line, newText = null; 

using (reader = new StreamReader(@"...")) 
{ 
    while ((line = reader.ReadLine()) != null) 
    { 
     if (line != "checkbox name") 
     { 
      newText += line + "\r\n"; 
     } 
    } 
} 

newText = newText.Remove(newText.Length - 2); //trim the last \r\n 

using (writer = new StreamWriter(@"...")) //same file 
{ 
    writer.Write(newText); 
} 
+0

我寧願推薦使用'StringBuilder'而不是一次串聯一個'string',那麼你也可以使用[StringBuilder.AppendLine (字符串)](http://msdn.microsoft.com/en-us/library/cb3sbadh%28v=vs.110%29.aspx)而不是硬編碼換行符 – 2014-10-20 16:55:39

+0

我寧願個人列表 - 字符串 – user1274820 2014-10-20 17:00:47

+0

@γηράσκωδ'αείπολλάδιδασκόμε複製那個Stank :-D。正在處理..... – 2014-10-20 17:19:12