无法将项目从另一个类添加到列表框 [英] Can't add items to listbox from another class

查看:81
本文介绍了无法将项目从另一个类添加到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我正在尝试从另一个类向列表框添加项目,信息传递给该功能,但列表框似乎没有更新.这是我的代码:

主类(FORM):

Hi!

I''m trying to add items to listbox from another class,the information pass to the funtion but the listbox dosen''t seem to update. this is my code:

Main class (FORM) :

public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            
        }
// the function that updates the listbox
public void logURI(string OutputLog, string Information, string JOB)
        {
            try
            {
                listBox1.BeginUpdate();
                listBox1.Items.Insert(0, DateTime.Now.ToString() + " : " + JOB + " " + Information);
                listBox1.Items.Add("1");
                listBox1.EndUpdate();
                textBox1.Text = JOB;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

}



二等舱:



Second Class:

 public class FtpFileSystemWatcherTS
    {
 Form1 logs = new Form1();
logs.logURI( "", "Found folder modefied today (" + FileName.TrimEnd(), ") ElectricaTS"); 



有人可以告诉我我做错了吗?



Can someone tell me wath I''m doing wrong ?
It''s urgent!

推荐答案

亲爱的Mitran,

当您在Form1中调用函数以填充列表框时,但是由于类不同并且ListBox1位于一种表单中,因此您需要将Listbox作为参数暂停到函数中,以便相应地填充它.

Dear Mitran,

As you are calling the function in Form1 in order to fill the list box but as the classes are different and the ListBox1 is located in one form hen you need to paas the Listbox as the parameter into the function in order to get it filled accordingly.

logs.logURI( "", "Found folder modefied today (" + FileName.TrimEnd(), ") ElectricaTS",listbox1);



我希望你能理解你要去哪里.

谢谢



I hope yo understand where you are going wrong.

Thanks


这篇关于无法将项目从另一个类添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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