Reputation: 921
I am making an app in which 4 chits are to be thrown randomly. I am done with generating random numbers by clicking on a button random numbers are generated between 1 to 4.
My question is how do I associate the 4 images with those numbers? Suppose if 1 comes randomly - then 1.jpg should be displayed. My programming language is c# and working in Visual studio 2012.
public sealed partial class MainPage : Page
{
Random rand = new Random();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int num=rand.Next(1,5);
textblock1.Text=num.ToString();
}}}
i m showing the numbers in a textblock .now i have 4 images which i have to display randomly.,my question is how and where to display them .
Upvotes: 1
Views: 10069
Reputation: 1
Random rand = new Random(); int randomPosition = rand.Next(0, 9);
lblEnglish.Text = sw.EnglishHeartSentence[randomPosition];
lblShona.Text = sw.shonaSentence[randomPosition];
switch (randomPosition)
{
case 0:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.diarrhea_900x600px;
break;
case 1:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.hunger1; ;
break;
case 2:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.stomachache1;
break;
case 3:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.full_clipart_1;
break;
case 4:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.Top_10_Foods_To_Relieve_Menstrual_Cramps_1;
break;
case 5:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.preg;
break;
case 6:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.belly;
break;
case 7:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.a_man_with_visible_spine_holds_his_back;
break;
case 8:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.bell;
break;
case 9:
mainPicBox.Image = Funda_isizulu_.Properties.Resources.coffee_growl_905;
break;
}
Upvotes: 0
Reputation: 921
Done used this method
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int dice = rand.Next(1, 5);
switch (dice)
{
case 1:
Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/1.png"));
break;
case 2:
two.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/2.png"));
break;
case 3:
three.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/3.png"));
break;
case 4:
four.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/4.png"));
break;
}
}for bitmap image refrence will be "using Windows.UI.Xaml.Media.Imaging;"
Upvotes: 3
Reputation: 35726
er,
switch (number)
{
case 1:
return "1.jpg";
case 2:
return "2.jpg";
case 3:
return "3.jpg";
case 4:
return "4.jpg";
default:
throw ArgumentException("number");
}
it all depends really,
how about,
private static readonly WhateverTypeMyImagesNeedToBe[] images =
{
new WhateverTypeMyImagesNeedToBe("1.jpg"),
new WhateverTypeMyImagesNeedToBe("2.jpg"),
new WhateverTypeMyImagesNeedToBe("3.jpg"),
new WhateverTypeMyImagesNeedToBe("4.jpg")
}
private static WhateverTypeMyImagesNeedToBe GetRandomImage()
{
return images[new Random().Next(0, images.Length)];
}
Upvotes: -1
Reputation: 16623
What about:
Random rand = new Random();
Bitmap GetRandomImage(){
Bitmap bmp;
int n = rand.Next(0, 4);
if(n == 0)
bmp = new Bitmap("mypath1");
else if(n == 1)
bmp = new Bitmap("mypath2");
else if(n == 2)
bmp = new Bitmap("mypath3");
else
bmp = new Bitmap("mypath4");
return bmp;
}
Or you can rename your files and use something like:
Random rand = new Random();
Bitmap GetRandomImage(){
int n = rand.Next(0, 4);
return new Bitmap(string.Format("mypath\\{0}.jpg", n));
}
Upvotes: 0
Reputation: 13541
You could just substitute the generated number to a file name like:
string.Format("{0}.jpg", generatedNumber);
Very little code, no 'if' or 'switch' needed.
I would also make a separate component (I)FileNameResolver that contains the logic to determine the correct file name and error handling; and use that one in your code.
Upvotes: 1