質問

I have a pointing to null error but i dont see where the problem is since everything gets initalised before used. the points where the error acures i have given a blockquote. Like always im thankfull for every help.

Button topAddBut = null;
//Button botAddBut = null;
Button loadBut = null;
Button saveBut = null;
StackPanel topSP = null;
//StackPanel botSP = null;      

public MainWindow()
{
      InitializeComponent();

      loadBut = new Button { Content = "Load", Width = 70, Height = 23 };
      Canvas.SetRight(loadBut, 160);
      Canvas.SetBottom(loadBut, 24);
      canvas1.Children.Add(loadBut);

      saveBut = new Button { Content = "Save", Width = 70, Height = 23 };
      Canvas.SetRight(saveBut, 80);
      Canvas.SetBottom(saveBut, 24);
      canvas1.Children.Add(saveBut);

      StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
      Canvas.SetLeft(topSP, 160);
      Canvas.SetTop(topSP, 100);

      AddWrapPanelTop();

      AddTextBoxTop();
      AddTopButton();
}

void AddTextBoxTop()
{
     TextBox txtB1 = new TextBox();
     txtB1.Text = "Text";
     txtB1.Width = 75;
     txtB1.Height = 75;
     WrapPanel wp = (WrapPanel)topSP.Children[0];
     wp.Children.Add(txtB1);   
 }

 void AddWrapPanelTop()
 {    
      WrapPanel myWrapPanel = new WrapPanel();

      SolidColorBrush mySolidColorBrush = new SolidColorBrush();
      mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

      myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
      myWrapPanel.Orientation = Orientation.Horizontal;
      myWrapPanel.Width = 4000;
      myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
      myWrapPanel.VerticalAlignment = VerticalAlignment.Top;

      topSP.Children.Add(myWrapPanel);
  }

  void AddTopButton()
  {    
       TextBox txtB1 = new TextBox();
       txtB1.Background = System.Windows.Media.Brushes.Magenta;
       txtB1.Text = "Text";
       txtB1.Width = 75;
       txtB1.Height = 75;
       topAddBut = new Button();
       topAddBut.Click += new RoutedEventHandler(this.TopClick);
       topAddBut.Content = "Add";
       topAddBut.Width = 75;

       // Add the buttons to the parent WrapPanel using the Children.Add method.
       WrapPanel wp = (WrapPanel)topSP.Children[0];
       wp.Children.Add(txtB1);
       wp.Children.Add(loadBut);
       this.topSP.Children.Add(wp);
   }

   void TopClick(Object sender, EventArgs e)
   {
        TextBox txtB1 = new TextBox();
        txtB1.Background = System.Windows.Media.Brushes.Magenta;
        txtB1.Text = "Text";
        txtB1.Width = 75;
        txtB1.Height = 75;
        Button s = (Button)sender;
        WrapPanel wp = (WrapPanel)s.Parent;
        wp.Children.Remove(s);
        wp.Children.Add(txtB1);
        AddTopButton();

        // Add the buttons to the parent WrapPanel using the Children.Add method.
    }
  }
}
役に立ちましたか?

解決

You define the following:

StackPanel topSP = null;

Then you have

StackPanel topSP = new StackPanel { Width = 400, Height = 50 };

This is in your local scope though so will be destroyed when you exit the function

Finally you have

topSP.Children.Add(myWrapPanel);

This will still be set to null which is why your error occurs. Basically you are creating a local version of the same variable.

In order to resolve simply change the second definition to:

topSP = new StackPanel { Width = 400, Height = 50 };
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top