2012-02-02 87 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