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,691 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 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. DaisyTian-1203 11,616 Reputation points
    2020-07-29T08:54:34.71+00:00

    You will give you a very simple demo for showing the custom MessageBox.
    MyMessageBox.cs

        public static int OK = 0;  
        public static int OKCANCLE = 1;  
        public MyMessageBox() { }  
    
        public static bool? ShowDialog(string mess, int style)  
        {  
            switch (style)  
            {  
                case 0:  
                    MessageBoxOK myMessageBoxOK = new MessageBoxOK(mess);  
                    return myMessageBoxOK.ShowDialog();  
                case 1:  
                    MessageBoxOKCancle myMessageBoxOKCancle = new MessageBoxOKCancle(mess);  
                    return myMessageBoxOKCancle.ShowDialog();  
                default:  
                    return false;  
            }  
        }  
      
    MessageBoxOK.cs  
    
     public MessageBoxOK()  
            {  
                InitializeComponent();  
            }  
            public MessageBoxOK(string mess)  
            {  
                InitializeComponent();  
                message.Text = mess;  
            }  
            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                try  
                {  
                    this.DialogResult = false;  
                }  
                catch (Exception ex) { }  
      
                this.Close();  
            }  
    

    MessageBoxOK.xaml
    14254-2020-07-29-165354.jpg

    Then you can use

    MyMessageBox.ShowDialog("This is content", MyMessageBox.OK);

    to show the MessageBoxOK

    1 person found this answer helpful.
    0 comments No comments

  2. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-07-28T17:54:23.803+00:00

    Hi Markus,
    you can create Window with content and call ShowDialog:

      public class ViewModel
      {
        public ICommand Cmd
        {
          get => new RelayCommand((state) =>
          {
            var wnd = new Window() { Width = 200, Height = 100 };
            var grid = new Grid();
            wnd.Content = grid;
            grid.Children.Add(new TextBlock() { Text = "Message", 
              HorizontalAlignment = HorizontalAlignment.Center });
            wnd.ShowDialog();
          });
        }
      }
    

  3. Markus Freitag 3,786 Reputation points
    2020-07-29T05:10:18.827+00:00

    See my comment14199--unclear.png


  4. Markus Freitag 3,786 Reputation points
    2020-07-29T05:54:19.117+00:00

    OK, I can do it normal.

    <Window x:Class="Test.MessageBoxInfo"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:PreSettingStation"  
            mc:Ignorable="d"  
            Title="MessageBoxInfo Error" Height="450" Width="800">  
        <Grid>  
            <TextBox HorizontalAlignment="Left" Height="74" Margin="180,199,0,0" TextWrapping="Wrap" Text="Fehler" VerticalAlignment="Top" Width="157" TextChanged="TextBox_TextChanged"/>  
      
        </Grid>  
    </Window>  
    

    How can I check the OK button when closing the window?
    Or YES, NO, Cancel

    14147--mess.png