w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How do I generate random colour dots on a panel?

As you need to show the dots on a panel, you need to add the panel to the control - if it does not already exist.

private void randomPaint(int numberOfTimes)
{
    Random r = new Random();
    Color rC;
    SolidBrush b1;

    Graphics g = pnlDraw.CreateGraphics();

    for (int i = 0; i < numberOfTimes; i++)
    {
        rC = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
        b1 = new SolidBrush(rC);
        g.FillEllipse(b1, r.Next(pnlDraw.Size.Width),
r.Next(pnlDraw.Size.Height), 30, 30);
    }
}

also, change the function for event handling to avoid variable name conflict

private void cmbDots_SelectedIndexChanged(object sender,
EventArgs e)
{
    try
    {
        randomPaint(Convert.ToInt32(cmbDots.SelectedItem));
    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message);
    }
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.