[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help me understand all the modes of a listbox

Posted on 2009-02-24
23
Medium Priority
?
244 Views
Last Modified: 2012-05-06
I have a listbox that appears to go into an unexpected mode at times.  It happens when I leave a textbox by clicking on an item in the listbox; instead of selecting the item just clicked on, the listbox goes into a mode where the mouse can hover over the different items and they each, in turn, highlight...but never get selected.  I have to click a second time on the item to actually select it.  So I guess my question is what is this mode called (when hovering the mouse over the different items cause the items to highlight but not SELECT)?  And how can I detect when the listbox goes into this mode?

By the way, I have the listbox's selectionMode set to "one", so only one item can be selected at a time.
0
Comment
Question by:David L. Hansen
  • 14
  • 9
23 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23725110
I don't think any of the SelectionModes cause the ListBox to behave in that manner.  =\

Do you have any code in the MouseMove() event for the Listbox?

Do you have any third party controls on the form?
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23725160
No code in MouseMove, and no third party controls.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23725495
The code below is what fires when I leave the textbox.  It works without errors in the senario described...as soon as I answer the msgbox question then (in this particular senario) the listbox gets the focus but instead of truly selecting the item I clicked on, it just highlights it and takes on the behavior described above (hovering, etc.).  Note: I have a custom made event associated with this sub.  It is raised as I leave the txtbox.
    Private Sub txtNote_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNote.Leave, Me.leaveNoteTxtBox
        If oldCurrNote <> "" Then 'oldCurrNote will NOT equal "" only if the user just cancelled a save to the noteText.
            currNote = oldCurrNote
            oldCurrNote = ""
        End If
 
        If currNote <> Me.txtNote.Text Then
            Dim reply As MsgBoxResult
            reply = MsgBox("Do you want to save the changes made to this note?", MsgBoxStyle.YesNoCancel)
            Select Case reply
                Case MsgBoxResult.Yes
                    Me.NotesBin(Me.lstNotes.SelectedIndex).NoteText = Me.txtNote.Text
                    If noteManager.updateNote(NotesBin(Me.lstNotes.SelectedIndex)) = AttempResult.Failed Then
                        MsgBox("Could not save changes.  Error description: " & Err.Description)
                    End If
                Case MsgBoxResult.No
                    Me.txtNote.Text = currNote 'Change the text back to the way it was before.
                Case Else
                    oldCurrNote = currNote
                    Me.txtNote.Focus() 'leave changes in txtbox but don't save them yet.
            End Select
        End If
 
        'make sure at this point that whichever note is selected has its text displayed in the txtbox
        Dim s1 As System.Object = Nothing
        Dim e1 As System.EventArgs = Nothing
        RaiseEvent lstMouseClick(s1, e1)
    End Sub

Open in new window

0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 15

Author Comment

by:David L. Hansen
ID: 23725558
If I step through the code line by line (in the described senario) the funny mode never happens.  Only when I run the code without any breaks does it do this.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23726034
You are using the legacy MsgBox() instead of the newer MessageBox.Show().  I have seen other peculiar oddities involving focus when the legacy MsgBox was used.  Try switchin to the MessageBox and see if the problem goes away...
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23726177
Sorry, that didn't do it.  I think you are right about the messagebox being the culprit though.  I'm sure that's the problem because when I comment-out the messagebox line altogether and force a "Yes" return value, the problem goes away.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23726322
Perhaps you could try a different event such as LostFocus() or one of the validating type events?  Just thinkin out loud...
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23727183
Well, trying the different events as you suggested didn't seem to get us to where we want to go.  However, as I've played with some of the MessageBox.Show parameters (ie. the 'options' parameter is now set) there is an improvement.  It now highlights the clicked on item, just as before, but now the hovering of the mouse over the other items does nothing...which is good...I want the highlight to stay on the item I've clicked on.  The item is still not actually SELECTED but hopefully this is a good clue.  |=>
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23728152
Have you tried passing the form ("Me") as the "owner" parameter?
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23728236
Tried it with much anticipation...no joy.

I did build a dialog form and made it act just like the message box and the same symptom appears.  grrrrr.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23728258
I noticed also that if I click on the form (in a blank area) right after clicking on an item, so that the form has focus, it forces the lstbox selection to finish.  But when I force the focus programmatically, it doesn't do the same thing.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23728262
That's so weird...I wish I could figure it out for ya.  =\

Let's figure out if it's something to do with your system as a whole...or just your current project.

Try making a fresh project with just the TextBox and the ListBox, and the bare bones code necessary to reproduce the functionality.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23728296
ok
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23728577
I've built it in a very bare bones solution (see code below).  If you run it and make a change to the text belonging to either item in the listbox you'll see the issue.

To be more clear:
1st - click on one of the two items in the listbox
2nd - you'll see "Merry" or "Christmas" appear in the textbox.
3rd - make a change to the "Merry" or "Christmas" string and click away (or tab away).
4th - You will be prompted to save those changes.
5th - choose "yes" and when you bring it back up you'll see that your change has been saved.

Now, to make the problem appear, just make a text change to "Merry" or "Christmas" and then immediately click the other listbox item.  You should get the prompt and notice that even though the other item in the listbox is now highlighted, your text in the textbox has not switched to the note associated with it.  Hope that is clear.

I just have a form with a listbox and a textbox...the button isn't even required.
Public Class Form1
    Dim origText As String
    Dim item0_note As String = "Merry"
    Dim item1_note As String = "Christmas"
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.Items.Add("item0")
        Me.ListBox1.Items.Add("item1")
    End Sub
 
    Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
        origText = Me.TextBox1.Text
    End Sub
 
    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        If Me.TextBox1.Text <> origText Then
            Dim reply As MsgBoxResult
            reply = MessageBox.Show("Want to save your changes to the text?", "Save changes?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
            Select Case reply
                Case MsgBoxResult.Yes
                    If Me.ListBox1.SelectedIndex = 0 Then
                        item0_note = Me.TextBox1.Text
                    Else
                        item1_note = Me.TextBox1.Text
                    End If
            End Select
        End If
    End Sub
 
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If Me.ListBox1.SelectedIndex = 0 Then
            Me.TextBox1.Text = item0_note
        Else
            Me.TextBox1.Text = item1_note
        End If
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub
End Class

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23728602
I'll check it out!  =)
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23728608
thanks for your help :)
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23745390
What do you think??
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23745461
I'll be taking a look today....
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23745491
super! ...thanks.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23745790
I've been playing with events to try and find a work around but to no avail.

By the way, I'll be out for an hour or so.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 23746642
The special "mode" the ListBox is in can be reproduced by selecting an Item and then moving the mouse up/down while HOLDING DOWN the button.  The selected item will change with the mouse but the SelectedIndexChanged() Event does not fire.

So somehow when the MessageBox is closed the ListBox thinks the Mouse is being held down.  Sounds like a MS bug to me...

I'm playing with it to see if I can come up with a "fix".
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 23746865
I've confirmed my assumption by adding this code:

    Private Const MOUSEEVENTF_LEFTUP = &H4
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

    Private Sub ListBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.MouseEnter
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    End Sub

So whenever the ListBox is entered I manually force the mouse button to be in the UP position...and the problem has gone away...at least it did on my system.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 23748682
Finally it works!!!  I'm so grateful :D
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Make the most of your online learning experience.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Screencast - Getting to Know the Pipeline

830 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