Change Corel VBA userform textbox color from user input

109 views Asked by At

I have a pretty extensive corel VBA userform and I want one of the textbox (textbox52) to change color based on the values entered in textbox38, 39 and 40 (RGB). Basically, it's an RGB color selector with visual representation. I tried a few things...

Private Sub TextBox38_Change()
    SetTextBox52BackColor
End Sub

Private Sub TextBox39_Change()
    SetTextBox52BackColor
End Sub

Private Sub TextBox40_Change()
    SetTextBox52BackColor
End Sub

Private Sub SetTextBox52BackColor()


Dim rCrease as integer
Dim gCrease as integer
Dim bCrease as integer
Dim hexCode as string
Dim rHex As String
Dim gHex As String
Dim bHex As String

rCrease = TextBox38.Value
gCrease = TextBox39.Value
bCrease = TextBox40.Value
rHex = Right("00" & Hex(rCrease), 2)
gHex = Right("00" & Hex(gCrease), 2)
bHex = Right("00" & Hex(bCrease), 2)
hexCode = "&H00" & rHex & gHex & bHex & "&"

TextBox52.BackColor = hexCode

End Sub

Or even straight up RGB

Private Sub TextBox38_Change()
    SetTextBox52BackColor
End Sub

Private Sub TextBox39_Change()
    SetTextBox52BackColor
End Sub

Private Sub TextBox40_Change()
    SetTextBox52BackColor
End Sub

Private Sub SetTextBox52BackColor()


Dim rCrease as integer
Dim gCrease as integer
Dim bCrease as integer

rCrease = TextBox38.Value
gCrease = TextBox39.Value
bCrease = TextBox40.Value

TextBox52.BackColor = RGB(rCrease, gCrease, bCrease)

End Sub

I've even tried to just put in the userform_initialize sub

Textbox52.BackColor = &H00ff00ff&

Or

TextBox52.BackColor = RGB(255, 0, 0)

And nothing works it stays the color that is set in the script editor.

Thanks

P.s.: I've found this but can't seem to be able to adapt that to my need https://community.coreldraw.com/sdk/f/coreldraw-and-corel-designer-api/63228/is-there-a-way-to-change-the-background-color-of-all-controls-on-a-form-via-code

2

There are 2 answers

2
Eric B On BEST ANSWER

It does work for me, but the weird thing is, when you get out of the line with the color assign, it is shown in a different way, it looks like this

Private Sub TextBox1_Change()
    TextBox1.BackColor = &HFF00FF
End Sub

even if you type lower case, or if you put the last "&" so I am just wondering how are you doing that.

I tested what you see here and it works like a charm, background changes to a pink color after changing its content.

1
taller On

If BackStyle property of TextBox52 is set to 0 - fmBackStyleTransparent, then BackColor setting does't make any change. Please change BackStyle to 1 - fmBackStyleOpaque.

Note: I don't have CorelDRAW. It's tested with Excel 365 VBA.

enter image description here