Search code examples
wpfmessagebox

Custom Message Box Advice


Well I'm using a Window as my custom message box with a couple of controls which are displayed/populated with text depending on which constructor is called.

I have a defined event, which is subscribed to via the original class, this fires once the button has been clicked.

However I can't see how to use this effectively, preferably I'd like to return a bool whether Yes or No was clicked, however obviously my code will carry on executing, hence the method which is subscibed to the button click. Below is some example code to make the issue clearer.

Message Box Window

public partial class CustomMessageBox : Window
    {

        public delegate void MessageBoxHandler(object sender, EventArgs e);
        public event MessageBoxHandler MessageBoxEvent;

        public CustomMessageBox()
        {
            InitializeComponent();
        }

        public CustomMessageBox(string message)
        {
            InitializeComponent();
            this.txtdescription.Text = message;
        }

        public CustomMessageBox(string message, string title, string firstBtnText)
        {
            InitializeComponent();
            this.lbltitle.Content = title;
            this.txtdescription.Text = message;
            this.btnstart.Content = firstBtnText;
        }

    }

    public static class MessageBoxButtonClick
    {

        public static bool Yes { get; set; }
        public static bool No { get; set; }
        public static bool Cancel { get; set; }
    }

Window Which Instantiates the MessageBox Window

private void StartProcess_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            foreach (var result in results)
            {
                if(result.ToBeProcessed)
                    _validResults.Add(new ToBeProcessed(result.Uri, result.Links));

            }
            _msgbox = new CustomMessageBox("Each Uri's backlinks will now be collected from Yahoo and filtered, finally each link will be visited and parsed. The operation is undertaken in this manner to avoid temporary IP Blocks from Yahoo's servers.", "Just a FYI", "OK");
            _msgbox.MessageBoxEvent += (MessageBoxHandler);

            if (_msgBoxProceed)
            {
                _msgbox.Close();
                Yahoo yahoo = new Yahoo();

                yahoo.Status.Sending += (StatusChange);

                //What I'd like to happen here is the code simply stop, like it does when calling a messagebox is winforms
                //e.g. 
                // if(ProceedClicked == true)
                // do stuff

               // yahoo.ScrapeYahoo(_validResults[Cycle].Uri, _validResults[Cycle].LinkNumber);

                //Cycle++;
            }
            else
            {
                _msgbox.Close();
            }

        }

private void MessageBoxHandler(object sender, EventArgs e)
        {
            if (MessageBoxButtonClick.Yes)
            {
                ProceedClicked = true;
            }
            else
            {
                ProceedClicked = false;
            }
        }

Hopefully that makes it clear enough, I can't put any execution code ie call a certain method due to using it multiple times throughout my application.


Solution

  • Very hard to understand what the problem exactly is. Also the code you wrote here, doesn't seemt to have any calls, that would actually show the CustomMessageBoxWindow.

    But I'll take a stab at this... First of all, am I right in guessing that in your main Window you want your code to wait at if(_msgBoxProceed) until the user actually presses a button in your CustomMessageBoxWindow (currently it just shows the message box and continues executing the next statements)?

    If so then I'm guessing you are showing your message box window with the Show() method. Use ShowDialog() instead. That will cause code execution to stop, until the message box gets closed.

    If you don't want to use a modal dialog then there are two options. Either use thread syncrhonization objects (eg AutoResetEvent) or set up a new event for when the message box closes and continue your code execution in the closed event handler (in StartProcess_Click the last line would be a call to _msgBox.Show() and everything from if(_msgBoxProceed) would be in the closed event handler).