0

I would like to be able to show a form and the console and to write text from the form to the console but I'm not able to do it.

Firstly, I tried to make a console program and add System.Windows.Forms to it to create the form. It worked but when I come with the mouse on the form it seems freeze.

Second I thought it was because the form was part of the console thread and while the console thread is waiting a ReadKey the form freeze. So I made a backgroundworker to make a second thread where I open my form but it didn't work either.

So it's something I miss and I ask you for a little help. Here is my code so far:

using System.Windows.Forms;
using System.Threading;

namespace testform
{
    class Program
    {
        static void Main(string[] args)
        {
            BackgroundWorker bgworker = new BackgroundWorker();
            bgworker.WorkerSupportsCancellation = true;
            bgworker.WorkerReportsProgress = true;
            bgworker.DoWork += new DoWorkEventHandler(secondthread.bgworker_DoWork);
            bgworker.RunWorkerAsync();
            Console.Read();
        }

        public static class secondthread
        {
            private static Form myform = new Form();//create my form
            private static TextBox mytxtbox = new TextBox();

            public static void bgworker_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
                mytxtbox.TextChanged += new EventHandler(mytxtbox_TextChanged);
                myform.Controls.Add(mytxtbox);
                myform.Show();
                for (int i = 1; (i <= 10); i++)
                {
                    if ((worker.CancellationPending == true))
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        // Perform a time consuming operation and report progress.
                        System.Threading.Thread.Sleep(500);
                        worker.ReportProgress((i * 10));
                    }
                }
            }

            private static void mytxtbox_TextChanged(object sender, EventArgs e)
            {
                Console.WriteLine(mytxtbox.Text);
            }
        }
    }
}

I had to make all static 'cause I start from main.

Could you help me?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
JudgeDreed
  • 143
  • 1
  • 13

2 Answers2

2

To show a Form via Console you need to do look here first.

And you don't need another thread.

Sample code

[STAThread]
private static void Main() {
    Application.EnableVisualStyles();

    //form things
    TextBox tbx = new TextBox();
    Form form = new Form();
    tbx.TextChanged += Tbx_TextChanged;
    form.Controls.Add(tbx);
    form.Show();

    Application.Run(form);

    Console.ReadLine();
}


private static void Tbx_TextChanged(object sender, EventArgs e) {
    TextBox tbx = sender as TextBox;
    Console.Write(tbx.Text);
}
tetralobita
  • 453
  • 6
  • 16
-1

This is not the best code. But is a way to help you understand :-

 static void Main(string[] args)
 {
    Form mainForm = new Form();
    TextBox txtInput = new TextBox();
    txtInput.Height = 50;
    txtInput.Multiline = true;
    txtInput.Dock = DockStyle.Fill;

    Button submitBtn = new Button();
    submitBtn.Text = "Send to Console";
    submitBtn.Dock = DockStyle.Bottom;
    submitBtn.Click += (x, e) =>
    {
        Console.WriteLine(txtInput.Text);
    };

    mainForm.Controls.Add(txtInput);
    mainForm.Controls.Add(submitBtn);

    mainForm.ShowDialog();
 }
Prateek Shrivastava
  • 1,877
  • 1
  • 10
  • 17
  • You need an `Application.Run` to start the message loop. – Enigmativity Oct 26 '17 at 06:56
  • @Enigmativity - But why is that required in this scenario? Read: https://stackoverflow.com/questions/2314514/whats-the-difference-between-application-run-and-form-showdialog – Prateek Shrivastava Oct 26 '17 at 07:04
  • Because the OP is doing `form.Show()` and not `form.ShowDialog()`, but it might be that your approach works. – Enigmativity Oct 26 '17 at 07:18
  • Hello, thanks for your answers. It helps but I realized that I would better start a new process "cmd.exe" and write to it from my form. So I will post a new question because that'is not really the same thing. – JudgeDreed Oct 26 '17 at 08:20