不断收到此错误:事件“System.Windows.Controls.Primitives.ToggleButton.Checked"只能出现在 += 或 -= 的左侧 [英] Keep getting this error: The event 'System.Windows.Controls.Primitives.ToggleButton.Checked' can only appear on the left hand side of += or -=
问题描述
在制作如下程序时,我不断收到此错误:
I keep getting this error when making the program as given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace simplecalc
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
int a, b, c;
a = int.Parse(textBox1.Text);
b = int.Parse(textBox2.Text);
if (rbadd.Checked == true)
c = a + b;
else if (rbsubtract.Checked == true)
c = a - b;
else if (rbdivide.Checked == true)
c = a / b;
else
c = a * b;
textBox3.Text = c.ToString();
}
}
}
我正在用 WPF 用 C# 制作一个基本的计算器.我对 C# 很陌生.
I am in the process of making a basic calculator in C# in WPF. I am very new to C#.
推荐答案
Checked
这里是一个事件,而不是一个bool
,发生了何时被检查.您需要一个不同的属性 - 大概是 IsChecked
.
Checked
here is an event, not a bool
, that happens when it is checked. You need a different property - presumably IsChecked
.
小注,但在风格上,通常最好不要将布尔值与 true
/false
进行比较,而是:
Minor note, but stylistically, it is usually preferable not to compare booleans to true
/false
, but rather:
if (rbadd.IsChecked)
c = a + b;
else if (rbsubtract.IsChecked)
c = a - b;
else if (rbdivide.IsChecked)
c = a / b;
等;或者如果您想测试 false
:if(!rbadd.IsChecked)
.
etc; or if you wanted to test for false
: if(!rbadd.IsChecked)
.
这篇关于不断收到此错误:事件“System.Windows.Controls.Primitives.ToggleButton.Checked"只能出现在 += 或 -= 的左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!