Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am nobie in C# .NET Core, and I would like to know if it is possible to display a TaskDialog in a Console type project?
With the C# .NET Framework I know it is possible to do this with MessageBox.


What I have tried:

Reading everything I can, on the internet and books on the subject ...
Posted
Updated 5-Apr-21 13:54pm
Comments
SeanChupas 5-Apr-21 14:24pm    
You could do it in the Framework? Possibly, but I doubt it. A console app is just an old black DOS command window. I don't think, though I never tried it, that it could show Dialogs.
BillWoodruff 5-Apr-21 20:00pm    
A good idea to test and experiment before posting what you "doubt." See my post,
SeanChupas 6-Apr-21 7:57am    
Sure thing bossy.

No, you can't display a dialog of any kind in a console app. That's why it's a console app.

Curiously though, you should be able to write a WinForms or WPF app without actually having to display a window, unless you choose to do so.
 
Share this answer
 
Comments
BillWoodruff 5-Apr-21 19:09pm    
Vote of #1: absolutely wrong ... you can use MessageBox.Show, and you can create and show (modal, or non-modal) Forms in a console app. See example here.

However, just because you CAN, does not mean you should, or that it is a good idea !

BillWoodruff 6-Apr-21 23:12pm    
changed vote to #3:

"Curiously though, you should be able to write a WinForms or WPF app without actually having to display a window, unless you choose to do so."

indeed, you can, using WinForms Application Context, but, why would you ?
I have not used Win Core.

Note: Think carefully before using a technique like this ... many users are socialized to expecting a Console App to not use MessageBox, or show Forms !

1) add using System.Windows.Forms; to your Console App.

1a) add a Form to your Console App Project. Put a TextBox and a Button on the Form.

2) example Console App
using System;
using System.Windows.Forms;

namespace ConsoleAppWithForm
{
    class Program
    {
        static void Main(string[] args)
        {
            // works
            //MessageBox.Show("????");

            Form1 f1 = new Form1();

            string data = null;

            // wire-up the Action delegate in Form1
            f1.TransferData = s => data = s;
            
            DialogResult dresult = f1.ShowDialog();

            f1.Dispose();

            Console.WriteLine($"result: {dresult} | value entered: {data}");

            Console.ReadKey();
        }
    }
}
3) example Form
using System;
using System.Windows.Forms;

namespace ConsoleAppWithForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // event delegate for external consumers
        public Action<string> TransferData;

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != String.Empty && TransferData != null)
            {
                TransferData(textBox1.Text);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }

            this.Close();
        }
    }
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 6-Apr-21 3:54am    
But strictly speaking that is not a Console app.
BillWoodruff 6-Apr-21 4:07am    
"that depends on what the meaning of 'is' is." Bill Clinton :)

the example shown was created as a Console app; the VS compiler thinks it's a Console app.
Richard MacCutchan 6-Apr-21 4:13am    
If you pain an elephant yellow that does not make it a banana. :))
#realJSOP 6-Apr-21 5:44am    
That's like saying a baby born as a male is a boy, and according to the latest information available, we all know that's not true.
BillWoodruff 6-Apr-21 6:18am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900