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 6984403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:32:41+00:00 2026-05-27T18:32:41+00:00

I have a Form with two ListBoxes on them. I have removeFromBoxWaitin g that

  • 0

I have a Form with two ListBoxes on them. I have removeFromBoxWaiting that watches for creating of new files in a directory.
Once a file is created it prints it out and then it should add the name of the file into the list box.
My problem is that the list box does not get updated. Items actually have been added but they do not show and i have tried update() as well but it didn’t work. So any tips will be appreciated. Thanks in advance.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Threading;
using System.Configuration;

namespace PrntToKitchen
{
public partial class PrintFiles : DevExpress.XtraBars.Ribbon.RibbonForm
{
    FileSystemWatcher filesWatcher = new FileSystemWatcher();

    public void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
    {          
        MessageBox.Show(e.Item.Name);
        if (Properties.Settings.Default[e.Item.Name + "Read"].ToString().Length > 1 && Properties.Settings.Default[e.Item.Name + "ExtIn"].ToString().Length > 1)
        {
            filesWatcher.Path = Properties.Settings.Default[e.Item.Name + "Read"].ToString();
            filesWatcher.Created += new System.IO.FileSystemEventHandler(eventG);
            //filesWatcher.Deleted += new System.IO.FileSystemEventHandler(addToBoxFinished);
            filesWatcher.EnableRaisingEvents = true;                    
        }
    }

    public void eventG(object sender, FileSystemEventArgs e)
    {
        string CurrentPrinter = "";
        string findPrt = e.FullPath.Substring(0, e.FullPath.Length-(e.Name.Length + 1));
        string findPrtExt = e.Name.Substring(e.Name.LastIndexOf("."));

        for (int i = 1; i <= Properties.Settings.Default.NumberOfPrinters; i++)
        {
            string testPrt = Properties.Settings.Default["Printer" + i + "Read"].ToString();
            string testExt = Properties.Settings.Default["Printer" + i + "ExtIn"].ToString();

            if (testPrt == findPrt && testExt == findPrtExt)
            {
                CurrentPrinter = "Printer" + i.ToString();
                POSPrinter printer = new POSPrinter(Properties.Settings.Default["Printer" + i + "Port"].ToString(), (int)Properties.Settings.Default["Printer" + i + "Speed"]);
                addToBoxWaiting(e.FullPath.ToString());
                string file = e.FullPath;
                printer.BeginPrint();
                printer.PrintFile(file);
                printer.EndPrint();
                printer.Dispose();
                if ((bool)Properties.Settings.Default[CurrentPrinter + "Delete"])
                {
                    IsFileLocked(file);
                    System.IO.File.Delete(file);
                }
                else
                {
                    System.IO.File.Move(file, Properties.Settings.Default[CurrentPrinter + "Store"] + "\\" + e.Name + Properties.Settings.Default[CurrentPrinter + "ExtOut"]);
                }
                removeFromBoxWaiting(e.FullPath.ToString());
                addToBoxFinished(e.FullPath.ToString());
                busy = false;
                break;
            }
        }
    }

    void addToBoxWaiting(string text)
    {
        listBox1.Items.Add(text);

    }

    void removeFromBoxWaiting(string text)
    {
        listBox1.Items.Remove(text);
    }

    public void addToBoxFinished(string destination)
    {
       listBox2.Items.Add(destination);

    }

}

}

  • 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-27T18:32:42+00:00Added an answer on May 27, 2026 at 6:32 pm

    I believe your problem is that you are trying to add/remove listbox items while iterating through a loop (and both this actions are happening on UI thread). You should move your for loop into a separate thread, and your addToBoxWaiting method will then look like this:

    private void AddToListBox(string item)
    {
        MethodInvoker del = delegate
        {
            listBox1.Items.Add(item);
        };
        BeginInvoke(del);
    }
    

    Edit. Added thread code.

        public void eventG(object sender, FileSystemEventArgs e)
        {
            Thread eventThread = new Thread(ThreadProcEventG);
            eventThread.Start(e);
        }
    
        private void ThreadProcEventG(object eventArgs)
        {
            var e = (FileSystemEventArgs)eventArgs;
            string CurrentPrinter = "";
            string findPrt = e.FullPath.Substring(0, e.FullPath.Length-(e.Name.Length + 1));
            string findPrtExt = e.Name.Substring(e.Name.LastIndexOf("."));
    
            for (int i = 1; i <= Properties.Settings.Default.NumberOfPrinters; i++)
            {
                string testPrt = Properties.Settings.Default["Printer" + i + "Read"].ToString();
                string testExt = Properties.Settings.Default["Printer" + i + "ExtIn"].ToString();
    
                if (testPrt == findPrt && testExt == findPrtExt)
                {
                    CurrentPrinter = "Printer" + i.ToString();
                    POSPrinter printer = new POSPrinter(Properties.Settings.Default["Printer" + i + "Port"].ToString(), (int)Properties.Settings.Default["Printer" + i + "Speed"]);
                    addToBoxWaiting(e.FullPath.ToString());
                    string file = e.FullPath;
                    printer.BeginPrint();
                    printer.PrintFile(file);
                    printer.EndPrint();
                    printer.Dispose();
                    if ((bool)Properties.Settings.Default[CurrentPrinter + "Delete"])
                    {
                        IsFileLocked(file);
                        System.IO.File.Delete(file);
                    }
                    else
                    {
                        System.IO.File.Move(file, Properties.Settings.Default[CurrentPrinter + "Store"] + "\\" + e.Name + Properties.Settings.Default[CurrentPrinter + "ExtOut"]);
                    }
                    removeFromBoxWaiting(e.FullPath.ToString());
                    addToBoxFinished(e.FullPath.ToString());
                    busy = false;
                    break;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has two buttons on it, one yes, one no,
I have a Windows Form and a class with two simple methods that run
I have a form with a dropdownlist, two buttons, and two Listboxes inside an
I have two form. One will display records form a RecordSet that the other
I have a form with two select boxes that are used to move values
I have a form with two places that use ajax to submit the information
I have a form with two text boxes, one select drop down and one
I have a form with two text_fields: <input type=text id=post_name name=post[name] /> <input type=text
I have a login form for my website. This login form have two text
I have form with one input for email and two submit buttons to subscribe

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.