我是C#的新手,想尝试一些问题。我创建了一个ListView
来存储两个随机数的运算,所有这些运算都在一定的时间内发生。
这是我构造Listview
的方式:
第一列称为“开始时间”,以记录计时器开始的时间。第二列的功能相同,只是“结束时间”。现在,它运行时,static int k = 10;
ListViewItem lvi1 = new ListViewItem();
private void Bstart_Click(object sender, EventArgs e)
{
timer1.Start();
lvi1 = new ListViewItem(DateTime.Now.ToString("HH:mm:ss"));
listView2.Items.Add(lvi1);
}
private void Timer1_Tick(object sender, EventArgs e)
{
Random ran1 = new Random();
Random ran2 = new Random();
double nu1 = ran1.Next(0, 100);
double nu2 = ran2.Next(2, 270);
string op;
var operation = ran1.Next(1, 5);
double res;
if (k > 1)
{
label1.Text = k.ToString() + " seconds remaining";
}
else if (k == 1)
{
label1.Text = k.ToString() + " seconds remaining";
}
else
{
timer1.Stop();
lvi1.SubItems[1].Text = DateTime.Now.ToString("HH:mm:ss");
label1.Text = "You have 10 seconds";
//MessageBox.Show("You are out of time!", "Too Bad", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
k = 10;
}
k--;
switch (operation)
{
case 1:
res = nu1 + nu2;
op = "+";
break;
case 2:
res = nu1 - nu2;
op = "-";
break;
case 3:
res = nu1 * nu2;
op = "*";
break;
case 4:
res = nu1 / nu2;
op = "/";
break;
default:
res = 0;
op = "?";
break;
}
textBox1.Text = Convert.ToString(nu1 + " " + op + " " + nu2 + " = " + res);
listView2.Columns.Add("OP", 45, HorizontalAlignment.Center);
lvi1.SubItems.Add(res.ToString());
}
private void Bstop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
将以编程方式创建多个列以存储操作结果。
但是,程序运行时,结果将跳到保留第二列以记录“结束时间”。
我如何确保第二列为空,直到计时器结束,并且结果将从第三列开始到结尾?
代码如下:
public intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
if (!this.reflector.get<boolean>(RequestMetaData.IS_PUBLIC_ROUTE, context.getHandler())) {
return next.handle().pipe(
map(data => {
const host = context.switchToHttp();
const req = host.getRequest();
const resp = host.getResponse();
this.persistRequest(data, req, resp)
.then(() => this.logger.log(`Request logged`))
.catch(e => {
this.logger.error(`Error logging request: ${e.message}`);
});
return data;
}));
}
return next.handle();
}
答案 0 :(得分:0)
您可以添加一个占位符项,以用string.Empty
或-
填充第二列,直到您真正要填充它为止:
private void Bstart_Click(object sender, EventArgs e)
{
timer1.Start();
lvi1 = new ListViewItem(DateTime.Now.ToString("HH:mm:ss"));
listView2.Items.Add(lvi1);
// Add an empty SubItem or any placeholder here:
// lvi1.SubItems.Add(string.Empty);
lvi1.SubItems.Add("-");
}
就像我在评论中已经提到的那样,您的if(k > 1)
和else if (k == 1)
的内容完全相同。您可以合并这些:
if (k >= 1)
{
label1.Text = k.ToString() + " seconds remaining";
}
else
{
timer1.Stop();
// ...