C#,combox,switch int.Parse导致错误

时间:2012-12-20 12:34:18

标签: c# parsing combobox switch-statement

我正在尝试使用组合框编写一个简单的程序。但运行程序时,下拉菜单中没有可用的选项。此外,我认为问题开始时我尝试在程序开头解析整数到文本。但是我还不够熟练解决这个问题:(。以下是我的代码和视觉工作室的错误:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testerv1._01
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnBuyEU_Click(object sender, EventArgs e)
    {
        int n = int.Parse(cbxBuyEU.Text);
        int price = 0;
        switch (n)
        {
            case 1:
                price += 25;
                break;
            case 2:
                price += 25;
                goto case 1;
            case 3:
                price += 50;
                goto case 1;
            default:
                MessageBox.Show("you made a wrong choice..");
                break;
        }
        if (price != 0)
        {
            MessageBox.Show("deposit "+ price +"");
        }
        MessageBox.Show("thank you and good buy");

    }
}
}

以下是错误:

System.FormatException was unhandled
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.Int32.Parse(String s)
   at testerv1._01.Form1.btnBuyEU_Click(Object sender, EventArgs e) in C:\Documents and Settings\jjj\my documents\visual studio 2010\Projects\testerv1.01\testerv1.01    \Form1.cs:line 21
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at testerv1._01.Program.Main() in C:\Documents and Settings\jjj\my documents\visual studio 2010\Projects\testerv1.01\testerv1.01\Program.cs:line 18
   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.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

2 个答案:

答案 0 :(得分:2)

错误信息非常清楚,输入字符串值无效。

您应该使用TryParse来检查该值是否有效。 Parse假定参数是一个有效的整数,在您的情况下不是这样。

int outValue;
if (int.TryParse(cbxBuyEU.Text, out outValue))
{
    // Then the value is OK and outValue contains the parsed value
}

答案 1 :(得分:0)

int n = Convert.ToInt32(cbxBuyEU.Text)