C#中收集供以后使用checkbox.Click事件 [英] c# Gather checkbox.Click events for later use

查看:428
本文介绍了C#中收集供以后使用checkbox.Click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够修改提供了另一个问题,我问这个代码

I was able to modify this code provided on another question I asked

C#删除基于一个按钮动态创建的复选​​框,单击

足以获得右键单击并更新文件,但我坚持,如果有人愿意可以使用一些更多的援助。我的代码目前正在从一个点击事件处理动态创建的复选​​框触发。

sufficiently to get the button to click and a file to update, but I'm stuck and could use a little more assistance if someone is willing. The code I have is currently triggered from a dynamically created checkbox with a click EventHandler.

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

当我打电话checkChangedPerm()正在读我的复选框,名称修整和格式化,结果放到供以后写入Permissions.txt文件中的字符串。该checkbox.Name是用户名+文件名的剪裁后的部分。在下面的代码我修剪补充说,部分早期发现文件中的正确的用户名。

When I call checkChangedPerm() my checkboxes are being read, the name trimmed and formatted and the results put into a string for later writing to the Permissions.txt file. The checkbox.Name is the username + a trimmed portion of the file name. In the following code I trim that portion added earlier to find the correct username in the file.

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
    {

    }        
}

$ B $ ; b

permLine和permLine_声明别处用于其他用途。

permLine and permLine_ are declared elsewhere for other uses.

writePerm();



被称为上的表单权限单击按钮。当点击该按钮,writePerm覆盖与新内容的文件的现有内容。问题在于这样一个事实:checkChangedPerm()被调用每checkbox.Click和每次点击后,随后被追加到permLine_。

is called on a button click on the form Permissions. When the button is clicked, writePerm overwrites the existing contents of the file with the new contents. The problem lies in the fact that checkChangedPerm() is called at every checkbox.Click and is subsequently appending to permLine_ after every click.

我想我明白这期间。点击事件中,我需要写的内容theFirstCalledString。如果theFirstCalledString不为空转到theFirstCalledString,看是否存在c.Name,否则使用我有写的东西。只是不知道从哪里开始。

I think I understand that during the .Click event I need to write the contents to theFirstCalledString. If theFirstCalledString isn't null go to theFirstCalledString to see if c.Name exists, otherwise use what I've got written. Just have no clue where to start.

任何人都可以请点我在正确的方向?这听起来像我通过正确地思考这个?请。预先感谢您非常多。我需要找到该论坛的一部分,有助于我知道的回馈一些。再次感谢:-D

Can anyone please point me in the right direction?? Does this sound like I'm thinking this through correctly?? Please. Thank You very much in advance. I need to find a part of this forum and contribute what I do know to give back some. Thanks Again :-D

推荐答案

您可以这样来做。声明字符串列表的。它可以是本地或私有的,它并不重要。如果是私人你需要清空列表:

You can do it this way. Declare a list of strings. It can be local or private, it doesn't matter. If it is private you need to clear the list:

private List <string> text = new List <string>();

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();
    }
}

这篇关于C#中收集供以后使用checkbox.Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆