Reputation: 33
This is the code:
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();
}
}
When I used listBox1
SelectedIndexChanged
event with the same code there were no problems.
But now when I click and select item in the listView1
, I am getting the exception on the line:
var xCoord = float.Parse(parts[0]);
Input string was not in a correct format.
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:
In this case, I clicked/selected the first item and I see that the variable item contains:
{ListViewItem: {281,145}}
Upvotes: 0
Views: 962
Reputation: 66459
Each item
is a ListViewItem
object, and the implementation of ListViewItem.ToString()
is:
return "ListViewItem: {" + Text + "}";
And Text
runs this snippet:
if (SubItemCount == 0)
return string.Empty;
else
return subItems[0].Text;
So you get "ListViewItem: {281,145}}", where "{281,145}" is the result of calling Text
on the first sub item in the list.
I don't know exactly what you've inserted into your list, but assuming it's just a set of points entered as strings, you can try this instead:
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]);
...
And if there's any chance your list item might have non-numeric characters in it, consider using float.TryParse()
instead. You can test the input value and if it's not a number, take some alternative action.
Upvotes: 2