Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void FormWPFCreate()
        {
          //Create a windows form with WPF namespace
                window2 window = new window2();

                                   window.Title = "Form to create a Mail";
                        

        //Set the dimension
                window.Height =600;
                window.Width =800;

                //Create the Grid Control
                Grid newgrid = new Grid();
                //Show the Grid Lines
                newgrid.ShowGridLines = true;

                //Define Row of Grid
                RowDefinition rowDef1 = new RowDefinition();
                rowDef1.Height = new GridLength(120, GridUnitType.Pixel);
                RowDefinition rowDef2 = new RowDefinition();
                rowDef2.Height = new GridLength(480, GridUnitType.Pixel);
                //Add the row to the grid component
                newgrid.RowDefinitions.Add(rowDef1);
                newgrid.RowDefinitions.Add(rowDef2);

                // Create a name scope for the Grid
                // and assign a name
                NameScope.SetNameScope(newgrid, new NameScope());


     #region Button
                System.Windows.Controls.Button button1 = new System.Windows.Controls.Button();
                button1.Height = 39;
                button1.Margin = new System.Windows.Thickness(10,340, 500, 10);
                button1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                button1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                button1.Width = 191;
                button1.Content = "Send Email";

                button1.Click += new RoutedEventHandler(button1_Click);
                button1.Tag = window;

                Grid.SetRow(button1, 1);
                #endregion



   // Create a FlowDocument
                FlowDocument mcFlowDoc = new FlowDocument();


                // Create a paragraph with text
                Paragraph para = new Paragraph();
                para.Inlines.Add(new Run("I am a flow document. Would you like to edit me? "));
                para.Inlines.Add(new Bold(new Run("Go ahead.")));


                // Add the paragraph to blocks of paragraph
                mcFlowDoc.Blocks.Add(para);


                // Create RichTextBox, set its hegith and width
                System.Windows.Controls.RichTextBox mcRTB = new System.Windows.Controls.RichTextBox();
                mcRTB.Width = 560;
                mcRTB.Height = 280;
             

                // Set contents
                mcRTB.Document = mcFlowDoc;
                mcRTB.Name = "rbox";
                

                // Add RichTextbox to the container

                Grid.SetRow(mcRTB, 1);


  newgrid.Children.Add(button1);   
  newgrid.Children.Add(mcRTB);

}



  void button1_Click(object sender, RoutedEventArgs e)
        {
           
System.Windows.Controls.Button btn1 = sender as System.Windows.Controls.Button;        

              Grid grid1 = (Grid)btn1.Parent;



//Here I got null exception  how to read the content of rich text box here?
              System.Windows.Controls.RichTextBox myRichTextBox = (System.Windows.Controls.RichTextBox)grid1.FindName("rbox");


[edit]Urgency deleted - OriginalGriff[/edit]
Posted
Updated 9-Mar-12 21:14pm
v3
Comments
OriginalGriff 10-Mar-12 3:14am    
Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.

The WPF RichTextBox has a Document Property which can be used to get the contents from the control, something like this:-

C#
TextRange rtbText = new TextRange(mcRTB.Document.ContentStart, mcRTB.Document.ContentEnd);
string allText = rtbText.Text;


Hope this helps
 
Share this answer
 
If you are getting a null there:
C#
void button1_Click(object sender, RoutedEventArgs e)
   {
   System.Windows.Controls.Button btn1 = sender as System.Windows.Controls.Button;        
   Grid grid1 = (Grid)btn1.Parent;
//Here I got null exception  how to read the content of rich text box here?
   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:
C#
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:
C#
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:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900