C# 单击按钮可将文本框和面板的值传递到一个多行文本框

C# 单击按钮可将文本框和面板的值传递到一个多行文本框,c#,C#,因此,我正在创建一个简单的程序来跟踪我的工作笔记。我的代码工作得很好,但今天我想用一种不同的方法使它工作,并且仍然达到相同的最终结果 目前,用户在几个文本框中键入所有内容,并选中几个复选框。他们单击“保存”按钮,所有信息加上一些预定格式被放入文本文件中,然后单击“复制”按钮,文本文件被读取并输出到“注释”视图文本框,以便确保注释的格式正确它也会复制到剪贴板 现在我想让它做的是,当用户在每个文本框中键入时,它会自动输出到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());



        }
    }
}

我没有仔细研究过您的代码,但我认为您可以简单地挂接到TextChanged(或类似)属性,然后调用与您在保存过程中调用的代码相同的代码

更新保存过程以使用内存中的流(而不是写入磁盘),或者重新写入它以使用更“适合”您的新场景的内容,如已建议的stringbuilder


这有帮助吗?

您可以使用StringBuilder存储所有信息。使用System.text.StringBuilder strBuilder=new System.text.StringBuilder()添加文本;追加(“字符串在这里”)。然后可以将其添加到notes_view_text中,如下所示:notes_view_text.text=strBuilder.ToString();我实现了Stringbuilder来代替Streamwriter,效果非常好。谢谢你的提示。在可能更多的高级备忘中,我很好奇如何让文本在每个文本框中输入时进入notes\u view\u文本,而不必点击save按钮。这是很难实现的吗?这是ASP.NET还是桌面应用程序?然后您可以使用TextBox的TextChanged、KeyPressed或LostFocus事件来实现您想要的。查看此链接的“事件”部分了解更多信息:谢谢,我查看了该链接,等我有更多时间再坐下来玩。谢谢你的指导谢谢你的回复。我按照另一位成员的建议实现了Stringbuilder,效果非常好:)