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



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

一切正常,但当我点击表单的另一个文本框时,条形码就会消失

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);
}

我对您的代码进行了一些修改。当点击该按钮时,它将生成一个条形码。虽然条形码不是唯一的,但它将继续生成条形码,直到它是唯一的。然后将条形码值分配给txtBarcodeText属性。

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);
}

最新更新