How to draw a line on random places with random colors in Windows Forms (WinForms) C#

1 Answer

0 votes
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();

            Color c = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));

            Pen pen;
            pen = new System.Drawing.Pen(c);
            Graphics graphics = this.CreateGraphics();
            graphics.DrawLine(pen, rnd.Next(0, 100), rnd.Next(0, 100), rnd.Next(0, 500), rnd.Next(0, 300));
            graphics.DrawLine(pen, 50, 100, 400, 100);
            
            pen.Dispose();
            graphics.Dispose();
        }
    }
}



answered Oct 13, 2014 by avibootz
...