因此,我創建了一個簡單的程序,用於跟蹤我的工作筆記。我的代碼工作正常,但我今天正在想一個不同的方式來使其工作,並仍然達到相同的最終結果。點擊一個按鈕將文本框和麪板的值傳輸到一個多行文本框
當前用戶將所有內容輸入到多個文本框中,並檢查一些複選框,他們單擊保存按鈕,將所有信息和一些預定格式設置放入文本文件中,然後單擊複製按鈕並讀取並輸出文本文件到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());
}
}
}
你可以使用StringBuilder來存儲所有的信息。使用System.Text.StringBuilder添加文本strBuilder = new System.Text.StringBuilder(); strBuilder.Append(「你的字符串在這裏」); 。然後你可以將它添加到notes_view_text中:notes_view_text.Text = strBuilder.ToString(); –
我實現了Stringbuilder來代替Streamwriter,它工作得很好。感謝您的提示。關於如何使文本轉到notes_view_text,因爲它是在每個文本框中鍵入的,而不是甚至必須點擊保存按鈕,可能更多的是高級註釋。那是難以完成的事情嗎? – Nabbic
這是一個ASP.NET或桌面應用程序? –