If you are getting a null there:
void button1_Click(object sender, RoutedEventArgs e)
{
System.Windows.Controls.Button btn1 = sender as System.Windows.Controls.Button;
Grid grid1 = (Grid)btn1.Parent;
System.Windows.Controls.RichTextBox myRichTextBox = (System.Windows.Controls.RichTextBox)grid1.FindName("rbox");
Then there are two possible reasons:
1) If the null occured in the line:
Grid grid1 = (Grid)btn1.Parent;
Then the originator of the event is not a Button. You shoudl always test for null after using
as
to make sure it is valid:
System.Windows.Controls.Button btn1 = sender as System.Windows.Controls.Button;
if (btn1 != null)
{
Grid grid1 = (Grid)btn1.Parent;
Even if your error isn't on this line, it is good practice to do this anyway.
2) If the null occurred on this line:
System.Windows.Controls.RichTextBox myRichTextBox = (System.Windows.Controls.RichTextBox)grid1.FindName("rbox");
Then the parent Grid does not appear to contain the name "rbox", or the Button which initiated this does not have a parent.
Put a breakpoint on the first line of the event handler, and step through, looking at the variables - it will be fairly obvious what the problem is.