2014-01-08 166 views
0

的的SelectedIndexChanged這是代碼:出現FormatException發生在ListView控件

private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      foreach (object item in listView1.SelectedItems) 
      { 
       string curItem = item.ToString(); 
       var parts = curItem.Split("{}XY=, ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
       var xCoord = float.Parse(parts[0]); 
       var yCoord = float.Parse(parts[1]); 
       var point = new PointF(xCoord, yCoord); 
       coordinates.Add(point); 
       CloudEnteringAlert.pointtocolor = coordinates; 
       pictureBox1.Invalidate(); 
      } 
     } 

當我用listBox1SelectedIndexChanged事件具有相同的代碼沒有問題。 但現在,當我在listView1單擊,然後選擇項目,我正在線路上的異常:

var xCoord = float.Parse(parts[0]); 

輸入字符串的不正確的格式。

System.FormatException was unhandled 
    HResult=-2146233033 
    Message=Input string was not in a correct format. 
    Source=mscorlib 
    StackTrace: 
     at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) 
     at System.Single.Parse(String s) 
     at Find_Distance.Form1.listView1_SelectedIndexChanged(Object sender, EventArgs e) in d:\C-Sharp\FindDistance\Find Distance\Find Distance\Form1.cs:line 382 
     at System.Windows.Forms.ListView.OnSelectedIndexChanged(EventArgs e) 
     at System.Windows.Forms.ListView.WmReflectNotify(Message& m) 
     at System.Windows.Forms.ListView.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.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m) 
     at System.Windows.Forms.Control.WmNotify(Message& m) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ScrollableControl.WndProc(Message& m) 
     at System.Windows.Forms.Form.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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) 
     at System.Windows.Forms.Control.DefWndProc(Message& m) 
     at System.Windows.Forms.ListView.WmMouseDown(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.ListView.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 Find_Distance.Program.Main() in d:\C-Sharp\FindDistance\Find Distance\Find Distance\Program.cs:line 19 
     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: 

在這種情況下,我點擊/選擇的第一項和我看到變量項目包含:

{ListViewItem: {281,145}} 
+0

錯誤的本地逗號作爲小數點我想象。 –

+1

使用調試器,找出*部件*包含的內容。 –

回答

2

每個itemListViewItem對象,的ListViewItem.ToString()實現如下:

return "ListViewItem: {" + Text + "}"; 

Text運行此代碼段:

if (SubItemCount == 0) 
    return string.Empty; 
else 
    return subItems[0].Text; 

因此,您將得到「ListViewItem:{281,145}}」,其中「{281,145}」是在列表中的第一個子項上調用Text的結果。


我完全不知道你已經插入到你的名單是什麼,但假設它只是一組輸入爲串點,可以改爲嘗試這個辦法:

foreach (object item in listView1.SelectedItems) 
{ 
    string curItem = item.Text; 
    var parts = curItem.Split(','); 

    var xCoord = float.Parse(parts[0]); 
    var yCoord = float.Parse(parts[1]); 

    ... 

如果有如果您的列表項目可能包含非數字字符,請考慮使用float.TryParse()。您可以測試輸入值,如果它不是數字,則採取一些替代操作。

相關問題