我完全坚持在这里创建一个随机数。理想情况下,不是同一个号码是客户号码的两倍。
然后我需要在我的 tb_id (文本框)中显示它并将该行写入客户文件。
这里的任何帮助都会很棒。
private void button1_Click(object sender, EventArgs e)
{
**var rng = new Random();**
// Here I need to write the random number (it's a customer number) to the tb_id text box. so I'm able to write it to their customer file afterwards.
try
{
string fileName = string.Format(tb_surname.Text);
if (tb_firstname.Text == "" || fileName == "" || tb_postcode.Text == "")
{
MessageBox.Show("Missing values from textboxes!");
}
else if (File.Exists(fileName + "Text"))
{
if (MessageBox.Show("Warning: There is already a file with the surname you enter already on the system. Contents will be replaced - do you want to continue?", "File Demo", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
//write lines of text to file
StreamWriter outputStream = File.CreateText(fileName + ".Txt");
outputStream.WriteLine(tb_firstname.Text);
outputStream.WriteLine(tb_surname.Text);
outputStream.WriteLine(tb_surname.Text);
**outputStream.WriteLine(tb_id.Text);**
outputStream.Close();
MessageBox.Show("Text saved to file successfully!");
this.Close();
}
}
}
// ...
}
第2部分
// creating a random number here for the customer id
Guid g = Guid.NewGuid();
tb_id.Text = g.ToString();
将 Guid 号写入文本文件。
outputStream.WriteLine(tb_id.Text);