尝试以随机顺序将图片读入flowlayoutpanel

时间:2018-12-15 18:14:31

标签: c#

我正在尝试为自己制作一个类似于存储卡游戏的程序,但出于教育目的,您需要使单词和单词的定义匹配。到目前为止,我已成功理解以下内容:

  1. 我在FlowLayoutPanel上使用PictureBox,我有150多个带有说明的单词(300多个图片),因此滚动工作并且它们彼此相邻。
  2. 我可以用button1_click删除图片面板1到1,但是我必须删除1个字和1个描述,否则看起来很奇怪。只要将它们成对移除,就不是问题。

我想做的,我坚持的是:

  • 当我在阅读图片时,我已经希望它们以随机的顺序出现,例如,图片中我仅提供了2个单词+描述,我想随机地包含单词或描述(或两者都有)顺序,因此正确的人的arent必然会彼此相邻,但由于它是随机的,因此它可能会发生的可能性很小(读法如下:pic1,pic1a,pic2,pic2a ... picN,picNa)
  • 当我单击与描述匹配的单词时(例如,可能用名称来指代它?单击pic1和pic1a),将删除这两个PictureBox,直到没有PictureBox为止删除。

现在看起来如何,红色就是我想要的样子,例如2:

image

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;

            }
        }
    }
}

1 个答案:

答案 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; }
    }
}