Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Creating Image - Does not Exist in Current Context
    primarykey
    data
    text
    <p>I am trying to create a WhackaMole game in WPF in C#. I am a bit of a noob. In the for loop I am trying to add the number of "i" to the "Image". I get the following error:</p> <pre><code> "Error The name 'Image1' does not exist in the current context" </code></pre> <p>same thing for 'Image2' and so on. I am trying to integrate the images into a StackPanel. </p> <p>Thanks for the help :)</p> <pre><code> public partial class MainWindow : Window { Image[] ImageArray = new Image[50]; public MainWindow() { Moleini = MoleScore[1]; InitializeComponent(); //string ImageName = "Image"; for (int i = 0; i &lt;= 8; i++) { Image Image = new Image(); ImageArray[i] = Image; Image.Name = "Image" + i.ToString(); } ////Create Images //for (int i = 0; i &lt;= 8; i++) //{ // StackPanel1.Children.Add(CreateImage(i)); //} //Dispacher for Mole to Appear System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = TimeSpan.FromSeconds(1); dispatcherTimer.Start(); //Dispacher for Full Game Time System.Windows.Threading.DispatcherTimer endGame = new System.Windows.Threading.DispatcherTimer(); endGame.Tick += new EventHandler(endGame_Tick); endGame.Interval = TimeSpan.FromSeconds(5); endGame.Start(); } ////Create Image //public Image CreateImage(int i) //{ //} private void dispatcherTimer_Tick(object sender, EventArgs e) { //Random Number Generator Random rnd = new Random(); int num = rnd.Next(1, 9); //If Random Number is "1" Then Image will display if (num == 1) { ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole)); Image1.Source = MoleImage; } //If Random Number does not equal 1 if (num != 1) { ImageSource hole = new BitmapImage(new Uri(ImgHole)); Image1.Source = hole; } //If Random Number is "2" Then Image will display if (num == 2) { ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole)); Image2.Source = MoleImage; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload