如何在特定事件期间停止单个按钮隐藏文本框?

时间:2012-12-07 23:46:36

标签: c# winforms

我有两个问题:

1)这是使变量的默认值为0的正确方法,然后将用户给出的值传递给该变量吗?

protected void btnCheck_Click(object sender, EventArgs e)
    {
        lblYesNo.Text = "";
        //default int values are set to 0
        int remainder = 0;
        int guess = 0;

        remainder = int.Parse(txtRemainder.Text);

        guess = int.Parse(txtAnswer.Text);
        answer = (int)Session["answer"];
        if (guess == answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Correct!";
        }
        else
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
    }//END Check Answer

2)如何阻止Check_Click(提交按钮)隐藏txtRemainder(文本框)?它现在'自动隐藏'的原因是因为我将默认值设置为'txtRemainder.Visible = false;'在Page_Load中,除非数学问题是除法,否则它将隐藏起来。当我点击btnDiv_Click(除法按钮)时,它会将其重置为'txtRemainder.Visible = true;',因为这提供了要解决的除法问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BasicMath : System.Web.UI.Page
{
    int number1;
    int number2;
    int answer;

    protected void Page_Load(object sender, EventArgs e)
    {
        txtRemainder.Visible = false;
    }

    protected void GetRandom()
    {
        Random rand = new Random();
        number1 = rand.Next(0, 10);
        number2 = rand.Next(0, 10);
        txtAnswer.Text = "";
        txtRemainder.Text = "";
        lblYesNo.Text = "";
    }//END Get Random Number

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        GetRandom();
        lblEquation.Text = number1.ToString() + " + " + number2.ToString();
        answer = number1 + number2;
        Session["answer"] = answer;
    }//END Addition Button

    protected void btnSub_Click(object sender, EventArgs e)
    {
        GetRandom();
        if (number2 > number1)
        {
            answer = number2 - number1;
            lblEquation.Text = number2.ToString() + " - " + number1.ToString();
        }
        else
        {
            answer = number1 - number2;
            lblEquation.Text = number1.ToString() + " - " + number2.ToString();
        }
        Session["answer"] = answer;

    }//END Subtraction Button

    protected void btnMult_Click(object sender, EventArgs e)
    {
        GetRandom();
        lblEquation.Text = number1.ToString() + " x " + number2.ToString();
        answer = number1 * number2;
        Session["answer"] = answer;
    }//END Multiplication Button

    protected void btnDiv_Click(object sender, EventArgs e)
    {

        Random rand = new Random();
        number1 = rand.Next(1, 10);
        number2 = rand.Next(1, 10);
        /*will only display the txtRemainder(textbox) while using the Divide button,
           txtRemainder will auto-hide when using another button because
           it's default setting of '.Visible = false' is placed in the Page_Load*/
        txtRemainder.Visible = true;
        lblEquation.Text = number1.ToString() + " / " + number2.ToString(); 
        answer = number1 / number2;
        Session["answer"] = answer;

    }//END Division Button

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        lblYesNo.Text = "";
        //default int values are set to 0
        int remainder = 0;
        int guess = 0;

        remainder = int.Parse(txtRemainder.Text);

        guess = int.Parse(txtAnswer.Text);
        answer = (int)Session["answer"];
        if (guess == answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Correct!";
        }
        else
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
    }//END Check Answer
}

如果这个问题没有意义,请要求澄清。

2 个答案:

答案 0 :(得分:1)

问题1:

是的,这是将变量初始化为默认值零的正确方法,尽管默认情况下int变量初始化为零,因此即使省略了赋值,它们仍然默认为零。 / p>

您用来接受用户输入的方法是正确的,但如果用户输入的值不是Int32值,则可能会抛出异常(例如3.2)。就目前而言,你没有try..catch块来处理这些异常。您可以添加这些try..catch块,也可以使用TryParse()方法检查值是否有效。例如:

protected void btnCheck_Click(object sender, EventArgs e)
{
    lblYesNo.Text = "";
    //default int values are set to 0
    int remainder = 0;
    int guess = 0;

    if (!Int32.TryParse(txtRemainder.Text, out remainder))
    {
        // do something here to inform the user that remainder is invalid
        return;
    }

    if (!Int32.TryParse(txtAnswer.Text, out remainder))
    {
        // do something here to inform the user that answer is invalid
        return;
    }

    answer = (int)Session["answer"];
    if (guess > answer)
    {
        lblYesNo.Text = lblYesNo.Text + "Try Again..";
    }
    else if (guess < answer)
    {
        lblYesNo.Text = lblYesNo.Text + "Try Again..";
    }
    else
    {
        lblYesNo.Text = lblYesNo.Text + "Correct!";
    }
}//END Check Answer

TryParse()会将值转换为输出变量,如果它是有效值&amp;返回true表示成功,否则如果无法执行转换,则会返回false

问题2:

为了在页面首次加载时隐藏txtRemainder&amp;如果点击btnDiv以外的任何按钮,则保持隐藏,这是一个建议的解决方案。首先,代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) {
        lblTest.Visible = false;
    } else {
        foreach (string ctrl in Request.Form) {
            Control c = FindControl(ctrl);
            if (c is Button) {
                txtRemainder.Visible = c.ID == "btnDiv";
                return;
            }
        }
    }
}

现在进行一些解释。页面最初加载时,标签被隐藏(通过!IsPostBack检查)。如果请求确实是回发,我们将浏览返回的表单控件的集合(包含在Request.Form集合中)&amp;检查是否存在可能导致回发的按钮。按钮在HTML中呈现为<input type="submit" .. />个元素,并且在回发时,只有点击的按钮才会在Form集合中发回,即使页面上有多个提交也是如此。可以找到更详细的解释here

现在,如果按钮确实导致回发,那么我们检查此按钮是否为btnDiv。如果是,则比较返回true&amp;显示txtRemainder。如果没有,它就被隐藏了。

这样,就不需要在每个事件处理程序中显示或隐藏txtRemainder。您甚至不需要在btnDiv的事件处理程序中将其设置为可见。

修改

根据评论中列出的要求,我改变了对问题2的回答,以便提供一种替代方法,以便在每个按钮事件中显示或隐藏标签。

答案 1 :(得分:0)

<强>问题1: 如果您使用的是.Net 4.0及更高版本,请考虑使用TryParse方法,并检查结果是否成功,这样您就可以更好地控制解析值和捕获异常 e.g。

int number = 0;
bool result = false;

result = int.TryParse("123", out number);

if (!result)
{
    throw new InvalidCastException("Error converting number");
}

<强>问题2: 看起来您正在使用Asp.Net,因此请尝试在Page_Load事件中测试Page.IsPostBack,这样您就可以确定页面是否因初始加载而加载,或者因为您将数据发送到服务器(即点击)一个按钮。

希望这有帮助!