2012-12-20 31 views
0

我想用一個組合框編寫一個簡單的程序。但程序運行時,下拉菜單中沒有選擇。此外,我認爲這個問題開始,而我試圖解析一個整數到文本在程序的開始。但是我還沒有足夠的技術來解決這個:(在下面是我的代碼,並從Visual Studio中的錯誤:C#,combox,開關int.Parse導致錯誤

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() 
+1

字符串是怎麼看的?這是'int n = int.Parse(cbxBuyEU.Text);'中的解析錯誤。 – flindeberg

回答

2

的錯誤信息很清楚,輸入字符串值無效

您應該使用TryParse來檢查該值是否有效Parse假設參數是一個有效的整數,你的情況。

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

嗨Ken2k謝謝! Ups。很快發佈了評論。你可能已經猜到了,我在編程方面有點新意。然而它現在可以在你的幫助之後工作:)。 這裏是我把它代替我的原代碼: 'code' int n; int.TryParse(cbxBuyEU.Text,out n); int price = 0; switch(n) { case 1:.... 此外,我還需要在窗體視圖中添加組合框任務「1,2,3」,然後才能工作。我不確定這是否是最理想的解決方案,但它工作:) – user1918714

0
int n = Convert.ToInt32(cbxBuyEU.Text) 
+0

這也適用。謝謝El Genieben – user1918714