字符串在单击另一个文本框后消失

问题描述 投票:0回答:1

当我点击按钮时,我试图生成随机整数值(条形码)。然后,我正在检查两个表(库存,单位),以确定新条形码是否已经存在。如果它是唯一的,新的条形码将被写​​在文本框中。

都可以,但是当我单击另一个表格的texbox时,条形码消失了。

PS:我在全局区域中将newBarcode定义为Integer ..

private void btnBarkodOlustur_Click(object sender, EventArgs e)
{
    BarcodeGenerator();
    string _newBarcode = newBarcode.ToString();
    if (context.Stocks.Any(c => c.Barcode == _newBarcode) || context.Units.Any(c => c.Unit == _newBarcode))
    {
        BarcodeGenerator();
        return;
    }
    else
    {
        txtBarcode.Text = _newBarcode;
    }
}

private void BarcodeGenerator()
{
    Random rnd = new Random();
    newBarcode = rnd.Next(10000000, 99999999);
}
c# string winforms random textbox
1个回答
0
投票

我已经对您的代码进行了一些修改。单击该按钮时,将生成条形码。尽管条形码不是唯一的,但它将继续生成条形码,直到唯一为止。然后它将条形码值分配给TexttxtBarcode属性。

private Random rnd = new Random();

private void btnBarkodOlustur_Click(object sender, EventArgs e)
{   
    string _newBarcode = BarcodeGenerator();
    while (context.Stocks.Any(c => c.Barcode == _newBarcode) || context.Units.Any(c => c.Unit == _newBarcode))
    {
        _newBarcode = BarcodeGenerator();
    }

    txtBarcode.Text = _newBarcode;
}

private string BarcodeGenerator()
{
    return rnd.Next(10000000, 99999999);
}
© www.soinside.com 2019 - 2024. All rights reserved.