我有一个几乎没有组合框,texbox等的winform ...所以我想通过点击btnAdd来增加txtTotal中txtStake的值,但是每当我点击btnAdd时我也希望乘以结果。
//For example:
//click-1, txtStake = 2, txtTotal = 2;
//click-2, txtStake = 2, txtTotal = 4;
//click-3, txtStake = 3, txtTotal = 12;
//click-3, txtStake = 4.5 txtTotal = 54; Ok? How to do that? Thanks.
--------------------------------------------------------------------
class Multy
{
public decimal Stake;
public Multy()
{}
}
private List<Multy> matches = new List<Multy>();
private void btnAdd_Click(object sender, EventArgs e)
{
matches = new List<Multy>();
Multy match = new Multy();
match.Stake = decimal.Parse(txtStake.Text);
matches.Add(match);
decimal l_Stake;
string strStake;
string strOutputLine;
foreach (Multy m in matches)
{
l_Stake = m.Stake;
strStake = l_Stake.ToString();
}
txtTotal.text = //code?
}
答案 0 :(得分:0)
我真的不明白你为什么要在列表中添加一个元素然后使用foreach ...如果你想将txtStake
值乘以txtTotal
的当前值,那么就这样做吧以下内容:
decimal result,value;
if(decimal.TryParse(txtStake.Text,out result)
&& decimal.TryParse(txtTotal.Text,out value))
txtTotal.Text = (result * value).ToString();
else if(decimal.TryParse(txtStake.Text,out result))
txtTotal.Text = result.ToString();
您的Multy
课程也毫无意义,您可以使用List<decimal>
答案 1 :(得分:0)
你基本上要求这个(没有错误处理,随意改进它):
private void btnAdd_Click(object sender, EventArgs e)
{
var stake = decimal.Parse(txtStake.Text);
var total = decimal.Parse(txtTotal.Text);
total *= stake;
txtTotal.Text = total.ToString();
}
但是你的代码中有很多东西。 Multy
? List
? foreach
?
答案 2 :(得分:0)
你可能想要这样的东西:
private List<Multy> matches = new List<Multy>();
private void btnAdd_Click(object sender, EventArgs e)
{
// do NOT create a new 'matches' list, use the existing one
Multy match = new Multy();
match.Stake = decimal.Parse(txtStake.Text);
// you should use "TryParse" here, what if the user entered "A"?
matches.Add(match);
decimal runningTotal = 1m; // base value
foreach (Multy m in matches)
{
runningTotal = runningTotal * m.Stake;
}
txtTotal.text = runningTotal.ToString();
}