Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7248593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:09:34+00:00 2026-05-28T22:09:34+00:00

i like to add controls inside the form1 where a event handler for watcher.Changed

  • 0

i like to add controls inside the form1 where a event handler for
watcher.Changed += new FileSystemEventHandler(OnChanged); is defined , is it possible to add control for example list box to the form1 but need to be added inside the event handler where it is defined

  /*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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:09:35+00:00Added an answer on May 28, 2026 at 10:09 pm

    This is what your looking for. From what you commented out, you probably didn’t set the location and size, thus adding the control probably wasn’t working. But you should really make sure to regulate this and make sure you are only adding controls exactly when you want to and no more.

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

    If you want to store the controls you added, try something like this:

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

    EDIT: In reply to what you mentioned in the comment below.

        [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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to add custom controls to the mediaPlayer control view much as this
To a WinForms control, I would like to add a handler after the container
I add a select control to a ASPX page like below: hgc = new
I would like to add graphing to my User Controls in ASP.NET MVC. I
I would like to add a DataGridViewTextBoxCell cell to a DataGridViewCell control, but as
To improve performances, I'd like to add a fairly long Cache-Control (up to 30
Using ASP.Net, I have a server control for which i would like to add
I have controls inside a UpdatePanel like this <asp:ScriptManager ID=ScriptManager1 runat=server></asp:ScriptManager> <asp:UpdatePanel ID=UpdatePanel1 runat=server>
I have a set of controls inside a WindowsFormsHost and I would like to
Looking for an example of how to dynamically add controls from the activity. Inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.