2012-01-06 106 views
1

我想添加項目到另一個類別的列表框,信息傳遞給函數,但列表框似乎並沒有更新。這是我的代碼:不能添加項目到另一個類別的列表框#

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); 
     } 
    } 
} 

二等:

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

我在做什麼錯?

+0

請提供關於你的第二個部分的詳細信息類和它對錶單的使用。您是在展示您在FtpFileSystemWatcherTS類中創建的表單還是存在多個表單實例? – MatthiasG 2012-01-06 15:15:59

+0

textBox1更新嗎?如果您從Visual Studio調試器運行代碼,是否在輸出窗口中看到任何錯誤消息? – dgvid 2012-01-06 15:17:24

+0

由於只是將2個項目添加到列表框中,因此您無需調用BeginUpdate和EndUpdate。 – evasilchenko 2012-01-06 15:20:08

回答

2

您正在創建其他課程中的Form - 您爲Form的孩子所做的任何更改都將不會顯示,因爲它是另一種顯示方式。相反,您要麼將Form實例運行到FtpFileSystemWatcher類中,以便它可以訪問Form.Controls屬性,或者讓其直接訪問ListBoxListBox項目的源。

編輯

建議:

public partial class Form1 : Form 
{ 
    private FtpFileSystemWatcher mWatcher; 

    // ... some code ... 

    public Form1() 
    { 
     InitializeComponent(); 

     // Create a new watcher and give it access to this form 
     mWatcher = new FtpFileSystemWatcher(this); 
    } 

    // ... Logging code ... 
} 

public class FtpFileSystemWatcher 
{ 
    private Form1 mMainForm; 

    public FtpFileSystemWatcher(Form1 mainForm) 
    { 
     mMainForm = mainForm; 
    } 

    public void Log() 
    { 
     mMainForm.logUri(...); 
    } 
} 

這僅僅是你可以用它來給FtpFileSystemWatcher訪問正在運行的Form一些代碼格式的例子。這將在運行Form時設置(假設您已正確運行)。你應該看到你想要的更新。

+0

我確實將「修改器」列表框設置爲公開,還是以其他方式直接訪問列表框? – XandrUu 2012-01-06 15:24:21

+0

這應該讓它直接訪問,但這不是問題。這個例子中的問題(從我可以看到的)是,你正試圖更新一個不是'Form'運行的'Form'的值。 – 2012-01-06 15:46:42

+0

,我該怎麼做? – XandrUu 2012-01-06 15:51:56

0

您可以使用繼承很容易,因爲程序的訪問修飾符設置爲公共

主類(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.Add("0"); 
      listBox1.Items[0] = DateTime.Now.ToString() + " : " + JOB + " " + Information; 
      listBox1.Items.Add("1"); 
      listBox1.EndUpdate(); 
      textBox1.Text = JOB; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

}

public class FtpFileSystemWatcherTS : Form1 
{ 
    logURI("", "Found folder modefied today (" + FileName.TrimEnd().toString(), ") ElectricaTS"); 
} 
+0

好吧,我是一個noob,你能告訴我如何使用繼承? – XandrUu 2012-01-06 15:29:30

+0

僅僅使用 :Form1 在第二個類的名字後面,所以你可以訪問它的父類的方法和屬性。當你喜歡你的方式時,記住它實際上並不是真正的listbox而是它的副本。 – user1120193 2012-01-06 15:34:04

+0

還有另一種方法,您可以將列表框作爲參數傳遞給第二個類並添加第二個類中的項。所以它會實際更新真實的。 – user1120193 2012-01-06 15:35:32

相關問題