C# ListView的SelectedIndexChanged发生FormatException

C# ListView的SelectedIndexChanged发生FormatException,c#,winforms,C#,Winforms,代码如下: private void listView1_SelectedIndexChanged(object sender, EventArgs e) { foreach (object item in listView1.SelectedItems) { string curItem = item.ToString(); var parts = curItem.S

代码如下:

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();
            }
        }
当我使用相同代码的
listBox1
SelectedIndexChanged
事件时,没有问题。 但现在,当我单击并选择
列表视图1
中的项目时,我得到了一个异常:

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}}

每个
item
都是一个
ListViewItem
对象,
ListViewItem.ToString()的实现是:

return "ListViewItem: {" + Text + "}";
然后
Text
运行此代码段:

if (SubItemCount == 0)
    return string.Empty;
else
    return subItems[0].Text;
因此您得到了“ListViewItem:{281145}}”,其中“{281145}”是对列表中的第一个子项调用
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]);

    ...

如果您的列表项中可能有非数字字符,请考虑使用<代码>浮点.TyPARSESER()< <代码>。您可以测试输入值,如果它不是一个数字,则采取一些替代措施。

我认为逗号的小数点是错误的。使用调试器,找出包含哪些部分。