0

所以我正在创建一个简单的程序来跟踪我的工作笔记。我的代码运行良好,但我今天正在考虑一种不同的方法来使其工作并仍然达到相同的最终结果。

目前,用户在几个文本框中输入所有内容并选中几个复选框,然后单击保存按钮,所有信息以及一些预定格式被放入文本文件中,然后单击复制按钮,读取文本文件并将其输出到 notes_view 文本框这样他们就可以确保笔记格式正确,并且还可以复制到剪贴板。

现在我想做的是当用户在每个文本框中输入时,它会自动输出到 notes_view 文本框,复选框也一样(需要保持格式和预定的文本行),然后是用户只需按一个按钮即可将其复制到剪贴板,而无需使用文件来存储信息。

我希望这会像我目前的程序一样简单,但只是采用不同的方式来获得相同的最终结果。

我对 C# 和一般编程相当陌生,所以关于如何做到这一点以及我应该从哪里开始的任何想法请告诉我。我也明白这基本上需要对我的代码进行完全重写。

这是我的程序的当前完整代码。

 public partial class notes_form : Form
{

    public notes_form()
    {
        InitializeComponent();


    }

    private void save_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


            string checkBoxesLine = "**Lights:";

            foreach (Control control in pnlCheckBoxes.Controls)
            {
                if (control is CheckBox)
                {
                    CheckBox checkBox = (CheckBox)control;

                    if (checkBox.Checked && checkBox.Tag is string)
                    {
                        string checkBoxId = (string)checkBox.Tag;
                        checkBoxesLine += string.Format("{0}, ", checkBoxId);
                    }
                }
            }
            //Newline for checkboxes
            sw.WriteLine(checkBoxesLine);

            //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();


        }

    }
    //Button that will pull all the text from the text file and then show it in the notes textbox and also push to clipboard
    private void generate_button_Click(object sender, EventArgs e)
    {
        //Loads the reader
        StreamReader streamreader = new StreamReader("C:\\INET Portal Notes.txt");
        //Reads the text from the INET Portal Notes.txt
        notes_view_text.Text = "";
        while (!streamreader.EndOfStream)
        {
            string read_line = streamreader.ReadToEnd();
            notes_view_text.Text += read_line + "\n";
        }

        streamreader.Close();
        //Copies text to clipboard for pasting into INET
        Clipboard.SetText(notes_view_text.Text);
    }
    //Button to reset entire form
    private void reset_form_button_Click(object sender, EventArgs e)
    {
        //Reset checkboxes panel
        try
        {
            foreach (Control ctrl in pnlCheckBoxes.Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                    ((CheckBox)ctrl).Checked = false;

            }
            //resets textboxes
            cust_name_text.Clear();
            cust_btn_text.Clear();
            cust_callback_text.Clear();
            cust_modem_text.Clear();
            tshooting_text.Clear();
            services_offered_text.Clear();
            other_notes_text.Clear();
            notes_view_text.Clear();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());



        }
    }
}
4

1 回答 1

1

我没有仔细查看您的代码,但我想您可以简单地挂钩到 TextChanged(或类似)属性,然后调用您为 Save 进程调用的相同代码。

更新保存过程以使用内存流(而不是写入磁盘),或重新编写它以使用更“适合”您的新场景的东西,例如已经建议的字符串生成器。

这有帮助吗?

于 2012-09-03T05:19:19.360 回答