Volker
Volker

Reputation: 465

Toggle the appearance of a VBA userform button between bold and non-bold

I'd like to toggle the appearance of a VBA userform button between bold and non-bold.

There is no problem to make it bold like this:

 cmd_WriteTable.Font.Bold = True

When doing the opposite nothing happens. The captions is still bold.

cmd_WriteTable.Font.Bold = False

I am using SolidWorks VBA, however I expect it to be identical to Excel VBA.

Upvotes: 0

Views: 1717

Answers (2)

Jeremy Fosker
Jeremy Fosker

Reputation: 1

I am using Excel Pro Plus 2019 and I have encountered a very similar problem. If you want to skip the details my successful solution is at the end of this post.

In a UserForm I have 16 label controls, each associated with an identifying textbox control. When the user selects a textbox I want to highlight the selection by switching the font of the associated label to bold.

I attempted this by iterating all the controls in the form in the normal way (for each control in me.controls). To locate and associate the correct controls I use tags (e.g. "lbl") and control names of the form 'nameNN'; where NN is numeric.

I use the textbox.enter event to trigger a scan of the controls.

This works fine where, on the scan of the controls, I change the control.forecolour to vbRed for the matching label and to vbBlack for all the non matching labels. However, I want to make the highlight colour independent.

When I do this with the control.font.bold property though the text is set to bold both when set to TRUE and, ALSO when set to FALSE. I have probed this thoroughly and carefully and am satisfied that it is faulty behaviour (not callin it a bug).

The consequence of this is the locking of all labels to bold on the first scan. An identical problem occurs using the control.font.italic property instead.

I have found no explanation despite many hours of trying. However, I have found a solution that works.

Instead of using control.font.bold I use control.font.weight and set the value to 800 (extra bold) or 400 for normal. Strangely the extra bold works better visually so, after much pain, a better solution.

Upvotes: 0

Volker
Volker

Reputation: 465

I finally made it by putting the command into a separate function. Otherwise it did not work in my application. I a test form there was no problem.

Function unBoldButton()
cmd_WriteTable.Font.Bold = False
End Function

Upvotes: 1

Related Questions