Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to draw and fill a rectangle with random x, y and color in WPF C#

1 Answer

0 votes
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Drawing;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Rectangle rect;
            Random rnd = new Random();
            Color c;
                
            rect = new System.Windows.Shapes.Rectangle();
            
            c = Color.FromArgb((byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), 
                               (byte)rnd.Next(0, 255));
            rect.Stroke = new SolidColorBrush(c);
            c = Color.FromArgb((byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), 
                               (byte)rnd.Next(0, 255));
            rect.Fill = new SolidColorBrush(c);
            rect.Width = rnd.Next(200, 500);
            rect.Height = rnd.Next(50, 300);
            rect.StrokeThickness = rnd.Next(1, 5); ;
            Canvas.SetLeft(rect, rnd.Next(0, 100));
            Canvas.SetTop(rect, rnd.Next(0, 100));
            canvas.Children.Add(rect);
        }
    }
}


<Canvas Width="500" Height="250" Name="canvas" Grid.Row="1" Grid.Column="0"></Canvas>


answered Oct 9, 2014 by avibootz
edited Oct 9, 2014 by avibootz

Related questions

2 answers 239 views
2 answers 236 views
1 answer 232 views
3 answers 218 views
...