2012-02-02 88 views
-1

我喜歡在form1中添加控件,其中一個事件處理程序爲 watcher.Changed + = new FileSystemEventHandler(OnChanged);定義,是有可能例如列表框控件添加到Form1,但需要定義它如何在WinForm中觸發事件時添加控件?

/*event added*/ 

    private void btn_start_Click(object sender, EventArgs e) 
    { 
     string[] args = {this.txtfolder.Text}; 
     if (args.Length != 1) 
     { 
      // Display the proper way to call the program. 
      Console.WriteLine("Usage: Invalid Operation"); 
      return; 
     } 

     FileSystemWatcher watcher = new FileSystemWatcher(); 
     watcher.Path = args[0]; 
     /* Watch for changes in LastAccess and LastWrite times, and 
      the renaming of files or directories. */ 
     watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
      | NotifyFilters.FileName | NotifyFilters.DirectoryName; 
     // Only watch text files. 
     watcher.Filter = this.txtfilter.Text;//"*.txt"; 

     // Add event handlers. 
     watcher.Changed += new FileSystemEventHandler(OnChanged); 
     watcher.Created += new FileSystemEventHandler(OnChanged); 
     // watcher.Deleted += new FileSystemEventHandler(OnChanged); 
     watcher.Renamed += new RenamedEventHandler(OnRenamed); 



    // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e) 
    { 
     // Specify what is done when a file is changed, created, or deleted. 
     //Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); 

     // Form1 F ;   
     // ListBox lst = new ListBox(); 
     //lst.Items.Add("File: " + e.FullPath + " " + e.ChangeType.ToString()); 
     //f.lsttracker.Items.Add("File: " + e.FullPath + " " + e.ChangeType.ToString()); 
     // F.controls.Add(lst); 
+2

是的,這是可能的。但每次事件發生時都會添加,這可能不是您想要的。很難說出你的代碼是在展示什麼。爲什麼所有這些東西都被註釋掉了? – 2012-02-02 04:22:33

+0

你試過了嗎? (注意:是否有可能通常最終被降低的問題)。 – 2012-02-02 04:24:41

+0

@selvannand嘗試此示例。我認爲這是做你正在做的事情:dl.dropbox.com/u/18919663/vs%20samples/TestingSandbox.zip – Corylulu 2012-02-02 20:48:05

回答

1

這是你找什麼事件處理中被添加。從您註釋的內容來看,您可能沒有設置位置和大小,因此添加控件可能不起作用。但是你應該確保調整這一點,並確保你只是在你想要的時候才加入控件,而不是更多。

private static void OnChanged(object source, FileSystemEventArgs e) 
{ 
    ListBox toAdd = new ListBox(); 
    toAdd.Location = new Point(20,20); 
    toAdd.Size = new Size(200,200); 
    this.Controls.Add(toAdd); 
} 

如果你想保存你添加的控件,嘗試這樣的事情:

private List<Control> AddedItems = new List<Controls>(); 
private int OffsetY = 0; 
private static void OnChanged(object source, FileSystemEventArgs e) 
{ 
    ListBox toAdd = new ListBox(); 
    if(AddedItem.Last().Point.Y == OffsetY) // just an example of reusing previously added items. 
    { 
     toAdd.Location = new Point(20, OffsetY); 
     toAdd.Size = new Size(200,200); 
     AddedItems.Add(toAdd); 
     this.Controls.Add(toAdd); 
    } 
    OffsetY += 200; 
} 

編輯:在回答你在下面的評論中提到的內容。

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] 
    private void btn_start_Click(object sender, EventArgs e) 
    { 
     string FolderPath = this.txtfolder.Text; 
     string Filter = this.txtfilter.Text; 
     if(!Directory.Exists(FolderPath)) 
     { 
      Console.WriteLine("Not a valid directory"); //checks directory is valid 
      return; 
     } 

     FileSystemWatcher watcher = new FileSystemWatcher(); 

     watcher.Path = FolderPath; 
     watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
           | NotifyFilters.FileName | NotifyFilters.DirectoryName; 
     // Only watch filter files. 
     watcher.Filter = Filter; 

     watcher.IncludeSubdirectories = true; //monitor subdirectories? 
     watcher.EnableRaisingEvents = true; //allows for changed events to be fired. 

     // Add event handlers. 
     watcher.Changed += new FileSystemEventHandler(OnChanged); 
     watcher.Created += new FileSystemEventHandler(OnChanged); 
    } 
    //Delegate to get back to UI thread since OnChanged fires on non-UI thread. 
    private delegate void updateListbox(string context); 
    private void OnChanged(object source, FileSystemEventArgs e) 
    { 
     this.Invoke(new updateListbox(UpdateListbox), "File: " + e.Name); 
     this.Invoke(new updateListbox(UpdateListbox), ">>Action: " + e.ChangeType); 
     this.Invoke(new updateListbox(UpdateListbox), ">>Path: " + e.FullPath); 
    } 
    public void UpdateListbox(string context) 
    { 
     lsttracker.Items.Add(context); 
    } 
+0

嗨,感謝您的解釋,但實際上我需要將值添加到名爲lsttracker的列表框到form1 ,當事件處理程序觸發時,它將執行下面給出的東西,如下所示 – selvaanand 2012-02-02 18:13:50

+0

HI,感謝您的解釋,但實際上我需要將值添加到名爲lsttracker的列表框到form1,當事件處理程序觸發它時,將執行以下操作在下面給出private static void OnChanged(object source,FileSystemEventArgs e) {指定在更改,創建或刪除文件時完成的操作。 File.Delete(e.FullPath); //在此之後,我需要將值或文件路徑刪除到名爲lsttracker的form1 } //這是我需要的,請幫助我,如果有人知道 – selvaanand 2012-02-02 18:21:23

+0

請查看我的編輯。我認爲這就是你要找的。 :] 因爲您將OnChange事件處理程序設爲靜態,您可能會遇到問題,因爲它不會讓您引用非靜態表單元素。 – Corylulu 2012-02-02 19:26:04