1

So I'm quite new to programming and especially c# so i hope you can help me. I have two WPF forms and when I'm pressing a button in one of them I want to draw a grid in my canvas. So when I click the button it calls the right function and everything, but the grid just won't show up. I tried to look up people with similar problems but couldn't figure out what is wrong. Here is some of my code:

namespace GameOfLife
{
  public partial class SetupPopUp : Window
  {
    public SetupPopUp()
    {
      InitializeComponent();
    }

    private void OkButton_Click(object sender, RoutedEventArgs e)
    {
      int cols;
      int rows;

      int.TryParse(tb_numCol.Text, out cols);
      int.TryParse(tb_numRows.Text, out rows);
      this.Close();
      MainWindow.Instance.DrawGrid(rows,cols);
    }
  }
}

and:

namespace GameOfLife
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private static MainWindow _instance;

    public static MainWindow Instance
    {
      get
      {
        return _instance = _instance ?? new MainWindow();
      }
    }

    public void DrawGrid(int rows, int cols)
    {
      for (int i = 0; i < rows; i++)
      {
        for (int j = 0; j < cols; j++)
        {
          Rectangle r = new Rectangle();                  // Erstellt die Rechtecke und fügt Sie dem Canvas hinzu
          r.Width =  MainCanvas.Width / rows;
          r.Height = MainCanvas.Height / cols;
          r.Fill = Brushes.WhiteSmoke;
          r.Stroke = Brushes.Black;
          r.StrokeThickness = 0.5;
          MainCanvas.Children.Add(r);

          Canvas.SetLeft(r, j * r.Width);                 //Reit die Rechtecke aneinander
          Canvas.SetTop(r, i * r.Height);

          r.MouseDown += R_MouseDown;
        }
      }
    }
Tantem
  • 55
  • 5

2 Answers2

1

Beside of that what @mrid mentioned, your singleton implementation might not work quite right. Maybe refer to here on how to make MainWindow a singleton.

Edit:

As @Tantem mentioned in the comments, the singleton problem can also be easily avoided by calling:

((MainWindow)Application.Current.MainWindow).DrawGrid(rows,cols);
alxnull
  • 909
  • 9
  • 18
  • 1
    Thank you so much, i was already suspecting it but couldn't put my finger on it because i'm a beginner, i now ended up just calling it with : ((MainWindow)Application.Current.MainWindow).DrawGrid(rows,cols) and it works fine now – Tantem Sep 16 '18 at 12:32
  • 1
    @TaW you're right. As the linked solution is already described in detail there I added the solution the questioner has finally decided on. – alxnull Sep 16 '18 at 13:53
0

Your this.Close() is closing your SetupPopUp form before MainWindow.Instance.DrawGrid(rows,cols) is being called

mrid
  • 5,782
  • 5
  • 28
  • 71
  • I thought of that too, but unfortunatly this is not the problem. Thank you anyways :) – Tantem Sep 16 '18 at 12:11
  • You're welcome. But are you sure that isn't the problem? Is the call going to DrawGrid on the second window ? – mrid Sep 16 '18 at 12:13
  • I tested it again when you commented but no. When debugging you can see the program iterating through the DrawGrid() function, but without result. – Tantem Sep 16 '18 at 12:15