1

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

Community
  • 1
  • 1
Frank Pytel
  • 127
  • 16
  • Could you stored the last `outPut` value for comparison in future calls, either as a `String` if you're only interested in the last value, or `List` if you want to remember all previous values? Also, you should consider using `StringBuilder` for concatenating the strings read from the `StreamReader`. – Evil Dog Pie Oct 21 '14 at 13:09
  • If you add *permLine_ = "";* in *checkChangedPerm()* before *using* statement or declare *permLine_* as local you will not have the problem of appending new text. – γηράσκω δ' αεί πολλά διδασκόμε Oct 21 '14 at 13:17
  • @γηράσκωδ'αείπολλάδιδασκόμε That definitely helped some. Unfortunately what is happening is that it is only checking for the last selected checkbox. I need to get the UI.Refresh() working, which it is not. Let me get the UI to refresh and see if that will change how it operates. Thank You Very Much Again. :-D – Frank Pytel Oct 21 '14 at 13:32

1 Answers1

2

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