我正在尝试为自己制作一个类似于存储卡游戏的程序,但出于教育目的,您需要使单词和单词的定义匹配。到目前为止,我已成功理解以下内容:
FlowLayoutPanel
上使用PictureBox
,我有150多个带有说明的单词(300多个图片),因此滚动工作并且它们彼此相邻。我想做的,我坚持的是:
PictureBox
,直到没有PictureBox
为止删除。现在看起来如何,红色就是我想要的样子,例如2:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void pbClick(object sender, EventArgs e)
{
var pictureBox = (PictureBox)sender;
int index = flowLayoutPanel1.Controls.GetChildIndex(pictureBox);
flowLayoutPanel1.Controls.Remove(pictureBox);
//MessageBox.Show(index.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
// allow multiple selection
d.Multiselect = true;
// filter the desired file types
d.Filter = "JPG |*.jpg|PNG|*.png|BMP|*.bmp";
// show the dialog and check if the selection was made
if (d.ShowDialog() == DialogResult.OK)
{
foreach (string image in d.FileNames)
{
// create a new control
PictureBox pb = new PictureBox();
// assign the image
pb.Image = new Bitmap(image);
// stretch the image
pb.SizeMode = PictureBoxSizeMode.StretchImage;
// set the size of the picture box
pb.Height = pb.Image.Height;
pb.Width = pb.Image.Width;
// add the control to the container
flowLayoutPanel1.Controls.Add(pb);
pb.Click += pbClick;
}
}
}
}
答案 0 :(得分:0)
下面的代码将相同的列表随机两次,以模拟您的字符串和图片。然后从两个随机列表中一次删除一对。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Item> items = new List<Item>() {
new Item() { name = "a", number = 1 },
new Item() { name = "b", number = 2 },
new Item() { name = "c", number = 3 },
new Item() { name = "d", number = 4 },
new Item() { name = "e", number = 5 },
new Item() { name = "f", number = 6 },
new Item() { name = "g", number = 7 },
new Item() { name = "h", number = 8 },
new Item() { name = "i", number = 9 },
new Item() { name = "j", number = 10 },
};
Random rand = new Random();
List<Item> randLetters = items.Select((x, i) => new { x = x, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.x).ToList();
List<Item> randNumbers = items.Select((x, i) => new { x = x, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.x).ToList();
//delete random pairs
for (int i = randLetters.Count - 1; i >= 0; i--)
{
//find matching item in randNumbers and remove from list.
randNumbers = randNumbers.Where(x => x.name != randLetters[i].name).ToList();
randLetters.RemoveAt(i);
}
}
}
public class Item
{
public string name { get; set; }
public int number { get; set; }
}
}