Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using checkboxes to save text to file need it to go to a new line if some are not checked

Tags:

c#

checkbox

I am creating a simple program to track notes into a text file. I have everything working correctly except for the checkboxes. I need it where you can select which checkboxes you want and it writes to the file on the same line. If no checkboxes are selected or only some of them it will go to next line as well. Below is the code i currently have and it works fine so far except if i use Writeline on each checkbox statement obviously it puts each checkbox text on a new line. If i use only Write it works except the "tshooting_text.Text ends up on the same line as the checkboxes text. I am a bit new to C# and programming in general so please forgive me if i am missing something simple. Anyway here is the code i have so far.

private void copy_clipboard_button_Click(object sender, EventArgs e)
{

    //Starts the file writer
    using (StreamWriter sw = new StreamWriter("C:\\INET Portal Notes.txt"))
    {
        string CBRSame = cust_btn_text.Text;
        if (cbr_same.Checked)
        {
            cust_callback_text.Text = CBRSame;
        }

        //Writes textboxes to the file
        sw.WriteLine("**Name: " + cust_name_text.Text);
        sw.WriteLine("**BTN: " + cust_btn_text.Text);  
        sw.WriteLine("**CBR: " + cust_callback_text.Text);
        sw.WriteLine("**Modem: " + cust_modem_text.Text);
        //Statements to write checkboxes to file
        if (checkbox_pwr.Checked)
            sw.Write("**Lights: PWR, ");
        if (checkbox_e1.Checked)
            sw.Write("E1, ");
        if (checkbox_e2.Checked)
            sw.Write("E2, ");
        if (checkbox_e3.Checked)
            sw.Write("E3, ");
        if (checkbox_e4.Checked)
            sw.Write("E4, ");
        if (checkbox_wlan.Checked)
            sw.Write("WLAN, ");
        if (checkbox_dsl.Checked)
            sw.Write("DSL, ");
        if (checkbox_dslblink.Checked)
            sw.Write("DSL Blinking, ");
        if (checkbox_inet.Checked)
            sw.Write("INET, ");
        if (checkbox_inetred.Checked)
            sw.Write("INET RED, ");

        //Continues textboxes to file
        sw.WriteLine("**Troubleshooting: " + tshooting_text.Text);
        sw.WriteLine("**Services Offered: " + services_offered_text.Text);
        sw.WriteLine("**Other Notes: " + other_notes_text.Text);
        sw.Flush();
    }
}
like image 995
Nabbic Avatar asked Dec 17 '25 03:12

Nabbic


1 Answers

My suggestion is the following:

  1. Put all your CheckBox controls inside a Panel control, let's call it pnlCheckBoxes.
  2. Input desired indicator to CheckBox's Tag property (at design time).
  3. Order check boxes if needed.
  4. Enumerate all the checkboxes and write the output text for each one.

This way, you will be able to easily add/remove check boxes and not need to modify your code to reflect these changes.

Here's how you can order your check boxes inside Panel control (be sure not to modify the rest of the code if you not sure about it). Modify the code in your Form1.designer.cs file:

// 
// pnlCheckBoxes
//
// Rearrange the lines below to match the order you desire
this.pnlCheckBoxes.Controls.Add(this.checkBoxWlan);
this.pnlCheckBoxes.Controls.Add(this.checkBoxDsl);
// Leave the lines below intact.
this.pnlCheckBoxes.Location = new System.Drawing.Point(21, 110);
this.pnlCheckBoxes.Name = "pnlCheckBoxes";
this.pnlCheckBoxes.Size = new System.Drawing.Size(200, 100);
this.pnlCheckBoxes.TabIndex = 6;

Here's the code of your writer function (after placing check boxes in panel, assigning Tag properties to them and arranging them):

private void copy_clipboard_button_Click(object sender, EventArgs e)
{
    //Starts the file writer
    using (StreamWriter sw = new StreamWriter("C:\\INET Portal Notes.txt"))
    {
        /* ... */

        //Statements to write checkboxes to file

        // This variable will store your whole line for check boxes.
        string checkBoxesLine = "**";

        // Enumerate all controls in panel.
        foreach (Control control in pnlCheckBoxes.Controls)
        {
            // Make sure the control is CheckBox, ignore others.
            if (control is CheckBox)
            {
                // Cast it to CheckBox variable, to make it easier to work with.
                CheckBox checkBox = (CheckBox)control;

                // Make sure it is checked, and if it is, make sure that the Tag property
                // has been set to string, so that we can get it's ID (WLAN, DSL, etc.).
                if (checkBox.Checked && checkBox.Tag is string)
                {
                    // Cast tag to string.
                    string checkBoxId = (string)checkBox.Tag;
                    // Append tag and comma to the end of the line.
                    checkBoxesLine += string.Format("{0}, ", checkBoxId);
                }
            }
        }

        // That's it, we have the whole line, write it as a new line.
        sw.WriteLine(checkBoxesLine);

        //Continues textboxes to file
        /* ... */ 
    }
}

The code above can be shortened, but since you're new to C#, I've tried to explain as much as I could.

Although the answer is a bit out of the scope of the question, I have demonstrated several functionalities here - enumerating controls, casting objects, formatting strings - and I hope you will find these useful in your future C# projects.

like image 56
Nikola Malešević Avatar answered Dec 19 '25 21:12

Nikola Malešević