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
250 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
 
LVL 19

Expert Comment

by:Desp
ID: 12062540
disregard the last comment!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

20 Experts available now in Live!

Get 1:1 Help Now