0

I've got function:

private string[] Letters = new string[11] {"", "A", "B", "C", "D", "E","F", "G","H", "I", "J"};

private void GenerateGameMap()
    {
        int LocA = 5;
        int LocB = 5;
        for(int i = 1; i < 12; i++)
        {
            for(int i2 = 1; i2 < 12; i2++)
            {
                Canvas canvas = new Canvas();
                canvas.Width = 26;
                canvas.Height = 26;
                canvas.Margin = new Thickness(LocA + 1, LocB + 1, 0, 0);
                Border border = new Border();
                border.BorderThickness = new Thickness(2);
                if (i == 1 || i2 == 1)
                {
                    Label label = new Label();
                    label.FontFamily = new FontFamily("Arial");
                    label.FontSize = 20;
                    label.Foreground = Brushes.White;
                    label.Margin = new Thickness(-1, -2, 0, 0);
                    label.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                    label.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    if(i == 1)
                    {
                        if(i2 > 1)
                        {
                            label.Content = Litery[i2 - 1];
                        }
                    }else
                    {
                        if(i2==1)
                        {
                            label.Content = (i - 1).ToString();
                        }
                    }
                    border.BorderBrush = Brushes.Gold;
                    canvas.Background = Brushes.Black;
                    canvas.Children.Add(label);
                }else
                {
                    border.BorderBrush = Brushes.CadetBlue;
                    canvas.Background = Brushes.BurlyWood;
                }
                if(i > 1 && i2 > 1 )
                {
                    canvas.Name = Letters[i2 - 1] + (i - 1).ToString();
                    canvas.MouseLeftButtonUp +=canvas_MouseLeftButtonUp;
                }
                border.Width = 28;
                border.Height = 28;
                border.Margin = new Thickness(LocA, LocB, 0, 0);
                LocA+=30;
                MainGameCanvas.Children.Add(canvas);
                MainGameCanvas.Children.Add(border);
                if(i2 == 11)
                {
                    LocA = 5;
                }
            }
            LocB += 30;
        }
    }

Everything is fine with that function. But when I'am trying to find canvas:

private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        string canvasName = Text1.Text + Text2.Text;
        var GameObject = MainGameCanvas.FindName(canvasName);
        Canvas SelectedGameobject = GameObject as Canvas;
        SelectedGameobject.Background = Brushes.YellowGreen;
    }

I'am receiving error 'Object reference not set on instance of an object' in line "SelectedGameobject.Background = Brushes.YellowGreen;". Is there other way to find this control ?. As i said, I'am sure that function GenerateGameMap() is working corectly.

Shagohad
  • 365
  • 1
  • 7
  • 23
  • So what does `GameObject` look like you please show all relevant code.. also your error is pretty much straight forward.. you need to look up how to do Casting as well you appear to have 2 potential issues from what I can see – MethodMan Jan 25 '15 at 22:24
  • I only see one line of code where the `Canvas` object's name is set, it doesn't appear to be guaranteed to be executed (skipped if either `i` or `i2` == 1), and in this incomplete code example, there's nothing to suggest that there's any assurance that `Text1.Text + Text2.Text` will result in the name of any `Canvas` child object. You really should just start by using the debugger to look at the children under `MainGameCanvas` and figure out why the object you're looking for isn't found. Post [a good, minimal, complete code example](http://stackoverflow.com/help/mcve) if you want specific help – Peter Duniho Jan 26 '15 at 04:24
  • 1
    Also, you may instead of the above code prefer to use an `ItemsControl`, with the `ItemsPanel` set e.g. to `UniformGrid`, binding a collection of model object class instances that describe each of the 12x12 map panels, and using a `DataTemplate` to describe how each model object should be rendered within each grid square. You can probably get the above to work, but it will never fully leverage the features found in WPF. – Peter Duniho Jan 26 '15 at 04:28

0 Answers0