所以我正在创建一个简单的程序来跟踪我的工作笔记。我的代码运行良好,但我今天正在考虑一种不同的方法来使其工作并仍然达到相同的最终结果。
目前,用户在几个文本框中输入所有内容并选中几个复选框,然后单击保存按钮,所有信息以及一些预定格式被放入文本文件中,然后单击复制按钮,读取文本文件并将其输出到 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());
}
}
}