Gerrit ten Napel
Gerrit ten Napel

Reputation: 149

Changing a canvas color using textboxes

I'm trying to create a Canvas where the colors can be changed using three textboxes: R, G and B. The canvas color needs to change whenever the value in a TextBox changes. The TextBox is limited to integers between 0 and 255, and as long as a valid number has been entered, I need to change the Canvas color.

I know that the Canvas background is set via a SolidColorBrush, but I do not know how to create a SolidColorBrush from an arbitrary collection of RGB values.

Upvotes: 2

Views: 296

Answers (2)

user6805161
user6805161

Reputation:

You can Use var brush = new SolidColorBrush(Color.FromRgb(r,g,b)); and set the color of your canvas to this property.

Upvotes: 0

pstrjds
pstrjds

Reputation: 17448

You have three text boxes, I will assume you can pull the text from those and can parse it into the appropriate byte values. Assuming you have three bytes: r,g,b - then you can create a SolidColorBrush with this line of code, which uses the Color.FromRgb static method to create the color from the three values from your textboxes:

var brush = new SolidColorBrush(Color.FromRgb(r,g,b));

Upvotes: 2

Related Questions