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
249 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 19

Expert Comment

by:Desp
Comment Utility
disregard the last comment!
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Expert Comment

by:Desp
Comment Utility
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
Comment Utility
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
Comment Utility
put this check:

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

Author Comment

by:jxharding
Comment Utility
thanks desp!
0
 
LVL 19

Expert Comment

by:Desp
Comment Utility
glad i could help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now