Link to home
Start Free TrialLog in
Avatar of cyimxtck
cyimxtckFlag for United States of America

asked on

WPF MainWindow update textbox from another class...

I need to update the status text box on the MainWIndow from another class.

Execute.cs should push a message to the text box tbxStatus on the MainWindow.  I have tried many things and this should just work...(in my opinion lol)

Execute.cs :
            catch (Exception ex)
            {
                var err = new MainWindow();
                String strInner = (ex.InnerException == null ? null : ex.InnerException.ToString());
                String strMsg = (ex.Message == null ? null : ex.Message.ToString());

                err.setStatus((strInner + "|" + strMsg), con_Bad);
               
            }


MainWindow:

        public void setStatus(
                                 String strText
                               , String strGoodBad
                               )
        {
            cleanStatus();

            tbxStatus.Text = strText;

            if (strGoodBad == con_bad)
            {
                tbxStatus.Foreground = Brushes.Red;
            }

            if (strGoodBad == con_good)
            {
                tbxStatus.Foreground = Brushes.Green;
            }
        }

This should be easy to me?  Set the text box from another window.

Could someone please tell me WHY it doesn't work as well as the solution?  This is very bizarre to me....

Thanks in advance,

B
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>Could someone please tell me WHY it doesn't work as well as the solution?  This is very bizarre to me....




Execute.cs :
            catch (Exception ex)
            {
                var err = new MainWindow();
                String strInner = (ex.InnerException == null ? null : ex.InnerException.ToString());
                String strMsg = (ex.Message == null ? null : ex.Message.ToString());

                err.setStatus((strInner + "|" + strMsg), con_Bad);
               
            }


You run code in a 'try' block and if it fails with an exception then a catch block of code is run.  Just what causes this catch block you have to execute ?  (If you only want to update the textbox when an exception is thrown then you are almost there).

                var err = new MainWindow();
The above line of code will create a NEW instance of the main window, not use the existing main window.  

You need to get the instance of the existing MainWindow which you pass into the other class so that the other class can call the function in the MainWindow to update the textbox.
Avatar of cyimxtck

ASKER

I get the try catch block part and I am creating this error on purpose to ensure the functionality is working which it is not.  The constructor is because I have tried a zillion different ways to get this to work.

Is there a clear cut way to do this?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There are other ways that WinForms support that are fractionally easier than the above, I don't know if WPF supports those.  Some things are trivial in a WinForm but an absolute nightmare in WPF.
I am seeing that and thanks for the prompt reply.  I guess the only issue that I will have is that the class foo will be the class that knows when/what to send/

Meaning, I have the class foo that fails some test and the try catch picks it up.  Now foo knows what the error message is but class MainWindow doesn't.

Know what I mean?  There wouldn't be a case where class MainWindow knows the error or when to show it.

Please let me know if that makes sense.

Thanks,

B
I did find that with your help~!

Thanks,

B