计算器总计失败

Has*_*san 1 c# wpf

我正在使用C#编写计算器.

namespace Calculator
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public void Total(string num)   
        {
            lblTotal.Content = Convert.ToString(lblTotal.Content) + num;
        }
        private void one_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(1) );
        }
        private void two_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(2) );
        }
        private void three_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(3) );
        }
        private void four_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(4) );
        }
        private void five_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(5) );
        }
        private void six_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(6) );
        }
        private void seven_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(7) );
        }
        private void eight_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(8) );
        }
        private void nine_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(9) );
        }
        private void ten_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(10) );
        }
        private void zero_Click(object sender, RoutedEventArgs e)
        {
            Total( Convert.ToString(0) );
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            Total("+");
        }

        private void subtract_Click(object sender, RoutedEventArgs e)
        {
            Total("-");
        }

        private void multiply_Click(object sender, RoutedEventArgs e)
        {
            Total("*");
        }

        private void divide_Click(object sender, RoutedEventArgs e)
        {
            Total("/");
        }

        private void total_Click(object sender, RoutedEventArgs e)
        {
            int calculate;
            string content;

            content = Convert.ToString(lblTotal.Content);
            calculate = Convert.ToInt32(content);

        }

    }
Run Code Online (Sandbox Code Playgroud)

}

因此,当用户单击总按钮时,它应该打开lblTotal.Content并获得计算所需的数学并将lblTotal.Content设置为答案.

我收到此错误:

System.FormatException was unhandled
  _HResult=-2146233033
  _message=Input string was not in a correct format.
  HResult=-2146233033
  IsTransient=false
  Message=Input string was not in a correct format.
  Source=mscorlib
  StackTrace:
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
       at System.Convert.ToInt32(Object value)
       at Calculator.MainWindow.total_Click(Object sender, RoutedEventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2013\Projects\Calculator\Calculator\MainWindow.xaml.cs:line 100
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at Calculator.App.Main() in c:\Users\Administrator\Documents\Visual Studio 2013\Projects\Calculator\Calculator\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

我希望有一个解决办法.一旦我单击总按钮,程序崩溃,然后我得到上面的错误.

Mit*_*tch 5

您正尝试使用Convert.ToInt32转换2+57.那不是什么Convert.ToInt32意思. Convert.ToInt32int.Parse获取一个表示数字(12345-1245)的字符串并将其转换为整数.

在执行任何其他操作之前,您应该查看数据类型.

您需要自己解析表达式,或使用表达式解析器.看我需要一个快速运行时表达式解析器如何在c#中创建表达式解析器?

有关如何在解析表达式的情况下在WPF中创建计算器的示例,请参阅MSDN 示例计算器.