I am coding a calculator using 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);
}
}
}
So when a user clicks the total button, it should open the lblTotal.Content and get the math it needs to calculate and set the lblTotal.Content to the answer.
I get this error:
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:
I hope there is a fix. As soon as I click the total button, the program crashes and then I get the error above.