将Txt从Form写入控制台

时间:2017-10-26 06:18:04

标签: c# .net multithreading forms

我希望能够显示表单和控制台,并将表单中的文本写入控制台,但我无法做到。

首先,我尝试创建一个控制台程序并向其添加System.Windows.Forms以创建表单。它工作,但当我用鼠标在表格上它似乎冻结。

其次我认为这是因为表单是控制台线程的一部分,而控制台线程正在等待ReadKey表单冻结。所以我创建了一个backgroundworker来创建第二个帖子,我打开我的表单,但它也没有用。

所以这是我想念的东西,我请你帮忙。 到目前为止,这是我的代码:

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);
            }
        }
    }
}

我必须使所有静态'因为我从main开始。

你能帮帮我吗?

2 个答案:

答案 0 :(得分:2)

要通过Form显示Console,您需要先查看here

而且你不需要另一个主题。

示例代码

[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);
}

答案 1 :(得分:-1)

这不是最好的代码。但这是一种帮助您理解的方法: -

 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();
 }