【问题标题】:Random picture from class in button按钮中班级的随机图片
【发布时间】:2015-03-29 12:47:20
【问题描述】:

我必须为学校制作一个小游戏,其中你有国家的国旗和他们的名字(在一个列表框中(对我来说是 lstCountries),你需要将它们组合起来以获得分数。

我想在我这样创建的按钮中获得随机标志

//Buttons Row 1
            for (int i = 0; i < 5; i++)
            {
                Button btnNew = new Button();
                btnNew.Location = new System.Drawing.Point(230 + 110 * i, 100);
                btnNew.Name = "btnFlag";
                btnNew.Size = new System.Drawing.Size(100, 100);
                btnNew.TabIndex = 0;
                btnNew.Text = "";
                btnNew.UseVisualStyleBackColor = true;
                btnNew.Click += new System.EventHandler(buttonToevoegen);
                Controls.Add(btnNew);
                aButtons.Add(btnNew);
            }

我创建了一个名为 Country 的类:

class Country
    {
        public string Name { get; set; }

        public string Flag { get; set; }

        public int Number { get; set; }
    }

我创建了这样的国家:

private void vlaggenDeclareren()
        {
            Country oAlbanie = new Country();
            oAlbanie.Name = "Albanie";
            oAlbanie.Flag = "Albanie.png";
            oAlbanie.Number = 0;
            lstLandVullen(oAlbanie);

            Country oAndorra = new Country();
            oAndorra.Name = "Andorra";
            oAndorra.Flag = "Andorra.png";
            oAndorra.Number = 1;
            lstLandVullen(oAndorra);

            //and so on..
        }

我想我可以给每个按钮一个从 0 到 40 的随机数(有 40 个标志和 20 个按钮),并将该随机数链接到 countryname.Number

如果有人能够帮助我(即使是完全不同的方式),那就太棒了.. 我们应该在一个 3 人的小组中执行此操作,但每个人都拒绝执行任何操作,所以我一个人执行此操作..

【问题讨论】:

  • 到目前为止一切正常。现在你想要一个例程 setRandomFlags() 它将:1)创建一个列表来收集使用的标志,然后 2)为你的按钮列表中的每个按钮做:检查并重复如果一个新的随机索引不在列表中,然后使用它分配按钮的标志和国家(后者可能在标签中)并将用完的索引/标志/国家添加到列表中。确保在循环之外创建随机对象!
  • 但是如何把随机数和国家的号码联系起来呢?
  • 在国家列表长度范围内创建一个随机索引:int rInd = R.Next(lstLandVullen.Count);(直到你找到一个新的)(假设它是一个列表;当你可以使用列表时尽量避免使用数组! ) - 然后你可以像这样使用它:foreach(Button btn in aButtons) { /*create index first, then..:*/ btn.Text = lstLandVullen[rInd].Name; etc.. incl. usedList.Add(rInd); } - 你到底想用 country.number 做什么?列表中的所有对象都不需要它。
  • 请随时提出更多问题或为问题添加您的努力,以便我们帮助解决问题..!
  • 好吧,我想给每个国家一个数字,然后当我制作按钮时,按钮会得到一个 0-40 之间的随机数,假设德国获得数字 7,按钮获得数字 7 它会从德国获取国旗并将其用作该按钮的背景图片

标签: c# class button random


【解决方案1】:

这是我的 cmets 的代码:

从我们的聊天中我现在知道你有一个ListBox lstLanden,你用Country 对象填充它的Items,它的DisplayMemberCountry.Name

void setRandomFlag()
{
    // a random number generator
    Random R = new Random();
    // a list to keep our used up country numbers
    List<int> usedFlags = new List<int>();
    // the number of countries to chose from:
    int cCount = lstLanden.Items.Count;


    // first index we try
    int rInd = R.Next(cCount );
    // we shall give each button a flag etc..
    foreach (Button btn in abuttons)
    {
        // find an unsed index
        while (usedFlags.Contains(rInd)) {rInd = R.Next(cCount );}
        // store it
        usedFlags.Add(rInd);
        // pull the Country object from the listbox
        Country country = (Country)lstLanden.Items[rInd];
        // use it
        btn.BackgroundImage = Image.FromFile(country.Flag);
        // this is a trick to keep a reference to the country at each button
        // we may need it later or not..
        btn.Tag = country;
    }
}

这应该为每个Button分配一个标志和一个Country对象。

下一个任务是比较ListBoxSelectedItem 的Text 与ButtonCountryName

为此,为所有Buttons 创建一个通用点击事件,将发件人转换为Button,然后将其Tag 转换为Country..

【讨论】:

  • 由于某种原因我无法计算列表:/
  • 请加入聊天! (请参阅问题的最后一条评论!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 2018-09-29
  • 2012-12-17
相关资源
最近更新 更多