List box lost focus - delete button

mane_uk

Registered User.
Local time
Today, 00:37
Joined
Feb 14, 2011
Messages
45
Hi all,

Here I am again with more questions... hope that, as previously, you guys can shed some light!!

In one of my forms which contain a lot of textbox fields, comboboxes, etc... I have a list box where I populate some existing contact names added previously by the user.

As I want the list box to be editable I created a "Delete" command button which I made invisible and every time the user click on the list box the "delete button" appears.

If the user click on something else (after had clicked first on the contact list box) I want the delete button to disapear again. I managed to do that by using the listbox lostfocus event.

But my problem is, If I am trying to click on the "delete" button after choosing the contact in my list box, the lostfocus of the listbox make the button "disappear first" and don't run the commands of the button.

Any ideas how to get it sorted?

By the way I have also tried using on exit, before update and after update on my list box but none of them worked!!

Cheers
Mane
 
Why don't you hide the button only when nothing is selected in the listbox or use the Mouse Move event of the button to make it visible.
 
Hi vbaInet,

Thanks for the reply...

My Delete command button is hidden/invisible when nothing is selected in the list box (when form is loading I hide the delete command button) and when I click on the list box it appears ... the thing is how to run the delete command button and make it disappear. I know I could do at the end of the delete command button but it is not sure that the user will want to delete a contact at all so how would I capture that... I mean, imagine the 2 situation:

Form is loaded therefore the delete command is hidden automatically then...

1) user click on the listbox contact and then click on delete command
- delete command is hidden until the point that user clicks on listbox when it appears on the screen
- I could then, in the delete command, delete the contact, set the focus somewhere else and then hide the delete command again

but what if, after the form is loaded,...

2) user click on the list box then in a textfield (and not in the delete command)
- the delete command button appear in the screen when user click on the listbox
- but then I have a lot of text fields and combobox that I would have to set up checks to see if the listbox has nothing selected (or lost focus!!) to hide de delete command button. Also in the same principle of the contact listbox I will have another 5 listboxes working in the same way... so it would be a lot of trouble and probably some performance impact(?)...

It might have an easier way but I can't see how!! Any other suggestions please?

Thanks
Mane
 
My point is the button should remain Visible if a row is selected in the listbox. If nothing is selected and you tab out, then it can be hidden. Check against the Listbox's ItemsSelected property to see if an item is selected:
Code:
If Me.Listbox.ItemsSelected.Count = 0 Then
      Me.Button.Visible = False
End If
 
Again, thanks for your reply...

You mentioned: "My point is the button should remain Visible if a row is selected in the listbox"... yes this is exactly what I want that the button just appear when the list box has a selection but also that the button disappear if the listbox loose focus (so if the user decide to click anywhere else it would hide again the button even though he might have selected a contact)...

So a few questions/situations based on your answer:

1) Where I am going to add this code to check the select?
1.1 if "on got focus" of the listbox how do I know that it lost focus and should hide again the delete command button?
1.2 If it in all textfields I have in the form (probably on "got focus" of every textfield/combobox/listbox)... that would be too many fields (probably impact the performance).
1.3 If in the delete button itself... what if the user decide not to click on the delete button?
1.4 if in the "lost focus" or "on exit" events for the list box... but then what if the user has selected a contact (so the itemselect.count would be more than 0) but decided not to delete it anymore and clicked in a textbox, the select would still be there when the list box loose focus therefore the count would still be more than 0 so it would not hide the delete command.

Any other ideas?
Cheers
 
yes this is exactly what I want that the button just appear when the list box has a selection but also that the button disappear if the listbox loose focus (so if the user decide to click anywhere else it would hide again the button even though he might have selected a contact)
This is not an intuitive design. You need to stop and think about this for a minute. In a multi-select listbox, if I made 20 selections and tab out, as a user I won't want the button to disappear because that means I have to click in the listbox AGAIN just to see the delete button. I could mistakenly click inside the listbox (without holding the Ctrl key) and lose my 20 selections, all because I wanted the button to become visible.
 
I understand where you come from and I appreciate your help but this listbox will have a maximum of 4 or 5 entrances and it is only editable so users can delete any wrong entrance in a period of 1 day, after it the fields will be disabled for changes, also the form itself looks quite cranky and the selection cases and the delete button appearing all time while there is a selection would only add to it therefore not only the button will disappear but all the selection as well when the user click somewhere else!! This might not be as user friendly but I can guarantee you that visually it is the only way forward.
So, based that I don't want to keep it showing when the listbox loose focus would know a different approach to do it?
Cheers
 
Based on your original request, this should give you some ideas:
Code:
Option Compare Database
Option Explicit


Private Sub cmdButton_Click()
    Me.TimerInterval = 0
    MsgBox "Button clicked"
    Me.SomeControl.SetFocus
    Me.cmdButton.Visible = False
End Sub


Private Sub cmdButton_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Me.lstBox.ItemsSelected.Count <> 0 Then
        Me.cmdButton.Visible = True
        Me.TimerInterval = 100
    End If
End Sub


Private Sub Form_Timer()
    If Screen.ActiveControl.Name <> "cmdButton" Then
        Me.cmdButton.Visible = False
    End If
    
    Me.TimerInterval = 0
End Sub


Private Sub lstBox_Click()
    If Me.lstBox.ItemsSelected.Count <> 0 Then
        Me.cmdButton.Visible = True
    Else
        Me.cmdButton.Visible = False
    End If
End Sub


Private Sub lstBox_LostFocus()
    If Me.TimerInterval = 0 Then
        Me.cmdButton.Visible = False
    End If
End Sub
cmdButton - the button to hide
lstBox - the listbox with the list of entrances
SomeControl - the control to receive focus after the button is clicked.

Another way would be to use the MouseMove event of the Detail section of the form to hide the button and keep the button visible on the button's MouseMove event.
 
Just gave this a little bit more thought and this should do it:
Code:
Option Compare Database
Option Explicit

Private buttHover As Boolean, lstCalling As Boolean

Private Sub cmdButton_Click()
    Me.TimerInterval = 0
    MsgBox "Button clicked"
    Me.SomeControl.SetFocus
    Me.cmdButton.Visible = False
    buttHover = False
End Sub


Private Sub cmdButton_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Me.lstBox.ItemsSelected.Count <> 0 Then
        buttHover = True
        Me.TimerInterval = 100
    Else
        buttHover = False
    End If
End Sub


Private Sub Form_Timer()
    If lstCalling Then
        lstCalling = False
    Else
        buttHover = False
    End If
    
    Me.TimerInterval = 0
End Sub


Private Sub lstBox_Click()
    If Me.lstBox.ItemsSelected.Count <> 0 Then
        Me.cmdButton.Visible = True
    Else
        Me.cmdButton.Visible = False
    End If
End Sub


Private Sub lstBox_LostFocus()
    If Not buttHover Then
        Me.cmdButton.Visible = False
        lstCalling = True
        Me.TimerInterval = 100
        buttHover = False
    End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom