Answer the question
In order to leave comments, you need to log in
How to make a random color by clicking on the Button and have the Rectangle change color?
by clicking on the Button, you need the Rectangle to change color randomly in C # (wpf)
Answer the question
In order to leave comments, you need to log in
Let's take this window as an example:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="Change color"
Click="Button_Click"
Margin="15"/>
<Rectangle Name="MyRectangle"
Grid.Row="1"
Margin="15"
Fill="Red"/>
</Grid>
private List<SolidColorBrush> brushes;
private Random rnd;
public MainWindow()
{
InitializeComponent();
rnd = new Random();
brushes = new List<SolidColorBrush> //заполняем цвета, которые будет принимать Rectangle
{
Brushes.AliceBlue,
Brushes.AntiqueWhite,
Brushes.Aqua,
Brushes.Aquamarine
};
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyRectangle.Fill = brushes[rnd.Next(0, brushes.Count)];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question