dimanche 29 mars 2015

Random picture from class in button (C#)

I have to make a little game for school where you have flags of countries and their names (in a listbox (named lstCountries) for me) and you need to combine them to score points.


I want to get random flags in buttons which I create like this



//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);
}


And I made a class named Country:



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

public string Flag { get; set; }

public int Number { get; set; }
}


And I created the countries like this:



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..
}


I thought I would be able to give each button a random number from 0-40 (have 40 flags and 20 buttons), and link that random number to countryname.Number


If someone would be able to help me out (even if it's a completely different way) that would be awesome.. We're supposed to do this in a group of 3 people but everyone's refusing to do anything so I'm doing this alone ..





Aucun commentaire:

Enregistrer un commentaire