【问题标题】:Using checkboxes to save text to file need it to go to a new line if some are not checked如果未选中某些复选框,则使用复选框将文本保存到文件需要转到新行
【发布时间】:2012-09-03 19:44:08
【问题描述】:

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

【问题讨论】:

  • 谢谢!这在人们不常见的地方很常见吗?大声笑,如果我不向你展示我正在使用的东西,你怎么能帮助我。
  • 许多人喜欢从尽可能多的信息开始提问,只有在愿意提供帮助的人提出要求时才一点一点地提供信息。您会惊讶地发现有多少问题是以这种方式被问到的。
  • 啊,我明白了。感谢您提供的信息,我会记住它以备后用,因为我相信我还有其他问题要问:)

标签: c# checkbox


【解决方案1】:

我的建议如下:

  1. 将所有CheckBox 控件放在Panel 控件中,我们称之为pnlCheckBoxes
  2. CheckBoxTag 属性中输入所需的指示符(在设计时)。
  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;

这是您的 writer 函数的代码(在面板中放置复选框后,将 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# 项目中很有用。

【讨论】:

  • 这实际上非常有用,因为我实际上正在考虑添加更多复选框。看起来也是一种更好的方法。非常感谢!
  • 不客气!不要忘记为您认为有用的答案投票。它作为一个小小的感谢信。
  • 我会尽快获得 2 个代表点,哈哈。必须有 15 个 :)
  • 顺便说一句,效果很好。将我的代码更改为使用面板。
  • 我已经更新了我的答案以包括重新排列。感谢您指出。
【解决方案2】:
 // ...
 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);

 // ...

【讨论】:

  • 天才!我认为这是我在那里想念的简单的东西。非常感谢,效果很好。
猜你喜欢
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
相关资源
最近更新 更多