Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在面板上生成点_C#_Combobox - Fatal编程技术网

C#在面板上生成点

C#在面板上生成点,c#,combobox,C#,Combobox,我创建了一个组合框和一个面板,因此当用户单击组合框中的一个数字时,点的数量以随机颜色随机出现在面板上。我试图搜索编码,但无法找到我真正需要的 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_MouseUp(object sender, MouseEventArgs e) { Ra

我创建了一个组合框和一个面板,因此当用户单击组合框中的一个数字时,点的数量以随机颜色随机出现在面板上。我试图搜索编码,但无法找到我真正需要的

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

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        Random r = new Random();
        Graphics g = this.CreateGraphics();
        //to randomize a color
        Color rC = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
        SolidBrush b1 = new SolidBrush(rC);
        g.FillEllipse(b1, e.X, e.Y, 30, 30);

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Cursor = Cursors.Cross;
    }
}

我一开始用的是这个,但是当用户点击面板时,圆点就会出现。我希望当用户按下组合框中的数字时,它们随机出现在屏幕上。

首先,您应该创建一个
组合框
,并将其附加到
SelectedIndexChanged
事件。 用所需的值填充组合框(我假设它们都是整数,正如您所说的)

现在我将创建此方法,根据给定的参数,多次使用随机位置和颜色绘制点:

private void randomPaint(int numberOfTimes)
{
    Random r = new Random();
    Graphics g = this.CreateGraphics();
    Color rC;
    SolidBrush b1;

    for (int i = 0; i < numberOfTimes; i++)
    {    
        // Randomize a color
        rC = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
        b1 = new SolidBrush(rC);
        // Paint with random position
        g.FillEllipse(b1, r.Next(this.Size.Width), r.Next(this.Size.Height), 30, 30);
    }
}

现在还不清楚发生了什么,你想发生什么。请你详细说明一下,让你的问题更清楚一些好吗?正如我所看到的,目前你没有使用
组合框来填充点或任何东西。使用组合框事件可能是
textchanged
而不是表单
MouseUp
事件!!不要在标题上使用标签!
try{
    randomPaint(Convert.ToInt32(comboBox1.SelectedItem));
}
catch (Exception e)
{
    // handle exception..
}