c#收集checkbox.Click事件供以后使用

我能够修改我提出的另一个问题提供的代码

c#根据按钮单击删除动态创建的复选框

足以让按钮点击和文件更新,但我陷入困境,如果有人愿意,可以使用更多的帮助。 我所拥有的代码目前是通过单击EventHandler从动态创建的复选框触发的。

chkPerm[i].Click += new EventHandler(checkChangedPerm); 

当我调用checkChangedPerm()时,我的复选框被读取,名称被修剪并格式化,结果放入一个字符串中,以便以后写入Permissions.txt文件。 checkbox.Name是用户名+文件名的修剪部分。 在下面的代码中,我修改了之前添加的部分,以便在文件中找到正确的用户名。

 public static void checkChangedPerm(Object sender, EventArgs e) { CheckBox c = sender as CheckBox; if (c.Name != null && c.Name != "") { StreamReader reader; int count = c.Name.Count(); string trimmed = c.Name.ToString(); string outPut = trimmed.Remove(count - 4); using (reader = new StreamReader(dataFolder + PermFile)) { while ((permLine = reader.ReadLine()) != null) { if (permLine != outPut) { permLine_ += permLine + "\r\n"; } } } //writer = writePerm(); } else { } } 

permLine和permLine_在其他地方宣布用于其他用途。

 writePerm(); 

单击表单权限上的按钮调用。 单击该按钮时,writePerm将使用新内容覆盖文件的现有内容。 问题在于checkChangedPerm()在每个复选框都被调用。单击并随后在每次单击后附加到permLine_。

我想我明白在.Click事件期间我需要将内容写入theFirstCalledString。 如果theFirstCalledString不为null,请转到FirstCalledString以查看是否存在c.Name,否则使用我编写的内容。 只是不知道从哪里开始。

任何人都可以指出我正确的方向? 这听起来好像我正在通过正确思考吗? 请。 非常感谢你提前。 我需要找到这个论坛的一部分并贡献我所知道的回馈一些。 再次感谢:-D

你可以这样做。 声明字符串列表 。 它可以是本地的也可以是私人的,没关系。 如果是私有的,您需要清除列表:

 private List  text = new List (); public static void checkChangedPerm(Object sender, EventArgs e) { CheckBox c = sender as CheckBox; if (c.Name != null && c.Name != "") { StreamReader reader; int count = c.Name.Count(); string trimmed = c.Name.ToString(); string outPut = trimmed.Remove(count - 4); using (reader = new StreamReader(dataFolder + PermFile)) { while ((permLine = reader.ReadLine()) != null) { if (permLine != outPut) { text.Add(permLine + "\r\n"); } } } text[text.Count - 1] = text[text.Count - 1].Replace("\r\n", ""); permLine = String.Join(String.Empty, text); //and write permLine to file //writer = writePerm(); } else { } if (text.Count != 0) { text.Clear(); } }