When we set the value of the Color in three TextBoxes
(Red, Green, Blue) and when we click on the Button the color of the Rectangle
will be change. Here we use the Color.FromArgb property of the SolidColorBrush
Pattern
For This, First we define the three TextBlocks and TextBoxes for Red, Green and
Blue ( txtRedColor, txtRedColor)
<StackPanel
Orientation="Horizontal"
Margin="9,0,13,0">
<TextBlock
VerticalAlignment="Center"
Text="Red
Color"
Margin="5"></TextBlock>
<TextBox
Name="txtRedColor"
Height="25"
Width="50"></TextBox>
<TextBlock
VerticalAlignment="Center"
Text="Green
Color"
Margin="5"></TextBlock>
<TextBox
Name="txtGreenColor"
Height="25"
Width="50"></TextBox>
<TextBlock
VerticalAlignment="Center"
Text="Blue
Color"
Margin="5"></TextBlock>
<TextBox
Name="txtBlueColor"
Height="25"
Width="50"></TextBox>
</StackPanel>
Now we take a Button Control and a Rectangle:
<Button
Name="btnchangeRect"
Content="Change
Color"
Grid.Row="1"
Width="100"
Height="25"></Button>
<Rectangle
Name="rect"
Grid.Row="1"
Stroke="Azure"
StrokeThickness="5"
Margin="14,0,26,0"
Height="18"
VerticalAlignment="Bottom"></Rectangle>
Here is the Output:
Now we create an EventHandler for the Button control, Here we write the
Following Code after the InitializeComponent() in the .cs page
this.btnchangeRect.Click
+= new
RoutedEventHandler(btnchangeRect_Click);
After that we write the code for the Event
handler:
void
btnchangeRect_Click(object sender,
RoutedEventArgs e)
{
this.rect.Fill =
new SolidColorBrush(
Color.FromArgb(
255,
byte.Parse(this.txtRedColor.Text),
byte.Parse(this.txtGreenColor.Text),
byte.Parse(this.txtBlueColor.Text)
));
Here Color.FromArgb is used to Set the Color property of SolidColorBrush. Each
Color value is in between 0-255. Here R denotes to red, B denotes to Blue, and G
denotes to Green.And A is for alpha, which manitains the transparency of the
color, So if here we use the value 0 for the complete transparent color.
Here we set the A to 255and set the other three properties( R,G,B) according to
the TextBoxes value.