我觉得这是一堆你知道我的意思。它的工作原理,但我觉得我在页面生命周期(加载和回发)方面做得太过分了,甚至我在每个if语句中都有冗余。
这是怎么回事:
所以我也在这之后调用这个方法 用户声称点(提交)下降 这些积分来自他们下一次的总计。所以 根据他们的总积分 帐户,我需要启用/禁用这些 页面从上次提交后刷新后的单选按钮
private void SetPointsOptions()
{
int totalPoints = customer.TotalPoints;
rbn200Points.Text = "200 pts";
rbn250Points.Text = "250 pts";
rbn400Points.Text = "400 pts";
rbn500Points.Text = "500 pts";
rbn600Points.Text = "600 pts";
// clear state of radio buttons & disable submit
if (totalPoints < 200)
{
rbn200Points.Enabled = false;
rbn250Points.Enabled = false;
rbn400Points.Enabled = false;
rbn500Points.Enabled = false;
rbn600Points.Enabled = false;
rbn200Points.Checked = false;
rbn250Points.Checked = false;
rbn400Points.Checked = false;
rbn500Points.Checked = false;
rbn600Points.Checked = false;
btnClaimRewardPoints.Enabled = false;
return;
}
if(totalPoints >= 200 && totalPoints < 250)
{
rbn200Points.Enabled = true;
}
else if(totalPoints >= 250 && totalPoints < 400)
{
rbn200Points.Enabled = true;
rbn250Points.Enabled = true;
}
else if(totalPoints >= 400 && totalPoints < 500)
{
rbn200Points.Enabled = true;
rbn250Points.Enabled = true;
rbn400Points.Enabled = true;
}
else if(totalPoints >= 500 && totalPoints < 600)
{
rbn200Points.Enabled = true;
rbn250Points.Enabled = true;
rbn400Points.Enabled = true;
rbn500Points.Enabled = true;
}
else if(totalPoints >= 600)
{
rbn200Points.Enabled = true;
rbn250Points.Enabled = true;
rbn400Points.Enabled = true;
rbn500Points.Enabled = true;
rbn600Points.Enabled = true;
}
}
答案 0 :(得分:8)
鉴于我没有错过任何重要的代码:
private void SetPointsOptions()
{
int totalPoints = customer.TotalPoints;
rbn200Points.Enabled = totalPoints >= 200;
rbn250Points.Enabled = totalPoints >= 250;
rbn400Points.Enabled = totalPoints >= 400;
rbn500Points.Enabled = totalPoints >= 500;
rbn600Points.Enabled = totalPoints >= 600;
}
答案 1 :(得分:2)
我不能说你如何/何时/何时想要发起这一切,但是有明确的冗长可以减少。例如,您启用各种按钮的最后一位可以简化为:
if(totalPoints > 200)
{
rbn200Points.Enabled = true;
}
if(totalPoints > 250)
{
rbn250Points.Enabled = true;
}
if(totalPoints > 400)
{
rbn400Points.Enabled = true;
}
if(totalPoints > 500)
{
rbn500Points.Enabled = true;
}
if(totalPoints > 600)
{
rbn600Points.Enabled = true;
}
答案 2 :(得分:2)
我暂时没有接触过C#并且手头没有VS,但它应该是这样的。
var points2buttons = new Dictionary<int, RadioButton>();
points2buttons[200] = rbn200Points;
points2buttons[250] = rbn250Points;
...
foreach (var pointsButton in points2buttons) {
var button = pointsButton.Value;
var pts = pointsButton.Key;
button.Text = pts + " pts";
button.Checked = totalPoints>pts;
}
...
使用反射,您甚至可以自动填充字典。
答案 3 :(得分:2)
将单选按钮及其关联的点值放在字典中可能会有所帮助:
// Untested.
int totalPoints = customer.TotalRewardPoints;
var radioButtons = new Dictionary<RadioButton, Int32>();
radioButtons.Add(rbn200Points, 200);
radioButtons.Add(rbn250Points, 250);
radioButtons.Add(rbn400Points, 400);
radioButtons.Add(rbn500Points, 500);
radioButtons.Add(rbn600Points, 600);
foreach (var keyValuePair in radioButtons)
{
keyValuePair.Key.Text = String.Format("{0} pts", keyValuePair.Value);
keyValuePair.Key.Enabled = (keyValuePair.Value < totalPoints);
keyValuePair.Key.Checked = false;
}
答案 4 :(得分:1)
好吧,你可以先不检查if else,然后使用
if (totalpoints >= val) control.enable
这将允许您减少重复启用
答案 5 :(得分:0)
您可以在C#中按如下方式分配多个属性:
if ( some condition )
{
rbnA.Enabled = rbnB.Enabled = rbnC.Enabled = rbnD.Enabled = true;
} else {
rbnA.Enabled = rbnB.Enabled = rbnC.Enabled = rbnD.Enabled = false;
}