创建包含两个文本框和两个按钮的Windows应用程序

7nbnzgx9  于 2023-03-09  发布在  Windows
关注(0)|答案(6)|浏览(122)

我搞砸了我的代码,当我试图把字体颜色黄色。请帮助!!
说明:创建一个包含两个文本框和两个按钮的Windows应用程序。文本框应用于允许用户输入两个正数值。按钮应标记为"加"和"乘"。创建事件处理程序方法以检索值、执行计算、并将计算结果显示在标签上。结果标签最初应设置为不可见,字体颜色为黄色。如果输入了无效数据,请将结果标签上的字体颜色更改为红色,并显示消息"值必须为数字且〉0"。显示最终答案时,字体颜色应为黄色。文本框标题需要其他标签。不允许输入非数字字符。调用 * TryParse()* 方法来获取值。所有涉及程序语句的控件都应该被命名。文本框中的值右对齐。
这是我的表格1页上的内容

namespace Add_and_Multiply
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            val1 = Convert.ToInt32(textBox1.Text);
            val2 = Convert.ToInt32(textBox2.Text);
            label3.Text = Convert.ToString(val1 + val2); //after this line I tried putting the code to give me yellow color text and now I have so many errors:(
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            val1 = Convert.ToInt32(textBox1.Text);
            val2 = Convert.ToInt32(textBox2.Text);
            label3.Text = Convert.ToString(val1 * val2);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

有人能帮帮我吗?我不太擅长这个。谢谢
有人叫我把它装成这样。

namespace Add_and_Multiply
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            if(!int.TryParse(textBox1.Text.Trim(), out val1))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "First value is invalid";
                return;
            }
            if(!int.TryParse(textBox2.Text.Trim(), out val2))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "Second value is invalid";
                return;
            }
            lblYellow.Visible = false;
            label3.Text = Convert.ToString(val1 + val2);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            if(!int.TryParse(textBox1.Text.Trim(), out val1))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "First value is invalid";
                return;
            }
            if(!int.TryParse(textBox2.Text.Trim(), out val2))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "Second value is invalid";
                return;
            }
            lblYellow.Visible = false;
            label3.Text = Convert.ToString(val1 * val2);

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

但是我得到一个错误"the name 'lblYellow' dose not exist in the current context Form1.cs"我应该在私有void button1_click行之前声明它吗?

iszxjhcz

iszxjhcz1#

你有什么错误?,我认为你必须删除一个}行后:

label3.Text = Convert.ToString(val1 + val2);
wooyq4lh

wooyq4lh2#

label3.ForeColor = Color.Yellow;

...应将标签颜色更改为黄色...
但是您还有另一个问题,您没有检查无效输入以将标签更改为红色,也没有将TryParse作为instructions命令调用。
此外,说明书还指出,用户应该能够输入任何正数值。这将包括 * 8. 9 * 和 * 216. 75 *。我想如果你想要最好的成绩,你将需要考虑到这一点。看看C# built-in data types

cu6pst1q

cu6pst1q3#

看起来您正在定义一个没有类或在类外部的方法,请检查所有方法是否都在Form1内部,并检查是否有额外的或丢失的括号。

kmbjn2e3

kmbjn2e34#

如果有人在这个项目上遇到麻烦,那么这里是最终产品。它看起来很不错,一旦运行。
一切都工作得很好,它应该如何!
{公共分部类Form 1:窗体{公共窗体1(){初始化组件();}

private void button1_Click(object sender, EventArgs e)
    {
        int val1;
        int val2;
        int valid1 = 0;
        int valid2 = 0;
        if(int.TryParse(textBox1.Text.Trim(), out val1))
        {
            if(val1>0)
            {
                valid1 = 1;
                caption1.ForeColor = Color.Yellow;
                caption1.Visible = true;
                caption1.Text = "First value is valid"; 
            }
            else
            {
                caption1.ForeColor = Color.Red;
                caption1.Visible = true;
                caption1.Text = "Value must be Greater than 0";
            }
        }
        else
        {
            caption1.ForeColor = Color.Red;
            caption1.Visible = true;
            caption1.Text = "Value must be Numeric";
        }
        if(int.TryParse(textBox2.Text.Trim(), out val2))
        {
            if (val2 > 0)
            {
                valid2 = 1;
                caption2.ForeColor = Color.Yellow;
                caption2.Visible = true;
                caption2.Text = "First value is valid";
            }
            else
            {
                caption2.ForeColor = Color.Red;
                caption2.Visible = true;
                caption2.Text = "Value must be Greater than 0";
            }
        }
        else
        {
            caption2.ForeColor = Color.Red;
            caption2.Visible = true;
            caption2.Text = "Value must be Numeric";
        }
        if((valid1 > 0)&& (valid2 > 0))
        {
            label3.Visible = true;
            label3.ForeColor = Color.Yellow;
            label3.Text = Convert.ToString(val1 + val2);
        }
        else
        {
            label3.Visible = false;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        int val1;
        int val2;
        int valid1 = 0;
        int valid2 = 0;
        if(int.TryParse(textBox1.Text.Trim(), out val1))
        {
            if (val1 > 0)
            {
                valid1 = 1;
                caption1.ForeColor = Color.Yellow;
                caption1.Visible = true;
                caption1.Text = "First value is valid";
            }
            else
            {
                caption1.ForeColor = Color.Red;
                caption1.Visible = true;
                caption1.Text = "Value must be Greater than 0";
            }
        }
        else
        {
            caption1.ForeColor = Color.Red;
            caption1.Visible = true;
            caption1.Text = "Value must be Numeric";
        }
        if(int.TryParse(textBox2.Text.Trim(), out val2))
        {
            if (val2 > 0)
            {
                valid2 = 1;
                caption2.ForeColor = Color.Yellow;
                caption2.Visible = true;
                caption2.Text = "First value is valid";
            }
            else
            {
                caption2.ForeColor = Color.Red;
                caption2.Visible = true;
                caption2.Text = "Value must be Greater than 0";
            }
        }
        else
        {
            caption2.ForeColor = Color.Red;
            caption2.Visible = true;
            caption2.Text = "Value must be Numeric";

        }
        if ((valid1 > 0) && (valid2 > 0))
        {
            label3.Visible = true;
            label3.ForeColor = Color.Yellow;
            label3.Text = Convert.ToString(val1 * val2);
        }
        else 
        {
            label3.Visible = false;
        }
         }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
}

}

gwo2fgha

gwo2fgha5#

我知道这是两年后的事了,但我想人们可能还在寻求帮助。

private void btnAdd_Click(object sender, EventArgs e)
{
    int firstNumber, secondNumber;

    if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
    {
        lblResults.Text = "First number needs\n to be numeric and > 0";
        lblResults.ForeColor = Color.Red;
        lblResults.Visible = true;
        txtBxFirstNumber.Clear();
        return;
    }
    if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) || secondNumber < 1)
    {
        lblResults.Text = "Second number need\n to be numeric and > 0";
        lblResults.ForeColor = Color.Red;
        lblResults.Visible = true;
        txtBxSecondNumber.Clear();
        return;
    }
    lblResults.ForeColor = Color.Yellow;
    lblResults.Visible = true;
    lblResults.Text = Convert.ToString(firstNumber + secondNumber);
}

private void btnMultiply_Click(object sender, EventArgs e)
{
    int firstNumber, secondNumber;

    if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
    {
        lblResults.Text = "First number needs\n to be numeric";
        lblResults.ForeColor = Color.Red;
        lblResults.Visible = true;
        txtBxFirstNumber.Clear();
        return;
    }
    if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) && secondNumber < 1)
    {
        lblResults.Text = "Second number need\n to be numeric";
        lblResults.ForeColor = Color.Red;
        lblResults.Visible = true;
        txtBxSecondNumber.Clear();
        return;
    }
    lblResults.ForeColor = Color.Yellow;
    lblResults.Visible = true;
    lblResults.Text = Convert.ToString(firstNumber * secondNumber);
}
ctehm74n

ctehm74n6#

您应该将该标签的设计名称更改为“lblYellow”,然后将工作...后藤该标签的属性并向下滚动,其中有一个名为”设计“的部分,您会发现“(名称)”将默认名称更改为“lblYellow ...”
enter image description here

相关问题