3

我正在创建一个简单的程序来将笔记跟踪到文本文件中。除了复选框之外,我的一切都正常工作。我需要它,您可以在其中选择所需的复选框并将其写入同一行的文件。如果未选择复选框或仅选择其中一些复选框,它也会转到下一行。下面是我目前拥有的代码,到目前为止它工作正常,除非我在每个复选框语句上使用 Writeline,显然它会将每个复选框文本放在一个新行上。如果我只使用 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();
    }
}
4

2 回答 2

1
 // ...
 if (checkbox_inetred.Checked)
        sw.Write("INET RED, ");

 sw.WriteLine (); // <--- this is new, means: insert line break

 //Continues textboxes to file
 sw.WriteLine("**Troubleshooting: " + tshooting_text.Text);

 // ...
于 2012-09-02T21:48:57.220 回答
1

我的建议如下:

  1. 将所有CheckBox控件放在一个Panel控件中,我们称之为pnlCheckBoxes.
  2. 将所需的指标输入到CheckBox' 的Tag属性(在设计时)。
  3. 如果需要,请订购复选框。
  4. 枚举所有复选框并为每个复选框编写输出文本。

这样,您将能够轻松添加/删除复选框,而无需修改代码以反映这些更改。

以下是如何在控件中订购复选框Panel(如果不确定,请确保不要修改其余代码)。修改文件中的代码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;

这是您的编写器功能的代码(在面板中放置复选框,Tag为它们分配属性并排列它们之后):

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
        /* ... */ 
    }
}

上面的代码可以缩短,但由于您是 C# 新手,所以我已尽力解释。

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

于 2012-09-02T22:09:50.930 回答