创建随机客户编号并将其显示在文本框中

Jam*_*oft 0 c# random

我完全坚持创建一个随机数.理想情况下,不是与客户编号相同的两倍.

然后我需要在我的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();
            }
        }
    }
    // ...
}
Run Code Online (Sandbox Code Playgroud)

第2部分

       // creating a random number here for the customer id
        Guid g = Guid.NewGuid();
        tb_id.Text = g.ToString();
Run Code Online (Sandbox Code Playgroud)

将Guid号码写入文本文件.

        outputStream.WriteLine(tb_id.Text);
Run Code Online (Sandbox Code Playgroud)

COL*_*OLD 7

我不建议使用随机数,如果你想让它对用户来说是唯一的,那么很有可能让不同的用户拥有相同的号码.

guid可能是更好的主意

  Guid g = Guid.NewGuid();
Run Code Online (Sandbox Code Playgroud)