c# Gather checkbox.Click events for later use

2019-03-03 02:17发布

问题:

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

c# Remove a dynamically created checkbox based on a button click

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

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
    {

    }        
}

permLine and permLine_ are declared elsewhere for other uses.

writePerm();

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.

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.

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

回答1:

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