Solved

context menu on form : if no selected items on listview , how to handle the exception, i pass a value from the selecteditem to function

Posted on 2004-09-15
9
254 Views
Last Modified: 2010-04-23
hi , i have a context menu on a listview.
the idea is for the user to right click on the listview , and then when the user clicks on the "Print" item on context menu,
i pass the selected items subitem to a function
but the problem is what to do when there is no item selected.
what i initially did was just have a empty exception but that's not professional and it took much longer when there was an error.


  Dim ID As Object
        Try
            ID = MyListview.SelectedItems(0).SubItems(1).Text
            If Not ID Is Nothing Then
               MyFunction(CInt(ID))
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

this does not work, immediately from
try
 -->ID = ...

it goes to the error message telling me that "specified argument out of range"
"parameter name:index"
thanks!
0
Comment
Question by:jxharding
  • 6
  • 3
9 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 12062425
This is the C# code i posted in the morning for a similar problem

          if (e.Button == MouseButtons.Right && listView1.GetItemAt(e.X,e.Y)!=null )
               {
                    ListViewItem lv = listView1.GetItemAt(e.X,e.Y);                    
                    lv.Selected = true;
                    Point spot = this.PointToClient(listView1.PointToScreen(new Point(e.X, e.Y)));
                    contextMenu1.Show(this, spot);
                    MessageBox.Show(lv.Text );
               }
0
 

Author Comment

by:jxharding
ID: 12062454
hi desp, thing is i also use the file-->menu--> Print
when the user clicks on a item on the listview and then selects file --> print.
for that  reason only i think i have to steer clear of the mouse location.
thanks!
0
 
LVL 19

Expert Comment

by:Desp
ID: 12062533
heres VB.Net
        If e.Button = MouseButtons.Right And ListView2.GetItemAt(e.X, e.Y) Is Nothing Then

            Dim lv As ListViewItem = ListView2.GetItemAt(e.X, e.Y)
            lv.Selected = True
            Dim spot As Point = Me.PointToClient(ListView2.PointToScreen(New Point(e.X, e.Y)))
            contextMenu1.Show(Me, spot)
            MessageBox.Show(lv.Text)

        End If
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Desp
ID: 12062540
disregard the last comment!
0
 
LVL 19

Expert Comment

by:Desp
ID: 12062649
put this code in the Mouse up event of the list view..

        If e.Button = MouseButtons.Right And MyListview.GetItemAt(e.X, e.Y) Is Nothing Then
            Dim lv As ListViewItem = MyListview.GetItemAt(e.X, e.Y)
            lv.Selected = True
            ID = MyListview.SelectedItems(0).SubItems(1).Text
            MyFunction(CInt(ID))
        End If
0
 

Author Comment

by:jxharding
ID: 12062712
hi desp,
i tried it ,and it works , its just i still get an error when no items are selected or when the listview is empty.
maybe i should do a check to see if there are any items on the listview , and then if there are
is there anyway to determine if one is actually selected?
thanks!
0
 
LVL 19

Accepted Solution

by:
Desp earned 500 total points
ID: 12062777
put this check:

If MyListview.SelectedItems.Count > 0 Then
....
0
 

Author Comment

by:jxharding
ID: 12062854
thanks desp!
0
 
LVL 19

Expert Comment

by:Desp
ID: 12062891
glad i could help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question