RazorSharp
RazorSharp

Reputation: 189

WPF C# Pass Rich Text Editor from class to Main Window

I need some help on passing a value from one class to the main window. The Main Window is up and running in the background, so a new instance is not needed (I don't think). The Main Window is a tab control using DevExpress. One the tab, I have a button. When the button is pressed, it opens a new window. I type text into the new window, check the spelling, and press the Return Data button. When I press the Return Data button, I want the text in the Rich Text Editor to pass to the Main Window in a textbox.

Here's the code I have now, but for some reason I keep getting that annoying error "object not set to an instance of an object". Can you help?

public MainWindow Main = null;

private void ReturnData_Click(object sender, RoutedEventArgs e)
    {
        var val = RichEditControl1.Text;
        Main.TbNoteText.Text = val;
        Close();
    }

Upvotes: -1

Views: 209

Answers (1)

d.moncada
d.moncada

Reputation: 17402

The reason why you are getting a null ptr exception is that you are trying to access Main when it is never set (the declaration is being set to null).

You can get an instance of the MainWindow like this:

private void ReturnData_Click(object sender, RoutedEventArgs e)
{
    var mainWindow = (MainWindow) Application.Current.MainWindow;
    var val = RichEditControl1.Text;

    mainWindow.TbNoteText.Text = val;
    Close();
}

Upvotes: 1

Related Questions