简体   繁体   English

为什么不能在主程序中与表单对象进行交互?

[英]Why can't I interact with form objects from within the main program?

This should be pretty straightforward, yet I am struggling to see any results. 这应该很简单,但是我正在努力查看任何结果。

I have the following form code: 我有以下表单代码:

//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        protected TextBox textReadInfo;
        public void SetReadInfo(String str)
        {
            txtReadInfo.Text = str;
        }
    }
}

I also have the following program code: 我也有以下程序代码:

//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NotepadRW
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form1 = new Form1();
            Application.Run(form1);

            form1.SetReadInfo("Hi");

        }
    }
}

Which result in the following form: 结果为以下形式:

空白表格

Why is it not displaying the string I provide it? 为什么不显示我提供的字符串? Am I not properly understanding how the program works with a windows form? 我是否不正确理解该程序如何与Windows窗体一起使用? Does execution of the Main() method of Program.cs stop when the new instance of Form1 is called? 调用Form1的新实例时,Program.cs的Main()方法的执行是否停止?

Note: I've tried this with a text box and a rich text box. 注意:我已经尝试过使用文本框和富文本框。 Same results. 结果相同。

Extra points: Am I following encapsulation correctly? 加分:我是否正确遵循封装? I was going to modify the text box to be publicly accessible (which will work), but I thought this would be the "correct" way to do it. 我打算将文本框修改为可公开访问(将起作用),但是我认为这将是“正确”的方法。

While it is not obvious from the documentation , the Application.Run(Form) method blocks. 虽然从文档中看不出来,但是Application.Run(Form)方法将阻止。 You can infer this from the following quote: 您可以从以下引用中推断出这一点:

The Dispose method of the Form class will be called prior to the return of this method. 在返回该方法之前,将调用Form类的Dispose方法。

This means that the form will do its thing and it has to be done doing its thing before Run will return. 这意味着表单将执行其操作,并且必须在返回Run之前完成其操作。 Ie, it blocks. 即,它阻止了。

You can accomplish what you want by switching up the order though. 您可以通过切换顺序来完成所需的操作。 Instead of 代替

Form1 form1 = new Form1();
Application.Run(form1);
form1.SetReadInfo("Hi");

try 尝试

Form1 form1 = new Form1();
form1.SetReadInfo("Hi");
Application.Run(form1);

You shouldn't be making your text box public, otherwise you may get cross thread execution exception. 您不应该公开您的文本框,否则您可能会遇到跨线程执行异常。

The reason your code is not working is because you are running the form on the main thread, so anything after Application.Run() does not get executed. 您的代码无法正常工作的原因是因为您正在主线程上运行表单,因此Application.Run()之后的所有内容都不会执行。 You can run your form on a separate thread. 您可以在单独的线程上运行表单。

Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form = new Form1();

            System.Threading.Thread workerThread = new System.Threading.Thread(() => Application.Run(form));

            workerThread.Start();

            form.SetReadInfo("Hello");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM