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
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

759 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