How to handle the unusual closure of a ComboBox list in VBA?

4

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
    
asked by anonymous 20.05.2016 / 06:46

1 answer

2

One possible solution would be to use change :

Private Sub UserForm_Activate()

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 = ComboBox1.Text
Label1.Visible = False
TextBox1.SetFocus

End Sub

Private Sub ComboBox1_enter()

Label1 = ComboBox1.Text
Label1.Visible = True

ComboBox1.TextAlign = fmTextAlignCenter
ComboBox1.DropDown
ComboBox1.SetFocus

End Sub

Private Sub ComboBox1_Change()

Label1 = ComboBox1.Text
Label1.Visible = False

ComboBox1.TextAlign = fmTextAlignLeft
TextBox1.SetFocus

End Sub

If you need to use AfterUpdate also for any resolution after the update:

Private Sub ComboBox1_AfterUpdate()

End Sub

Another option would be to use a ListBox and a Label, hence configuring the layout of the Label to stay as you want it to look like a ComboBox, for example:

Private Sub UserForm_Activate()

ListBox1.AddItem "Primeiro"
ListBox1.AddItem "Segundo"
ListBox1.AddItem "Terceiro"
ListBox1.ListIndex = 0

ListBox1.TextAlign = fmTextAlignCenter

Label1.Left = ListBox1.Left + 2
Label1.Width = ListBox1.Width - 4
Label1.Top = ListBox1.Top + 2
Label1.TextAlign = fmTextAlignCenter
Label1.BorderStyle = fmBorderStyleNone
Label1.ZOrder 0
Label1 = ListBox1.Text
ListBox1.Visible = False
Label1.Visible = True

TextBox1.SetFocus

End Sub

Private Sub Label1_Click()

Label1.Visible = False
ListBox1.Visible = True

End Sub

Private Sub ListBox1_Click()

Label1 = ListBox1.Text
ListBox1.Visible = False
Label1.Visible = True

End Sub

See if this works better.

Abs,

    
20.05.2016 / 16:20