将信息从类发送到文本框到表单 [英] Sending information to a textbox from a class to a form

查看:94
本文介绍了将信息从类发送到文本框到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我问了一个有关在类表单中填充列表框的问题.它有效并且很棒!但是,我现在想对文本框或标签执行相同的操作.该问题的答案如下:

I asked a question a couple of days ago about populating a listbox on a form from a class. It works and was great! However I now want to do the same with a textbox or a label. The answer from that question is below:

您正在做的是创建表单的新实例-我想 您要在现有表单的列表框中添加项目吗?

What you are doing is creating a new instance of the form - I presume you are trying to add items in a listbox on an existing form?

如果是这样的话.

在带有列表框的表单上创建一个函数,如:

Create a function on the form with the listbox like:

    public void addItemToListBox(string item)
    {
         listBox1.Items.Add(item);
    }

然后,在类中(记住要添加使用System.Windows.Forms 参考)

Then, in the class (remember to add the using System.Windows.Forms reference)

   public void doStuff()
    {
 //Change Form1 to whatever your form is called
 foreach (Form frm in Application.OpenForms)
 {
      if (frm.GetType() == typeof(Form1))
      {
           Form1 frmTemp = (Form1)frm;
           frmTemp.addItemToListBox("blah");

      }
 }
    }

这很好.现在,我想对文本框执行相同的操作.我想知道是否有人可以解释,或者有人对此有链接吗?

This works well. Now I want to do the same with a textbox. I was wondering if someone could explain or if someone has a link on this?

我有一个表格和一个课.表格构成了该类的新实例,并在该类中启动了一个方法,比如说数学方程式4 + 4;我想要答案"8"显示在类中的方法的form1上的文本框或标签中.

I have a form and a class. The form makes a new instance of the class and initiates a method in the class, say a maths equation 4+4; I want the answer, "8" to then be shown in a textbox or label on form1, from the method in the class.

推荐答案

通过编辑,您描述的是一个模型,您的Form 使用.正在使用的类应该不了解Form,控件或如何使用其计算结果.

What you are describing, with your edit, is a model in which your Form uses another class. The class that is being used should have no knowledge of the Form, it's controls, or how the result of its calculation could possibly be used.

您的其他类不应找到该表单,它不应调用该表单的任何方法或字段,或任何其他内容.它应该做的只是返回一个值:

Your other class shouldn't find the form, it shouldn't call any methods or fields of the form, or anything. What it should do is just return a value:

public class OtherClass
{
    public int Add(int first, int second)
    {
        return first + second;
    }
}

然后表单可以执行以下操作:

Then the form can do something like this:

private void button1_Click(object sender, EventArgs e)
{
    //create an instance of the other class
    OtherClass other = new OtherClass();
    //call it's add method, and then use the result to set the textbox's text
    textbox1.Text = other.Add(4, 4).ToString(); 
}

另一个类应该负责进行计算,创建结果,但不能基于这些结果来更新用户界面.所有的用户界面逻辑都应包含在Form类中.

The other class should be responsible for doing the calculations, creating a result, but not updating the user interface based on those results. All of the user interface logic should be contained in the Form class.

这篇关于将信息从类发送到文本框到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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