2014-10-20 82 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