简体   繁体   中英

Clicking a button to transfer values of textboxes and a panel to one multiline textbox

So i am creating a simple program that keeps track of my notes from work. The code i have works fine but i was thinking today about a different way to make it work and still achieve the same end result.

Currently the user types everything into several textboxes and checks a few checkboxes they click the save button and all the information plus some predetermined formatting is put into a text file and then they click the copy button and the textfile is read and output to the notes_view textbox so they can ensure the notes are formatted properly and it also copies to the clipboard.

Now what i would like it to do is as the user is typing in each textbox it will output automatically to the notes_view textbox and also the same with the checkboxes(needs to keep the formatting and predetermined lines of text as well) and then the user can just push one button that will copy it to the clipboard without having to use the file to store the information.

I am hoping this would be as simple as my program currently is but just going a different way to get the same end result.

I am rather new to C# and programming in general so any ideas on how to do this and where i should begin please let me know. Also i do understand this will essentially require an entire rewrite of my code.

Here is the current complete code for my program as is.

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



        }
    }
}

I've not looked closely at your code, but I'd imagine you can simply hook to a TextChanged (or similar) property, then call the same code you were calling for your Save process.

Update the save process to use an in-memory stream (rather than writing to disk), or re-write it to use something more "fitting" for your new scenario eg a stringbuilder as was already suggested.

Does that help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM