如何在WinForm中触发事件时添加控件

本文关键字:事件 添加 控件 WinForm | 更新日期: 2023-09-27 18:22:07

我喜欢在form1中添加控件,其中观察者。Changed+=新的FileSystemEventHandler(OnChanged);是否可以将控件(例如列表框)添加到表单1中,但需要添加到定义的事件处理程序中

  /*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);

如何在WinForm中触发事件时添加控件

这就是您想要的。根据您的评论,您可能没有设置位置和大小,因此添加控件可能不起作用。但你真的应该确保对此进行监管,并确保你只是在你想要的时候添加控件,而不是更多。

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