C#:如何将文本附加到类中的表单上的文本框? [英] C#: How can I append text to a textbox on a form from a class?

查看:25
本文介绍了C#:如何将文本附加到类中的表单上的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Form1.cs"的表单,它调用了一个我们称之为Class1.cs"的类,以及另一个名为Form2.cs"的表单.如果窗体处于打开状态,Class1 中的子例程需要更新 Form2 中的文本框.文本需要在附加到文本框中的当前文本后出现,以便实时更新.我不知道如何进行这项工作.我尝试了很多东西,他们没有给我错误,但他们也没有将文本写入文本框.

I have a form called "Form1.cs", which calls a class which we will refer to as "Class1.cs", as well as another form called "Form2.cs". A subroutine in Class1 needs to update a textbox in Form2 if that form is open. The text needs to appear after it is appended to the current text in the textbox so that is updates in real time. I can't figure out how to make this work. I have tried many things and they don't give me errors but they don't write the text into the textbox either.

这里的每个请求是我当前的代码.请记住,这是一个测试项目,用于在将其实施到真正的项目中之前弄清楚这一点.

Per request here is my current code. Keep in mind that this is the test project for figuring this out before implementing it into the real one.

Form1.cs 中的代码

Code in Form1.cs

namespace Test
{
    public partial class Form1 : Form
    {
        Form2 cs_form2 = new Form2();
        Class1 cs_class1 = new Class1();
        public Form1()
        {
            InitializeComponent();

        }
        public void button1_Click(object sender, EventArgs e)
        {
            cs_class1.Writelog();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cs_form2.Show();
        }
        public void writeToTextbox()
            {
                i = 0;
                while(i<=10)
                {
                    cs_form2.testTextBox.AppendText("still works");
                    i++;
                }
            }
    }
}

Form2.cs 中的代码

Code in Form2.cs

namespace Test
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void clear_Click(object sender, EventArgs e)
        {

            testTextBox.Text = "";
        }

        public void AppendText()
        {
            testTextBox.AppendText("asklvhslieh");
        }
    }
}

Class1 中的代码

Code in Class1

namespace Test
{
    class Class1
    {
        Form2 cs_form2 = new Form2();

        public void Writelog()
        {
            cs_form2.testTextBox.AppendText("asg");
        }
    }
}

推荐答案

EDIT:通过编写new Form2(),你的代码在Class1> 正在创建 Form2 的新实例.
该实例与在 Form1(也通过编写 new Form2())中创建的另一个实例没有任何关系,它实际上是可见的.
您需要为 Class1 提供 Form2 的现有实例,可能使用静态属性(如下所述).

EDIT: By writing new Form2(), your code in Class1 is creating a new instance of Form2.
This instance does not have anything to do with the other instance created in Form1 (also by writing new Form2()) which is actually visible.
You need to give Class1 the existing instance of Form2, perhaps using a static property (as described below).

要将文本附加到文本框,您应该调用 AppendText 方法.

To append text to the textbox, you should call the AppendText method.

要在 Form2 之外执行此操作,您应该在 Form2 上创建一个调用 AppendTextpublic 方法.

To do that outside of Form2, you should make a public method on Form2 that calls AppendText.

例如:

partial class Form2 : Form {
    ...
    public void AppendMyText(string text) {
        myTextbox.AppendText(text);
    }
    ...
}

要在 Class1 中调用此方法,您需要对 Form2 对象的引用.
如果你一次只有一个Form2,你可以做一个静态属性,像这样:

To call this method in Class1, you'll need a reference to a Form2 object.
If you only have one Form2 at a time, you can make a static property, like this:

partial class Form2 : Form {
    static Form2 instance;
    public static Form2 Instance { get { return instance; } }

    protected override void OnShown(EventArgs e) {
        base.OnShown(e);
        instance = this;
    }
    
    protected override void OnClosed(EventArgs e) {
        base.OnClosed(e);
        instance = null;
    }

Class1(或其他任何地方),你可以写

In Class1 (or anywhere else), you can then write

if (Form2.Instance != null)
    Form2.Instance.AppendMyText(someString);

请注意,您需要在 UI 线程上执行此操作;如果您在后台线程上运行,则可以调用 BeginInvoke.

Note that you need to do this on the UI thread; if you're running on a background thread, you can call BeginInvoke.

这篇关于C#:如何将文本附加到类中的表单上的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆