0

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

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()
4

2 回答 2

2

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

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

int outValue;
if (int.TryParse(cbxBuyEU.Text, out outValue))
{
    // Then the value is OK and outValue contains the parsed value
}
于 2012-12-20T12:36:51.030 回答
0
int n = Convert.ToInt32(cbxBuyEU.Text)
于 2012-12-20T12:36:21.090 回答