Checkbox Text Color

Why is it that I can change the text (font) color of a textbox and a label but not a checkbox?

lblDesc.TextColor = …
txtName.TextColor = …
chkDefault.???

So “cheat”. set the caption of your checkbox to “”, and place a label of your own beside it

If you’re under OSX you can use declares for it:

This works for a PushButton or all NSButton based controls.

[code]Sub titleColor(extends cb as CheckBox, assigns c as color)
// alternatively :
//declare function initWithAttributedString lib CocoaLib selector “initWithAttributedString:” (id as ptr, a as ptr) as ptr
//declare function attributedTitle lib CocoaLib selector “attributedTitle” (id as ptr) as ptr
//declare function length lib CocoaLib selector “length” (id as ptr) as integer

declare function initWithString lib CocoaLib selector “initWithString:” (id as ptr, s as CFStringRef) as ptr
declare function whiteColor lib CocoaLib selector “whiteColor” (id as ptr) as ptr
declare sub addAttribute lib CocoaLib selector “addAttribute:value:range:” (id as ptr, attr as CFStringRef, v as ptr, r as NSRange)
declare sub setAttributedTitle lib CocoaLib selector “setAttributedTitle:” (id as ptr, p as ptr)

#if Target32Bit then
declare function colorFromRGBA lib CocoaLib selector “colorWithCalibratedRed:green:blue:alpha:” (class_id as Ptr, red as Single, green as Single, blue as Single, alpha as Single) as Ptr
#else
declare function colorFromRGBA lib CocoaLib selector “colorWithCalibratedRed:green:blue:alpha:” (class_id as Ptr, red as double, green as double, blue as double, alpha as double) as Ptr
#endif

dim col as ptr = colorFromRGBA(NSClassFromString(“NSColor”),c.Red/255,c.Green/255,c.Blue/255,(255-c.alpha)/255 )
if col<>nil then
dim colorTitle as ptr = initWithString(allocate(“NSMutableAttributedString”), cb.Caption)
if colorTitle<>nil then
addAttribute( colorTitle, _
stringConstant(“NSForegroundColorAttributeName”,CocoaBundleID), col, NSMakeRange(0,cb.Caption.Len) )
setAttributedTitle(ptr(cb.Handle), colorTitle)
end if
end if
End Sub[/code]

that is exactly what i did for my application.

I did that too before but if you have a couple of boxes/buttons in different colors it’s more convenient for me to use system declares but it it’s not cross platform - yep.

I just thought this thread may be a goods idea to store that code snipped. :slight_smile:

[quote=231759:@Rob Egal]If you’re under OSX you can use declares for it:

This works for a PushButton or all NSButton based controls.

[code]Sub titleColor(extends cb as CheckBox, assigns c as color)
// alternatively :
//declare function initWithAttributedString lib CocoaLib selector “initWithAttributedString:” (id as ptr, a as ptr) as ptr
//declare function attributedTitle lib CocoaLib selector “attributedTitle” (id as ptr) as ptr
//declare function length lib CocoaLib selector “length” (id as ptr) as integer

declare function initWithString lib CocoaLib selector “initWithString:” (id as ptr, s as CFStringRef) as ptr
declare function whiteColor lib CocoaLib selector “whiteColor” (id as ptr) as ptr
declare sub addAttribute lib CocoaLib selector “addAttribute:value:range:” (id as ptr, attr as CFStringRef, v as ptr, r as NSRange)
declare sub setAttributedTitle lib CocoaLib selector “setAttributedTitle:” (id as ptr, p as ptr)

#if Target32Bit then
declare function colorFromRGBA lib CocoaLib selector “colorWithCalibratedRed:green:blue:alpha:” (class_id as Ptr, red as Single, green as Single, blue as Single, alpha as Single) as Ptr
#else
declare function colorFromRGBA lib CocoaLib selector “colorWithCalibratedRed:green:blue:alpha:” (class_id as Ptr, red as double, green as double, blue as double, alpha as double) as Ptr
#endif

dim col as ptr = colorFromRGBA(NSClassFromString(“NSColor”),c.Red/255,c.Green/255,c.Blue/255,(255-c.alpha)/255 )
if col<>nil then
dim colorTitle as ptr = initWithString(allocate(“NSMutableAttributedString”), cb.Caption)
if colorTitle<>nil then
addAttribute( colorTitle, _
stringConstant(“NSForegroundColorAttributeName”,CocoaBundleID), col, NSMakeRange(0,cb.Caption.Len) )
setAttributedTitle(ptr(cb.Handle), colorTitle)
end if
end if
End Sub[/code][/quote]

That can not work because too much is missing

You’re right the “stringConstant” function is missing. Use “NSColor” as constant name which is resolved from “NSForegroundColorAttributeName” and it should work fine.

Sub titleColor(extends cb as CheckBox, assigns c as color)
// alternatively :
//declare function initWithAttributedString lib CocoaLib selector “initWithAttributedString:” (id as ptr, a as ptr) as ptr
//declare function attributedTitle lib CocoaLib selector “attributedTitle” (id as ptr) as ptr
//declare function length lib CocoaLib selector “length” (id as ptr) as integer

declare function initWithString lib CocoaLib selector “initWithString:” (id as ptr, s as CFStringRef) as ptr
declare function whiteColor lib CocoaLib selector “whiteColor” (id as ptr) as ptr
declare sub addAttribute lib CocoaLib selector “addAttribute:value:range:” (id as ptr, attr as CFStringRef, v as ptr, r as NSRange)
declare sub setAttributedTitle lib CocoaLib selector “setAttributedTitle:” (id as ptr, p as ptr)

#if Target32Bit then
declare function colorFromRGBA lib CocoaLib selector “colorWithCalibratedRed:green:blue:alpha:” (class_id as Ptr, red as Single, green as Single, blue as Single, alpha as Single) as Ptr
#else
declare function colorFromRGBA lib CocoaLib selector “colorWithCalibratedRed:green:blue:alpha:” (class_id as Ptr, red as double, green as double, blue as double, alpha as double) as Ptr
#endif

dim col as ptr = colorFromRGBA(NSClassFromString(“NSColor”),c.Red/255,c.Green/255,c.Blue/255,(255-c.alpha)/255 )
if col<>nil then
dim colorTitle as ptr = initWithString(allocate(“NSMutableAttributedString”), cb.Caption)
if colorTitle<>nil then
addAttribute( colorTitle, _
“NSColor”, col, NSMakeRange(0,cb.Caption.Len) )
setAttributedTitle(ptr(cb.Handle), colorTitle)
end if
end if
End Sub

there is no declare for NSClassFromString, allocate, NSMakeRange

do you use macoslib ?

And to extend Dave’s thought, I would make a Container Control with a checkbox (no caption) and a Label. The container properties mirror that of the Label and mirrors the events of the Checkbox. That way it acts like both and is a single control.