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
253 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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