简体   繁体   中英

Using radio buttons in a calculator

I am realy new to C#. I have tried for days now to find out how to use radio buttons in my calculator. I am making an calculator that works by choosing a radio button.

I have tried allmost everything from tutorials to schoolbooks.

Hope you can help me out.

This is one of my failed codes

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

        private void button1_Click(object sender, EventArgs e)
        {
            int x,
                y;

            x = Convert.ToInt16(textBox1.Text);
            y = Convert.ToInt16(textBox2.Text);

            if (radioButton1.Checked) ;

            Math.Pow(x,y);

            if (radioButton2.Checked) ;
            (x / y);

            if (radioButton3.Cheked) ; 

        }
    }
}

In this case I allways get the error

Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

I really don't know what to do.

You should assing result of calculation into some variable

private void button1_Click(object sender, EventArgs e)
{
    double result;
    int x, y;

    x = Convert.ToInt16(textBox1.Text);
    y = Convert.ToInt16(textBox2.Text);

    if (radioButton1.Checked)
        result = Math.Pow(x,y);

    if (radioButton2.Checked)
       result = (x / y);

    //...

}

You need to assign the result to some variable or a textbox (if you have one):

txtboxResult.Text = Math.Pow(x,y).ToString();

There are actually many issues.

private void button1_Click(object sender, EventArgs e)
{
    int x,
        y;

    x = Convert.ToInt16(textBox1.Text);
    y = Convert.ToInt16(textBox2.Text);

    if (radioButton1.IsChecked)    //removed `;`, it refers to the empty if block and 'IsChecked' is a property not 'Checked'
        txtboxResult.Text = Math.Pow(x,y).ToString();    //assign result to a textbox or may be a variable

    if (radioButton2.IsChecked)    //removed `;`
        (x / y);

    if (radioButton3.IsChecked) ; 
}

The Checked is an event, not a property (the event fired when the radio button get checked). The property is IsChecked

if(radioButton1.IsChecked.GetValueOrDefault(false))
    ...

i think that the error is in this statement

if (radioButton2.IsChecked)
        (x / y);

it should be like this

if (radioButton2.IsChecked)
{
    txtboxResult.Text = Convert.toString(x / y);
}

Populating result in another textbox as below.

    int x,y;

    double res = 0.0;

    x = Convert.ToInt16(textBox1.Text);
    y = Convert.ToInt16(textBox2.Text);

    if (radioButton1.Checked)

        res = Math.Pow(x, y);

    if (radioButton2.Checked)
        res = (x / y);

    if (radioButton3.Checked) ;

    textBox3.Text = res.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM