我创建了一个组合框和一个面板,因此当用户点击组合框中的数字时,点的数量会随机地随机定位在面板上。我尝试搜索编码但无法找到我真正需要的东西。
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;
}
}
我最初使用它,但这是当用户点击面板并出现点时。当用户按下组合框中的数字时,我希望它们随机出现在屏幕上。
答案 0 :(得分:1)
首先,您应该创建ComboBox
并将其附加到SelectedIndexChanged
事件。
使用所需的值填充您的ComboBox(我假设它们都是整数,正如您所说的那样)。
现在我创建这个方法用随机位置和颜色绘制点,多次,取决于给定的参数:
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);
}
}
并将此代码添加到SelectedIndexChanged
处理程序:
try{
randomPaint(Convert.ToInt32(comboBox1.SelectedItem));
}
catch (Exception e)
{
// handle exception..
}