Solved

With ListView, how to make sure one element is always selected?

Posted on 2008-06-17
6
623 Views
Last Modified: 2010-04-21
I have created a listview control, which works OK, except that I want to make sure that one element is always selected - i.e that it never happens that no element is selected.

My problem is that if the user clicks in the listview outside of the populated rows, the previously selected element is deselected. When this happens, I would prefer the last element to be selected.  When loading my form I want the first element to be selected. I have the same problem with both the "Click" event and the "SelectedIndexChanged" event. Neither are activated when clicking in the listview outside of the populated rows, yet an existing selection is cleared.

Key properties I have assigned to my listview: MultiSelect = false, HideSelection = False, and FullRowSelect = True
Private Sub ShowLinkList()

 

        ' If no companies are defined, display a dummy element, otherwise display the real links

 

        ' ----- So, read the table of links which have been defined ----

        ' Create a connection string 

        Dim connString As String = "Provider=VFPOLEDB.1;" + _

               "Data Source=" & MyDataPath & ";"

        Dim myConnection As OleDbConnection = New OleDbConnection

        Dim strSqlQuery As String

        myConnection.ConnectionString = connString

        ' Create a data adapter

        strSqlQuery = "SELECT * FROM TaInit WHERE s200name <> '' ORDER BY TempaidID"

        Dim da As OleDbDataAdapter = New OleDbDataAdapter(strSqlQuery, myConnection)

        ' Create a new dataset 

        Dim ds As DataSet = New DataSet

        ' Fill dataset 

        da.Fill(ds, "TaInit")

 

        '--- Display the three columns ---

        ' Can also change the width of the columns by changing the '100' to a different value. 

        ' The alignment can also be changed, to be right or centre aligned.

        lvwCompanyLinks.Columns.Add("Co ID", 80, HorizontalAlignment.Left)

        lvwCompanyLinks.Columns.Add("Company Name", 150, HorizontalAlignment.Left)

        lvwCompanyLinks.Columns.Add("Target Company", 220, HorizontalAlignment.Left)

        lvwCompanyLinks.MultiSelect = False

 

        '--- Populate the rows -----

        ' Define variable to hold the ListItem information 

        ' so that we can add values to the column we want to change 

        Dim lSingleItem As ListViewItem

        ' Check if any links have been defined

        With ds.Tables(0)

            If .Rows.Count = 0 Then

                ' Indicate that we have NO links

                bLinksDefined = False

 

                ' Create dummy link as no links are defined

                lSingleItem = lvwCompanyLinks.Items.Add("TBA")     ' Create a new line, and assign the ListItem into the variable so we can add sub items

                lSingleItem.SubItems.Add("TBA")     ' The first sub item for the first line

                lSingleItem.SubItems.Add("TBA")     ' The second sub item for the first line

 

            Else

                ' Indicate that we DO have links

                bLinksDefined = True

 

                Dim i As Integer                    ' Row counter

                For i = 0 To .Rows.Count - 1

                    ' Display the list of real links

                    lSingleItem = lvwCompanyLinks.Items.Add(.Rows(i).Item("TempaidID"))  'Create a new line, and assign the ListItem into the variable so we can add sub items

                    'The first sub item for the line

                    lSingleItem.SubItems.Add(.Rows(i).Item("TaName"))

                    'The second sub item for the line

                    lSingleItem.SubItems.Add(.Rows(i).Item("S200Name"))

                Next

            End If

 

        End With

 

        ' No item is selected. Check if there is a row to select!

        lvwCompanyLinks.Items(0).Selected = True

        lvwCompanyLinks.Items(0).Focused = True

        lvwCompanyLinks.Focus()

 

    End Sub

 

    Private Sub lvwCompanyLinks_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwCompanyLinks.SelectedIndexChanged

 

        ' NOTE To show that a row is selected, even when the control does not have focus, 

        '   set HideSelection = False, and FullRowSelect = True.

 

        ' Check if a row is selected

        If lvwCompanyLinks.SelectedIndices.Count = 0 Then Exit Sub

 

        Dim lvi As ListViewItem = lvwCompanyLinks.SelectedItems(0)

        MessageBox.Show(lvi.Text & " / " & lvi.SubItems(1).Text & " has been selected.")

 

    End Sub

Open in new window

0
Comment
Question by:logicalc
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 21811575
When you select a new item it fires two times the event: unselect the previous item and select the new one.

This way you have to check if you do one or other action. You can do that this way:

Private Sub SelectedIndexChanged(...) Handles Listview1.SelectedIndexChanged

If Me.ListView1.SelectedItems.Count > 0 Then
  Debug.WriteLine(Me.ListView1.SelectedItems(0).Text)
End If

End Sub
0
 

Author Comment

by:logicalc
ID: 21811792
Thanks jpaulino, but the problem is not that I cannot recognise when an item _is_ selected, but that I cannot recognise when _nothing_ is selected.

Because SelectedIndexChanged is also called when the previous item is unselected, I cannot use this to find if nothing has been selected.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21812015
Sorry but I didn't looked very well to the code!

The best solution (I think) is to show a message to the user to select any item but you can do this walkaround using a timer to detect if there's no item selected and force the selection.



    Dim tm As New Timer

    Dim lastItem As Integer
 

    Sub tmSelectItem()

        tm.Stop()

        If Me.ListView1.SelectedItems.Count = 0 Then

            Me.ListView1.Items(lastItem).Selected = True

        End If

    End Sub
 
 

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        If Me.ListView1.SelectedItems.Count > 0 Then

            If Me.ListView1.SelectedItems(0).Index <> lastItem Then

                lastItem = Me.ListView1.SelectedItems(0).Index

                MsgBox(Me.ListView1.SelectedItems(0).Text)

            End If

        Else

            tm.Interval = 50

            AddHandler tm.Tick, AddressOf tmSelectItem

            tm.Start()

        End If

    End Sub

Open in new window

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.

 

Author Comment

by:logicalc
ID: 21876172
Thanks jpaulino, this certainly does ensure an item is selected.

But what I really wanted is to force the user to actually make a choice, so that when the user presses a button or selects a menu I know that he has deliberately selected an option. Otherwise I need to check whether an item has been selected and abort if not - or disable all the controls - and ether method makes a lot of unnecsssary work.

Would a different control be better?
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 21879590
>> Otherwise I need to check whether an item has been selected and abort if not - or disable all the controls - and ether method makes a lot of unnecsssary work.

But this is the normal procedure. Check it any item is selected and show a waning message to the user.

You can use other controls, like a datagridview, that keeps always a selected row, but you will also spend some time to implementing.
0
 

Author Closing Comment

by:logicalc
ID: 31468100
Thanks for your 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

Suggested Solutions

Title # Comments Views Activity
Change data in datatable 8 30
WPF - Tooltips for ComboBox items 5 35
Get Count in a variable with 5 digits always 1 28
Passing a form as a parameter to an external class 1 27
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

22 Experts available now in Live!

Get 1:1 Help Now