C# 使用复选框将文本保存到文件,如果未选中某些复选框,则需要将文本转到新行

C# 使用复选框将文本保存到文件,如果未选中某些复选框,则需要将文本转到新行,c#,checkbox,C#,Checkbox,我正在创建一个简单的程序,将注释跟踪到文本文件中。除了复选框外,我所有的东西都正常工作。我需要它,你可以选择哪些复选框,你想要和它写在同一行的文件。如果未选中任何复选框或仅选中其中一些复选框,则也将转到下一行。下面是我目前拥有的代码,到目前为止它运行良好,除非我在每个checkbox语句上使用Writeline,显然它会将每个checkbox文本放在新行上。如果我只使用Write,它可以工作,除了“tshooting_text.text”和复选框文本结束在同一行。我对C#和编程有点陌生,所以如果

我正在创建一个简单的程序,将注释跟踪到文本文件中。除了复选框外,我所有的东西都正常工作。我需要它,你可以选择哪些复选框,你想要和它写在同一行的文件。如果未选中任何复选框或仅选中其中一些复选框,则也将转到下一行。下面是我目前拥有的代码,到目前为止它运行良好,除非我在每个checkbox语句上使用Writeline,显然它会将每个checkbox文本放在新行上。如果我只使用Write,它可以工作,除了“tshooting_text.text”和复选框文本结束在同一行。我对C#和编程有点陌生,所以如果我遗漏了一些简单的东西,请原谅。无论如何,这是我到目前为止的代码

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();
    }
}
/。。。
如果(复选框\u inetred.Checked)
sw.Write(“INET RED,”);

sw.WriteLine();//我的建议如下:

  • 将所有
    复选框
    控件放入
    面板
    控件中,我们称之为
    pnlcheckbox
  • 将所需指示器输入到
    复选框的
    标记
    属性(在设计时)
  • 如果需要,请选择“订购”复选框
  • 枚举所有复选框并为每个复选框写入输出文本
  • 这样,您就可以轻松地添加/删除复选框,而无需修改代码来反映这些更改

    以下是如何在
    面板
    控件中排序复选框(如果不确定,请确保不要修改其余代码)。在
    Form1.designer.cs
    文件中修改代码:

    // 
    // 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;
    
    以下是writer函数的代码(在面板中放置复选框,为其分配
    标记
    属性并对其进行排列后):

    上面的代码可以缩短,但由于您是C#新手,我已经尽可能多地解释了


    虽然答案有点超出了问题的范围,但我已经在这里演示了一些功能—枚举控件、强制转换对象、格式化字符串—我希望您在未来的C#项目中会发现这些功能非常有用。

    Genius!我还以为我缺少了一些简单的功能。非常感谢您的帮助。谢谢!这在人们不知道的地方很普遍吗?哈哈,如果我不告诉你我在做什么,你怎么能帮助我。很多人喜欢从尽可能多的信息开始提问,只有在有人愿意帮助的时候才一点一点地提供信息。你会惊讶地发现有这么多问题被这样问。啊,我明白了。谢谢你的帮助nfo和我以后会记住这一点,因为我相信我还有其他问题要问:)这其实很有用,因为我正在考虑添加一些复选框。看起来这也是一种更好的方法。非常感谢!不客气!别忘了给你觉得有用的答案投票。这是一种小小的感谢你注意到。我会尽快得到2个以上的代表点lol。必须有15:)这工作非常漂亮顺便说一句。改变了我的代码,以使用面板代替。我已经更新了我的答案,包括重新排列。谢谢你指出。
    // 
    // 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;
    
    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
            /* ... */ 
        }
    }