WPF - C# Desktop App - Is there a way to call a Windows form from C# WPF?

Markus Freitag 3,786 Reputation points
2020-07-28T14:35:17.05+00:00

Hello,
Is there a way to call a Windows form from C#WPF?

The idea is to create a separate message box. Or a WPF message box.

With best regards Markus

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-07-30T09:58:20.677+00:00

    Hi Markus,
    create your own Messagebox like this:

    14672-x.png

    And CodeBehind:

    using System.Windows;  
    using System.Windows.Input;  
    using WpfApp01;  
      
    namespace WpfApp66  
    {  
      public partial class MyMessageBox : Window  
      {  
        public MyMessageBox()  
        {  
          InitializeComponent();  
          this.DataContext = this;  
        }  
        public static object ShowDialog(string msg) => (new MyMessageBox() { Message = msg }).ShowDialog();  
        public string Message { get; set; }  
        public ICommand Cmd { get => new RelayCommand((state) => { ExitMessageBox((state != null && state.ToString() == "True")); }); }  
        private void ExitMessageBox(bool result)  
        {  
          this.DialogResult = result;  
          this.Close();  
        }  
      }  
    }  
    

    And using:

    MyMessageBox.ShowDialog("Display my Message");  
    

    14534-30-07-2020-14-44-52.gif

    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Markus Freitag 3,786 Reputation points
    2020-07-29T10:03:51.507+00:00
    namespace TestApp
    {
        public static class MyMessageBox
        {
            //public static int OK = 0;
            //public static int OKCANCLE = 1;
    
            public enum MyMessageBoxStyle
            {
                OK = 0,
                OK_Cancel = 1,
                YES_NO = 2
            }
    
    
            public static bool? ShowDialog(string mess, MyMessageBoxStyle style) => MessageBoxT.ShowDialog(mess, style);
    
            private static MessageBoxIntern MessageBoxT = new MessageBoxIntern();
    
            private class MessageBoxIntern
            {
                public MessageBoxIntern()
                {
                }
    
                internal bool? ShowDialog(string mess, MyMessageBoxStyle style)
                {
                    switch (style)
                    {
                        case MyMessageBoxStyle.OK:
                            MessageBoxOK myMessageBoxOK = new MessageBoxOK(mess);
                            return myMessageBoxOK.ShowDialog();
    

    Hi, Can I do it this way? SingleTon pattern is the right solution?

    For each MessageBox I can expand this pattern, class MyMessageBox

    How is your opinion?
    anonymous user-3316 @DaisyTian-MSFT


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.