T
T
TechNOIR2018-12-17 11:30:27
C++ / C#
TechNOIR, 2018-12-17 11:30:27

C#. How to embed a dialog box in a console application?

Good afternoon!
Can you please tell me how to implement a dialog box in a console application?
Here's what I've got so far:

static void Main(string[] args)
        {
            Size resolution = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
            Console.WriteLine(resolution);

            if (resolution.Height == 1080 && resolution.Width == 1920)
            {
                Console.WriteLine("Yes");
                Form frmAbout = new Form();
                Button button1 = new Button();
                Button button2 = new Button();
                button1.Text = "Продолжить";
                button2.Text = "Завершить программу";
                frmAbout.Controls.Add(button1);
                frmAbout.ShowDialog();
            }


            Console.WriteLine("Я сработал");

        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            Тут надо закрыть форму и продолжить выполнение программы
        }
        private void button2_Click(object sender, System.EventArgs e)
        {
            А тут надо завершить выполнение программы
        }
    }

How to do it right? The form from button1_click cannot be closed. In both buttons, sign what this primitive program should do.
Thanks in advance for your help
PS I need a form with two buttons, not a DialogBox

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slav2, 2018-12-18
@Slav2

Most likely, you need to redefine the form and add a delegate and a function that closes the form.

delegate void CloseFormCallback();

        private void CloseForm()
        {
            try
            {
                if (this.IsDisposed || this.Disposing) return;

                CloseFormCallback d = CloseForm;

                if (this.InvokeRequired)
                {
                    Invoke(d);
                }
                else
                {
                       this.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question