I'm creating an important effect for a particular ComboBox presentation.
The text of this ComboBox is originally centralized , and when you open your list, I make the text left aligned for items in your list to appear < strong> left-aligned , but the idea is for the text in the ComboBox's box to remain centralized . As can not do this with just the ComboBox itself , I found a way to do it:
A "Label" with the same text as the ComboBox and also centralized has the approximate dimension of this ComboBox and becomes visible on it when the ComboBox is selected, and this is done through the "Enter" event.
So the user sees the text centered on the ComboBox since the Label hides the original text that is aligned to the left by the "Enter" event, and strong> automatically via the left-aligned DropDown . So good!
I've set up the "Click" event of the Label in case the user "clicks" on it instead of selecting an item, then the Label "would be removed" ( getting invisible ) and the list would be closed , but in this case the list is NOT closed, but the Label is removed , needing to click on the ComboBox so that the list closes (at this point the text of the ComboBox is again centered).
If there was a control over the "close list" , when the Label is removed on the first "click" the list would be closed but in the searches I did not find anything that could use or solve the problem .
Is there any way to control the closing of the ComboBox list in VBA, or another way to create this effect?
The code for verification is this:
Private Sub UserForm_activate()
ComboBox1.TextAlign = fmTextAlignCenter
ComboBox1.AddItem "Primeiro"
ComboBox1.AddItem "Segundo"
ComboBox1.AddItem "Terceiro"
ComboBox1.ListIndex = 0
Label1.Left = ComboBox1.Left + 2
Label1.Width = ComboBox1.Width - 4
Label1.Top = ComboBox1.Top + 2
Label1.Height = ComboBox1.Height - 2
Label1.TextAlign = fmTextAlignCenter
Label1.BorderStyle = fmBorderStyleNone
Label1.ZOrder 0
Label1.Visible = False
TextBox1.SetFocus
End Sub
Private Sub ComboBox1_enter()
ComboBox1.TextAlign = fmTextAlignLeft
Label1 = ComboBox1.Text
Label1.Visible = True
ComboBox1.DropDown
End Sub
Private Sub Label1_Click()
ComboBox1.TextAlign = fmTextAlignCenter
Label1.Visible = False
End Sub