得到帮助..在编码 [英] got help.. in coding
问题描述
private void textBox4_TextChanged(object sender, EventArgs e)
{
double rank;
if(Double.TryParse(textBox4.Text, out rank))
{
if (rank < TimeSpan.Parse("00:00:15.200").TotalMilliseconds) { textBox5.Text = "Excellent"; }
else if (rank >= TimeSpan.Parse("00:00:15.200").TotalMilliseconds && rank < TimeSpan.Parse("00:00:16.100").TotalMilliseconds) { textBox5.Text = "Above Average"; }
else if (rank >= TimeSpan.Parse("00:00:16.200").TotalMilliseconds && rank < TimeSpan.Parse("00:00:18.100").TotalMilliseconds) { textBox5.Text = "Average"; }
else if (rank >= TimeSpan.Parse("00:00:18.200").TotalMilliseconds && rank < TimeSpan.Parse("00:00:19.300").TotalMilliseconds) { textBox5.Text = "Below Average"; }
else if (rank >= TimeSpan.Parse("00:00:19.300").TotalMilliseconds){ textBox5.Text = "Poor"; }
else { textBox5.Text = "Unknown - Error"; }
}
}
i已经为我的代码做了这个..但是当我的text5.text保持空白时出现了问题..我确实尝试将它放在按钮中但没有运气..它保持不变..真的..请帮助..
例如,00:00:15:200 ..它是秒表值..这是我从其他形式转移的......
i already did this for my code.. but the problem occurred when my text5.text remained blank.. i did try to put it in button but no luck.. it remained the same.. really.. please help..
for example, 00:00:15:200.. it is stopwatch value.. this is wat i transferred from other form...
推荐答案
您输入textbox4的值不是double,因此
The value you are entering into textbox4 is not a double, so
Double.TryParse(textBox4.Text, out rank)
将始终返回 false
- 并且你的textBox5将保持空白。
您正在将输入与
will always return false
- and your textBox5 will remain blank.
You are comparing the input against things like
TimeSpan.Parse("00:00:15.200").TotalMilliseconds
所以输入必须是一个表示毫秒的双倍...例如尝试输入15200
或
将if语句更改为
so the input must be a double that represents milliseconds ... e.g. try entering 15200
OR
change the if statement to
if (Double.TryParse(TimeSpan.Parse(textBox4.Text).TotalMilliseconds.ToString(), out rank))
编辑 - 修正了编译错误 - 看突出显示的代码
[最终编辑...]这是我用来试验你的问题的代码。我添加了几行,如果使用了所有冒号,将输入更改为正确的格式。
换句话说,如果您输入格式00:00:15:200或00:00:15.200它仍然可以工作。
EDIT - fixed the compile error - see highlighted code
[Final Edit ...] This is the code I used to try out your problem. I've added a couple of lines that will change the input to the correct format if all colons have been used.
In other words, if you enter in the format " 00:00:15:200" or " 00:00:15.200" it will still work.
private void button1_Click(object sender, EventArgs e)
{
double rank;
string CheckFormatOfInput = textBox4.Text.Trim();
// could use a regex here but I'm keeping it very simple
CheckFormatOfInput = CheckFormatOfInput.Remove(8, 1).Insert(8, ".");
if (Double.TryParse(TimeSpan.Parse(CheckFormatOfInput).TotalMilliseconds.ToString(), out rank))
{
if (rank < TimeSpan.Parse("00:00:15.200").TotalMilliseconds)
{
textBox5.Text = "Excellent";
}
else if (rank >= TimeSpan.Parse("00:00:15.200").TotalMilliseconds && rank < TimeSpan.Parse("00:00:16.100").TotalMilliseconds)
{
textBox5.Text = "Above Average";
}
else if (rank >= TimeSpan.Parse("00:00:16.200").TotalMilliseconds && rank < TimeSpan.Parse("00:00:18.100").TotalMilliseconds)
{
textBox5.Text = "Average";
}
else if (rank >= TimeSpan.Parse("00:00:18.200").TotalMilliseconds && rank < TimeSpan.Parse("00:00:19.300").TotalMilliseconds)
{
textBox5.Text = "Below Average";
}
else if (rank >= TimeSpan.Parse("00:00:19.300").TotalMilliseconds)
{
textBox5.Text = "Poor";
}
else
{
textBox5.Text = "Unknown - Error";
}
}
}
好的 - 让我们看一下。
问题是文本框中的值不是双倍的:它是一个秒表值,你可以从另一个表单获得并通过文本框传输 - 这是一件很奇怪的事情,但没关系。
因此将其解析为时间跨度:
OK - let's look at it.
The problem is that the value in your textbox is not a double: it's a stopwatch value, which you get from a different form and transfer via a textbox - which is an odd thing to do, but never mind.
So parse it as a timespan:
TimeSpan rank;
if (TimeSpan.TryParseExact(textBox4.Text, "hh\\:mm\\:ss\\:fff", null, out rank))
{
...
}
比较排名与Timespan值直接:
And compare rank against your Timespan values directly:
if (rank < TimeSpan.Parse("00:00:15.200")) { textBox5.Text = "Excellent"; }
Mistyped。 for: - OriginalGriff [/ edit]
[edit]Mistyped "." for ":" - OriginalGriff[/edit]
这篇关于得到帮助..在编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!