Hello, all.
As winter is coming up and there's a lot of bright light during the day and then it gets dark quickly I thought about making a button on a form that lets the user switch between light and dark form control backgrounds.
I started with buttons based on code which I found online that seemed to make sense (I would later add more control types as necessary):
Private Sub CommandButton39_Click() Dim ctrl As Control Dim ct As String ct = "CommandButton" For Each ctrl In niTestIT.Controls If TypeName(ctrl) = ct Then ctrl.BackColor = RGB(64, 64, 64) End If Next ctrl End Sub
But I get a "Type mismatch" error. Any idea of what I am doing wrong?
Try this:
Private Sub CommandButton39_Click() Dim ctrl As Control, CB as commandbuttom Dim ct As String ct = "CommandButton" For Each ctrl In niTestIT.Controls If TypeName(ctrl) = ct Then set cb=ctrl cb.BackColor = RGB(64, 64, 64) End If Next ctrl End Sub
'Control' may have different types, you need to bring this object in the correct controltype before
Hey, thanks for your input!
Unfortunately I am getting the same error as stated previously. This is with CorelDRAW 2018, btw.
If I use the color setting directly, say: "CommandButton1.BackColor = RGB(255, 0, 0)" then it works, but using the loop it does not...
If nothing else I can just change the colors of all the elements by hand, they shouldn't really change after I'm done designing the form. But a universal approach would be nicer of course.
At which line you get your error?
I modified your code without testing
Now I saw a small typo: ... , CB as CommandButton
Maybe this will fix it
Alright, I found the source of the error!
As I was typing up Cont... The autocomplete showed Control twice. So once I changed that to MSForms.Control it works just fine.
So basically that was the main error here, something I should remember for the future.
Just wanted to add another reply with the result, since it seems rather neat and may help save some eyes when working in a darker room:
And here's the final code:
Private Sub cbChangeTheme_Click() Dim ctrl As MSForms.Control If cbChangeTheme.Caption = "Dark" Then niTestIT.BackColor = RGB(48, 64, 64) For Each ctrl In niTestIT.Controls If TypeName(ctrl) = "CommandButton" Or TypeName(ctrl) = "ScrollBar" Or TypeName(ctrl) = "Label" Then ctrl.BackColor = RGB(48, 64, 64) ctrl.ForeColor = RGB(192, 255, 255) End If Next ctrl cbChangeTheme.Caption = "Standard" Else niTestIT.BackColor = &H8000000F For Each ctrl In niTestIT.Controls If TypeName(ctrl) = "CommandButton" Or TypeName(ctrl) = "ScrollBar" Or TypeName(ctrl) = "Label" Then ctrl.BackColor = &H8000000F ctrl.ForeColor = &H80000012 End If Next ctrl cbChangeTheme.Caption = "Dark" End If End Sub
I'm using the default color codes so you can go back to regular colors and not some strange light version after going to the dark one.
And, of course, there's the super annoying that it is not possible to edit the border colors of the buttons, but that's just something I'll have to live with.