C
C#2y ago
asianaf

❔ How can I make it where I'm able to change the colour of the text

When the user starts the program they need to pick a colour for the text called "Visual C#"
80 Replies
Anton
Anton2y ago
no idea what this means
asianaf
asianafOP2y ago
sorry for my bad english but I'm trying to make it were when the user clicks "start" they'll have an option to change a color of the text like this
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
You're on the right track, however BackColor is the background color. If you want to change the color of the drawn text, you need to apply it to the brush and redraw
asianaf
asianafOP2y ago
so should it be "Brushes.Forecolor"?
TheBoxyBear
TheBoxyBear2y ago
Where is the brushes class from? The one in System.Windows.Media has no such property
asianaf
asianafOP2y ago
here's all the code rn
asianaf
asianafOP2y ago
I was thinking of grabbing "this.ForeColor = pickcolor.Color;" and then putting in on "Brushes.b"
TheBoxyBear
TheBoxyBear2y ago
Just trying to find the Brushes class you're using to check the docs Normally you create a Brush instance and use that
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
Where is that class from when you hover the name?
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
That's a static class though, doesn't make sense to have as a member
asianaf
asianafOP2y ago
I'm confused
TheBoxyBear
TheBoxyBear2y ago
same Anyway, the proper way of doing it is declaring b as Brush, not Brushes
asianaf
asianafOP2y ago
oh
asianaf
asianafOP2y ago
wait for this code a color picker pops-up and you can pick any color you want
asianaf
asianafOP2y ago
I wan't to use this code but for
asianaf
asianafOP2y ago
for this
TheBoxyBear
TheBoxyBear2y ago
Set the Brush's color to the picker color g.DrawString(..., b, ...);
asianaf
asianafOP2y ago
B = this.ForeColor = pickcolor.Color;
asianaf
asianafOP2y ago
something like this?
TheBoxyBear
TheBoxyBear2y ago
yes, though Brush should be initialized as a new SolidBrush
asianaf
asianafOP2y ago
asianaf
asianafOP2y ago
new SolidBrush??
TheBoxyBear
TheBoxyBear2y ago
Reference types are null by default var b = new SolidBrush();
asianaf
asianafOP2y ago
is var just brush??
TheBoxyBear
TheBoxyBear2y ago
Or SolidBrush b = new() since it's a class member var is a shortcut that determines the type based on the value given but doesn't work for declaring members, only variables so jsut SolidBrush b = new(someColor) at the class level to reuse it* Then when the color changes you can set the Color property of the brush
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
You need to specify a color too
asianaf
asianafOP2y ago
but the color could be anything tho
TheBoxyBear
TheBoxyBear2y ago
But you know what it is when it reaches that line
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
The color in new() can also be a variable or property 🙂
asianaf
asianafOP2y ago
man I'm soo confused 💀 I'm soo sorry
TheBoxyBear
TheBoxyBear2y ago
Color is a type so you can have say Color red = Color.Red or Color red = Color.FromArgb(0, 255, 0, 0) or in this case, Color selection = pickColor.Color
asianaf
asianafOP2y ago
wait
asianaf
asianafOP2y ago
this is a System.Drawing.Color??
asianaf
asianafOP2y ago
but I want to convert it to
TheBoxyBear
TheBoxyBear2y ago
yes
asianaf
asianafOP2y ago
brush
TheBoxyBear
TheBoxyBear2y ago
Brush is System.Drawing.Brush
asianaf
asianafOP2y ago
asianaf
asianafOP2y ago
asianaf
asianafOP2y ago
here's the entire code
TheBoxyBear
TheBoxyBear2y ago
SolidBrush has a Color property of type System.Drawing.Color which you also need to create the brush
asianaf
asianafOP2y ago
the "brush" is this > SolidBrush b = new();
TheBoxyBear
TheBoxyBear2y ago
yes But you need to specify the color in new()
asianaf
asianafOP2y ago
I did
TheBoxyBear
TheBoxyBear2y ago
as a Color, not a string
asianaf
asianafOP2y ago
I tried doing new("blue") like Color.Blue? or just Blue
TheBoxyBear
TheBoxyBear2y ago
With pickColor.Color
asianaf
asianafOP2y ago
inside the ()
TheBoxyBear
TheBoxyBear2y ago
yes
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
mb you defined it as pickcolor no uppercase
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
What does the error say?
asianaf
asianafOP2y ago
TheBoxyBear
TheBoxyBear2y ago
$newproject
MODiX
MODiX2y ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
asianaf
asianafOP2y ago
our teach told us to use "Windos Forms App (.NET Framework)
TheBoxyBear
TheBoxyBear2y ago
For now, just put new SolidBrush(colorHere)
asianaf
asianafOP2y ago
asianaf
asianafOP2y ago
nvm nvm
TheBoxyBear
TheBoxyBear2y ago
Yeah many do that. Mostly just a case or 1. Textbooks not being up to date and 2. Computer labs potentially not having 5+ installed
asianaf
asianafOP2y ago
asianaf
asianafOP2y ago
asianaf
asianafOP2y ago
quick question I can delete everything here? v
TheBoxyBear
TheBoxyBear2y ago
That should work
asianaf
asianafOP2y ago
@TheBoxyBear BRO I LOVE YOU
TheBoxyBear
TheBoxyBear2y ago
But don't forget to dispose of the brush afterwards, or do it automatically with using
asianaf
asianafOP2y ago
what do you mean?
TheBoxyBear
TheBoxyBear2y ago
(using SolidBrush b = new...) {// use the brush} or calling b.Dispose() after drawing
asianaf
asianafOP2y ago
I'm still confused 💀
TheBoxyBear
TheBoxyBear2y ago
To release the resources used by the Brush. If there's a Dispose, you most likely need to call it when you're done with the object In more recent versions of c#, you can simply the using syntax to just be using Type name = new...; with no brackets.
asianaf
asianafOP2y ago
oh OH MY GOD I'm stupid when the form loads a color picker should pop up first and then after that the forms loads with the text 💀 I'm not gonna fix it @TheBoxyBear Thanks for the help
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server