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();
}
}
My suggestion is the following:
CheckBoxcontrols inside aPanelcontrol, let’s call itpnlCheckBoxes.CheckBox‘sTagproperty (at design time).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
Panelcontrol (be sure not to modify the rest of the code if you not sure about it). Modify the code in yourForm1.designer.csfile:Here’s the code of your writer function (after placing check boxes in panel, assigning
Tagproperties to them and arranging them):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.