问题描述
我最近正在做一个小项目可上面有很多的按钮,由于我要给它们赋予一个随机的图片(与"水晶连连看"相似),我打算把它们放到一个按钮类型的数组buttonarray中,可按钮太多,一个一个赋值太烦.
哪位高手有简单的办法可以帮我把当前窗体的部分按钮赋给buttonarray中.先谢谢了!
我最近正在做一个小项目可上面有很多的按钮,由于我要给它们赋予一个随机的图片(与"水晶连连看"相似),我打算把它们放到一个按钮类型的数组buttonarray中,可按钮太多,一个一个赋值太烦.
哪位高手有简单的办法可以帮我把当前窗体的部分按钮赋给buttonarray中.先谢谢了!
毕竟是常量enum
Foreach(Control al in Controls)
{
if(al is Button)
{
al.BlackImage=随机到的图片
}
}
用
ArrayList al=new ArrayList();//存路径
al.add(图片路径);
Random rx = new Random();
Foreach(Control al in Controls)
{
if(al is Button)
{
int i=rx.Next(0,al.count+1);al.BackgroundImage==Image.FromFile(al[i]);
}
}
你去试一下
随机一个ImageIndex;就可以了,方法实在是很多
丢进去就成obj了 可以用泛型试试
是不是你的button放在其他容器中啊,如panel,groupbox等 ,
放在哪个容器中,就用哪个容器的Controls进行遍历
如果直接放在FORM中,就用10楼的代码应该可行
al.add(图片路径);
Random rx = new Random();
Foreach(Control al in Controls
这里重复了一个了
al上面改掉一个
代码都给你看吧,我测试过了using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 联兴布行ERP
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
ArrayList arraylist = new ArrayList();
Random rx = new Random(); private void button1_Click(object sender, EventArgs e)
{
foreach (Control al in Controls)
{
if (al is Button)
{
int i = rx.Next(0, 2);
((Button)(al)).Image=Image.FromFile(arraylist[i].ToString());
}
}
} private void Form2_Load(object sender, EventArgs e)
{
arraylist.Add(@"C:\Documents and Settings\All Users\Documents\My Pictures\示例图片\Blue hills.jpg");
arraylist.Add(@"C:\Documents and Settings\All Users\Documents\My Pictures\示例图片\Sunset.jpg");
}
}}