c# - 单击另一个文本框后字符串消失
问题描述
当我点击一个按钮时,我试图生成随机整数值(条形码)。然后,如果新条形码已经存在,我将检查两个表(库存、单位)。如果它是唯一的,则新的条形码将写入文本框中。
一切正常,但是当我单击另一个 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);
}
解决方案
我对你的代码做了一些修改。单击按钮时,它将生成一个条形码。虽然条形码不是唯一的,但它将继续生成条形码,直到它是唯一的。然后它将条形码值分配给 的Text
属性txtBarcode
。
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);
}
推荐阅读
- javascript - vbox 布局在边框布局中不起作用
- numpy - 在 Tensorflow 中将 3D 张量简化为 1D 张量
- javascript - React PropTypes:数字范围
- jmeter - 如何在 Jmeter 中汇总来自不同机器的结果?
- java - Crafter CMS:上传多张图片
- python - 遗传算法 - python中的有序交叉
- javascript - 反应 - 反应路由器 - 私有路由 - 无限循环
- ios - Swift 4 Alamofire 分段上传不起作用
- javascript - Amplify JS SDK 抛出 Uncaught TypeError: Cannot read property 'crypto' of undefined
- php - 如何选择第一个 n 个字符或通过拳头“<”?