我正在创建一个小的会员类型表格。它由1 X表单组成,里面包含以下内容:
4个分组框-每个分组框内有几个文本框,单选按钮和复选框。
在页面底部,我有一个注册按钮,该按钮应该捕获我选中的所有复选框,单选按钮和文本。
private void Bregister_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("member.txt");
txt.Write("First Name:" + tfirstn.Text "\r\n" + "Last Name:" + tlastn.Text "\r\n" + "Address:" + taddr.Text "\r\n" + "Mobile Number:" + tmobi.Text "\r\n" + "Recucrring Payment Amount:" + trpa.Text "\r\n" + "Account Number:" + taccnbr.Text "\r\n" + "Frequency:" + rweek.Text "\r\n" + DateTime.Today.ToString());
txt.Close();
}
答案 0 :(得分:1)
从技术上讲,您错过了几个+
:
("First Name:" + tfirstn.Text "\r\n" + "Last Name:" + tlastn.Text "\r\n" ....
^ ^
Here and here should be pluses +
我建议:
string.Join
(可读性)整理所有行Date:
Date
时,我们指定 format (因为它取决于文化)File.WriteAllText
Bregister_Click
)和业务逻辑(保存数据)代码:
private void SaveData(string fileName) {
string data = string.Join(Environment.NewLine,
$"First Name: {tfirstn.Text}",
$"Last Name: {tlastn.Text}",
$"Mobile Number: {tmobi.Text}",
$"Recurring Payment Amount: {trpa.Text}", // Typo? "Recucrring"
$"Account Number: {taccnbr.Text}",
$"Frequency: {rweek.Text}",
//DONE: added name - "Date" and Date format
$"Date: {DateTime.Today.ToString("dd.MM.yyyy")}"
);
// Or File.AppendAllText if you don't want to rewrite file if it exists
File.WriteAllText(fileName, data);
}
private void Bregister_Click(object sender, EventArgs e) {
SaveData("member.txt");
}