本文介绍了从其他班级更新用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个获胜表格,此表格上有一个按钮和一个列表框,现在我想从其他班级的列表中添加项目.为此的代码为

表单后面的代码

I have a win form there is a button and listbox on this form now i want to add item in list from other class. code for this given as

code behind form

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.updatelist();
        }
        public void UpdateL(string msg)
        {
            listBox1.Items.Add(msg);
        }
    }



而class1后面的代码是



and the code behind class1 is

class Class1
    {
        Form1 obj = new Form1();
        public void updatelist()
        {
            obj.UpdateL("This is updated from other class");
        }
    }



当我按下按钮但没有任何项目添加到列表框中时,所有函数都调用,这意味着未从其他类更新UI

有人可以帮我吗
谢谢!



all functions calls when i press button but no item added to listbox mean UI not updated from other class

can anybody help me
Thanks!

推荐答案

class Class1
    {
        Form1 obj = new Form1();
        public delegate void UpdateList(string item);
        public event UpdateList OnUpdateList;

        public void updatelist()
        {
          //Invoke the event
            if (OnUpdateList != null)
            {
                OnUpdateList("This is updated from other class");
            }
        }
    }



在Form1类中注册事件,您的代码将如下所示.




Register the event in the Form1 class, your code will look like this.


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();

           // Register event.
           obj.OnUpdateList += UpdateL;
           obj.updatelist();
        }


        public void UpdateL(string msg)
        {
            listBox1.Items.Add(msg);
        }
    }


Form1 obj = new Form1();


这将构造Form1对象的新实例,然后您可以对其进行修改.
由于它包含在类中,因此不会影响外部实例(包含class1实例).

没有图片很难解释,但我会尝试:

您想要做的是拥有一辆汽车(Form1).车内是杂物箱(Class1).当您打开杂物箱(通过button1_click事件)时,您将创建一辆新汽车并重新粉刷它.然后,您关闭手套箱,并期望原车具有不同的颜色.

查找有关C#的书.入门,并阅读有关实例的章节.您需要进一步详细了解这些内容,因为在您这样做之前,没有任何意义!

祝你好运.


This constructs a new instance of the Form1 object, which you then modify.
Since this is contained within the class, it does not affect the instance outside (which contains the class1 instance).

This is difficult to explain without pictures, but I''ll try:

What you are trying to do is have a car (Form1). Inside the car, is a glove box (Class1). When you open the glove box (via the button1_click event) you create a new car and repaint it. You then close the glove box, and expect the original car to be a different colour.

Find a book on C#. An elementary one, and read the chapter on instances. You need to understand this stuff in good detail before you go any further, as nothing will make sense until you do!

Good luck.


这篇关于从其他班级更新用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:24