2

Am quite new to this, so please help. I have the following image clicked code. However, an error will occur if the user does not click on a image. How can i do an error check to catch that if the user does not click on an image and attempts to proceed, a messagebox will display notifying him to click an image.

Error msg: The error is at "ListViewItem selectedItem = listView1.SelectedItems[0] Error Msg: Invalid Argument = Value of '0' is not valid for 'index

Below is my code:

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { int i = e.ProgressPercentage; object fbUserObject = e.UserState; if (fbUserObject is DataRow) { var fbUser = fbUserObject as DataRow; var item = new ListViewItem(fbUser["friend_name"].ToString()); item.Tag = fbUser["friend_id"]; item.ImageIndex = i; listView1.Items.Add(item); } else if (fbUserObject is Image) { imageList.Images.Add(fbUserObject as Image); } } private void imageClicked(Object sender, System.EventArgs e) { ListViewItem selectedItem = listView1.SelectedItems[0]; selectedFBId = selectedItem.Tag as string; selectedFBName = selectedItem.Text; DialogResult dialogA = MessageBox.Show("Analyse employee data?", "SOC", MessageBoxButtons.YesNo); if (dialogA == DialogResult.Yes) { TargetEmployee.Text = "Selected Target: " + selectedFBName; pf.Show(); ThreadPool.QueueUserWorkItem(LoadUserDetails); } } 
6
  • 1
    Where does the exception occur? In the implementation of LoadUserDetails? Commented Jul 22, 2013 at 12:20
  • 9
    you don't need to catch an exception, you need to handle when the item is null Commented Jul 22, 2013 at 12:22
  • which line is giving you error? Commented Jul 22, 2013 at 12:27
  • The error is at "ListViewItem selectedItem = listView1.SelectedItems[0] Commented Jul 22, 2013 at 12:51
  • The error is at "ListViewItem selectedItem = listView1.SelectedItems[0] Error Msg: Invalid Argument = Value of '0' is not valid for 'index' Commented Jul 22, 2013 at 12:51

3 Answers 3

4

You shouldn't catch an exception, you should handle when there aren't any selected items

if(listView1.SelectedItems.Count == 0) { MessageBox.Show(this, "No image"); return; } 

Exceptions should be caught when you don't expect something to happen, if you are aware of a possible issue, you should handle that before it becomes an issue

Sign up to request clarification or add additional context in comments.

1 Comment

(Return exits your method)
3

change your code like this

 private void imageClicked(Object sender, System.EventArgs e) { if(listView1.SelectedItems.Count < 1) return; ListViewItem selectedItem = listView1.SelectedItems[0]; selectedFBId = selectedItem.Tag as string; selectedFBName = selectedItem.Text; DialogResult dialogA = MessageBox.Show("Analyse employee data?", "SOC", MessageBoxButtons.YesNo); if (dialogA == DialogResult.Yes) { TargetEmployee.Text = "Selected Target: " + selectedFBName; pf.Show(); ThreadPool.QueueUserWorkItem(LoadUserDetails); } } 

2 Comments

The error is at "ListViewItem selectedItem = listView1.SelectedItems[0] Error Msg: Invalid Argument = Value of '0' is not valid for 'index
@SamuelLee you should apply the check as i have mentioned above in my code if(listView1.SelectedItems.Count < 1) return;
1

You can use a try { } catch { } statement for your error handling.

Once you locate the line of code that generates an exception, you can wrap it into a block like this

try { int a = int.Parse("pedantic"); // This throws an error because you cannot convert } catch (Exception e) { // Handle your error here instead of crashing your program } 

4 Comments

That doesn't even compile ("Division by constant zero")
@Sayse he has just given an example of how to handle exception. And he has commented that as well.
@EhsanUllah - I'm just being picky, normally examples should compile so that the OP can try them if need be, if i was being more picky I'd say something about catching Exception but I can see what SLC is trying to do. I've stated in my other comment why I don't think this is correct also
Yeah but the compiler would probably optimize it as its not used ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.