0

I'm lost. I want to remove a value from a text file. The value is a checkbox.Name. I want to open a text file, find the corresponding username in the textfile, remove it and save the file based on a button click.

Here is how I get the 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();

}

I can access the checkbox.Name based on a click event so I know I'm getting the correct 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
    {

    }


}

I've been searching for this most of the morning and all day Friday. Can anyone point me in the right direction or possibly suggest some sample code. Please Please. Thank you in advance. I truly do appreciate it. :-D

ZoomVirus
  • 615
  • 7
  • 22
Frank Pytel
  • 127
  • 16
  • So, it looks like you're dynamically creating checkboxes based on the names in the file. You can either, open the file, find the string and delete it, or recreate the file using what you pulled for your checkboxes, minus the one you don't need. I would personally go with the second option, but I'm not sure what the file looks like. Are you unsure how to write to a text file or ... ? Alternatively, it might be easier to use serialization depending on the ultimate purpose, since you could just deserialize/serialize the object to the text file as needed. – user1274820 Oct 20 '14 at 15:43
  • 1
    I do agree with @user1274820's recommendations. In addition to them, please do not ignore all exceptions with an empty `try..catch`, put your two `Stream` objects into `using` clauses to ensure they are properly freed and take a look at the `TextReader` class to read the file line by line instead of loading the whole file into memory first, this will also remove the requirement to manually trim the newline characters from your strings – Bernd Linde Oct 20 '14 at 16:02
  • @user1274820 My confusion I think is in trying to write back to the file. I can get the click event of the checkbox I'm wanting to remove, but that does not provide me with the other 20 checkboxes on the page that I need to remain. Therein lies my difficulty most I think. I think I understand parsing text from a string, but parsing a string from the middle of a string or from an stringArray. I don't get it I think. – Frank Pytel Oct 20 '14 at 16:43
  • That's probably a good reason to go with what Bernd Linde proposed. If you read it line by line, you can store each line to an array and when it comes time to write it back, you can just skip over the line you don't want. Check this link out http://www.dotnetperls.com/textreader – user1274820 Oct 20 '14 at 16:46
  • @BerndLinde I do not understand the using() enclosure around, frankly any thing. I'm new to this. Is that a part of Linq? – Frank Pytel Oct 20 '14 at 16:47
  • 1
    @FrankPytel, you can get the `ContainerControl` of your checkbox by calling `c.GetContainerControl()`. Cast that to your form's type and then iterate through it's `Controls`, finding each `CheckBox` and writing that then to the file if you go the route of overriding the file each time – Bernd Linde Oct 20 '14 at 16:47
  • @FrankPytel the definition of a [using clause](http://msdn.microsoft.com/en-us/library/yh598w02.aspx) and in addition to user1274820's link to `TextReader`, here is [another one](http://msdn.microsoft.com/en-us/library/system.io.textreader%28v=vs.110%29.aspx) – Bernd Linde Oct 20 '14 at 16:51

1 Answers1

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);
}
  • I would rather recommend using a `StringBuilder` instead of concatenating a `string` the whole time, then you can also use [StringBuilder.AppendLine(string)](http://msdn.microsoft.com/en-us/library/cb3sbadh%28v=vs.110%29.aspx) instead of hardcoding the newline characters – Bernd Linde Oct 20 '14 at 16:55
  • I'd prefer a list personally - of strings – user1274820 Oct 20 '14 at 17:00
  • @γηράσκωδ'αείπολλάδιδασκόμε Brilliant!! Again. I've been playing with it stand alone. You wrote it for exactly what I had displayed. Processing. THANK YOU!! – Frank Pytel Oct 20 '14 at 18:12
  • @γηράσκωδ'αείπολλάδιδασκόμε You answered the question I asked mostly. I fixed it for a button trigger, but its not writing correctly. Every time the .Click fires it appends the last string to the new string line_. I've changed it substantially though so I will need to re ask. Thank You Very Much :-D You are very good. – Frank Pytel Oct 21 '14 at 12:22
  • @FrankPytel Edit your question to see the problem. We will fix it :) – γηράσκω δ' αεί πολλά διδασκόμε Oct 21 '14 at 12:26
  • @γηράσκωδ'αείπολλάδιδασκόμε Thank you. I actually did rewrite the question. Not sure of the rules here so I posted it as a separate question. http://stackoverflow.com/questions/26487297/c-sharp-gather-checkbox-click-events-for-later-use – Frank Pytel Oct 21 '14 at 12:57