Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. update listbox??

update listbox??

Scheduled Pinned Locked Moved Managed C++/CLI
questiongraphicsalgorithmshelpannouncement
16 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Thilek

    hi guys, i am writing a directory scanning program.. my program will scan the entire directory and list down the file names. i am using a listbox to show the filenames. Currently the program will do the entire searching then will list down the file names after complete. the program should show the filenames one by one while searching the directory. how can i do that guys..help me plz.. Below is my coding :-

    wstring directory =(L"C:\\Windows\\system32");

    if (ListFiles(directory, L"\*", files)) {
        for (vector<wstring>::iterator it = files.begin(); 
             it != files.end(); 
             ++it) {
    
    		std::string filename  = WStringToString(it->c\_str());;
    		 std::string s=filename;
    
              String ^someString= gcnew String(s.c\_str());
    
    
    		  listBox1->Items->Add(String::Concat(someString));
    
    		 
        }
    }
    

    listBox1->EndUpdate();

    Regards, Thilek

    N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #3

    Thilek wrote:

    i am writing a directory scanning program..

    Since you are using C++/CLI, I don't see any need of using standard C++ for directory search. There is Directory::GetDirectories() method which will help you on this. Read this[^] article which talks about how to do it recursively.

    Thilek wrote:

    the program should show the filenames one by one while searching the directory.

    Thilek wrote:

    listBox1->EndUpdate();

    Problem here is you are calling EndUpdate only after finishing the loop. So control is redrawing only after completing the full loop. You need to change this so that control will draw immediatly once the item is added. :)

    Navaneeth How to use google | Ask smart questions

    1 Reply Last reply
    0
    • M Mark Salsbery

      Maybe try updating the control after each item is added, something like:

      listBox1->Items->Add(String::Concat(someString));
      listBox1->SelectedIndex = index;
      listBox1->Update();
      

      I'm curious - why all the string conversions in there? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      T Offline
      T Offline
      Thilek
      wrote on last edited by
      #4

      what should i replace the index with.. when i compile it its showing :-

      error C2065: 'index' : undeclared identifier

      Help me plz and thanks ya.

      M 2 Replies Last reply
      0
      • T Thilek

        what should i replace the index with.. when i compile it its showing :-

        error C2065: 'index' : undeclared identifier

        Help me plz and thanks ya.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #5

        Oops , sorry...

        int index = listBox1->Items->Add(String::Concat(someString));
        listBox1->SelectedIndex = index;
        listBox1->Update();
        

        listBox1->SelectedIndex = index; is optional - without it the added items won't automatically scroll into view. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        T 1 Reply Last reply
        0
        • T Thilek

          what should i replace the index with.. when i compile it its showing :-

          error C2065: 'index' : undeclared identifier

          Help me plz and thanks ya.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #6

          I'm not sure what you were doing with those string conversions, but if you don't need them, this should work:

          wstring directory =(L"C:\\Windows\\system32");

          if (ListFiles(directory, L"*", files))
          {
          for (vector<wstring>::iterator it = files.begin(); it != files.end(); ++it)
          {
          int index = listBox1->Items->Add(String::Concat(gcnew String(it->c_str())));
          listBox1->SelectedIndex = index;
          listBox1->Update();
          }
          }

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          T 1 Reply Last reply
          0
          • M Mark Salsbery

            Oops , sorry...

            int index = listBox1->Items->Add(String::Concat(someString));
            listBox1->SelectedIndex = index;
            listBox1->Update();
            

            listBox1->SelectedIndex = index; is optional - without it the added items won't automatically scroll into view. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            T Offline
            T Offline
            Thilek
            wrote on last edited by
            #7

            its scrolling down.. i notice on the scroll bar, its blinking. but the list only visible after its finish scanning :P Do i need to do anything to the form or any other options i need to add ??

            M 1 Reply Last reply
            0
            • T Thilek

              its scrolling down.. i notice on the scroll bar, its blinking. but the list only visible after its finish scanning :P Do i need to do anything to the form or any other options i need to add ??

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #8

              hmm it worked for me. I'm assuming you're using Windows Forms for UI, and your code is running on the UI thread... Maybe try an Application::DoEvents(); call after (or instead of) the Update() call.

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              1 Reply Last reply
              0
              • M Mark Salsbery

                I'm not sure what you were doing with those string conversions, but if you don't need them, this should work:

                wstring directory =(L"C:\\Windows\\system32");

                if (ListFiles(directory, L"*", files))
                {
                for (vector<wstring>::iterator it = files.begin(); it != files.end(); ++it)
                {
                int index = listBox1->Items->Add(String::Concat(gcnew String(it->c_str())));
                listBox1->SelectedIndex = index;
                listBox1->Update();
                }
                }

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                T Offline
                T Offline
                Thilek
                wrote on last edited by
                #9

                well i were collecting the codes there and here, so to match them i keep converting but ur code made it look short and nice :) thanks ya. but the listbox showing the list after complete scanning..

                M 1 Reply Last reply
                0
                • T Thilek

                  well i were collecting the codes there and here, so to match them i keep converting but ur code made it look short and nice :) thanks ya. but the listbox showing the list after complete scanning..

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #10

                  Here's the code I'm testing with - I can see all the items get added...

                          for (int i = 0; i < 1000; i++)
                          {
                              int index = listBox1->Items->Add(i.ToString());
                              listBox1->SelectedIndex = index;
                              listBox1->Update();
                              //Application::DoEvents();
                          }
                  

                  Using the Update() or the DoEvents() method yields the same results. Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  T 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Here's the code I'm testing with - I can see all the items get added...

                            for (int i = 0; i < 1000; i++)
                            {
                                int index = listBox1->Items->Add(i.ToString());
                                listBox1->SelectedIndex = index;
                                listBox1->Update();
                                //Application::DoEvents();
                            }
                    

                    Using the Update() or the DoEvents() method yields the same results. Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    T Offline
                    T Offline
                    Thilek
                    wrote on last edited by
                    #11

                    below is the full coding of my form :-

                    #pragma once

                    #include "Mainmenu.h"
                    #include "Scanner.cpp"

                    using namespace std;
                    using namespace System;
                    using namespace System::ComponentModel;
                    using namespace System::Collections;
                    using namespace System::Windows::Forms;
                    using namespace System::Data;
                    using namespace System::Drawing;

                    namespace trialcpp {

                    /// <summary>
                    /// Summary for Autoscanner
                    ///
                    /// WARNING: If you change the name of this class, you will need to change the
                    ///          'Resource File Name' property for the managed resource compiler tool
                    ///          associated with all .resx files this class depends on.  Otherwise,
                    ///          the designers will not be able to interact properly with localized
                    ///          resources associated with this form.
                    /// </summary>
                    public ref class Autoscanner : public System::Windows::Forms::Form
                    {
                    public:
                    	Autoscanner(void)
                    	{
                    		InitializeComponent();
                    		//
                    		//TODO: Add the constructor code here
                    		//
                    	}
                    
                    protected:
                    	/// <summary>
                    	/// Clean up any resources being used.
                    	/// </summary>
                    	~Autoscanner()
                    	{
                    		if (components)
                    		{
                    			delete components;
                    		}
                    	}
                    private: System::Windows::Forms::Label^  label1;
                    
                    private: System::Windows::Forms::Button^  start\_button;
                    private: System::Windows::Forms::Button^  close\_button;
                    protected: 
                    
                    private:
                    	/// <summary>
                    	/// Required designer variable.
                    	/// </summary>
                    	System::ComponentModel::Container ^components;
                    

                    #pragma region Windows Form Designer generated code
                    /// <summary>
                    /// Required method for Designer support - do not modify
                    /// the contents of this method with the code editor.
                    /// </summary>
                    void InitializeComponent(void)
                    {
                    this->label1 = (gcnew System::Windows::Forms::Label());
                    this->start_button = (gcnew System::Windows::Forms::Button());
                    this->close_button = (gcnew System::Windows::Forms::Button());
                    this->SuspendLayout();
                    //
                    // label1
                    //
                    this->label1->AutoSize = true;
                    this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
                    static_castSystem::Byte(0)));
                    this->label1->Location = System::Drawing::Point(22, 22);
                    this->label1->Name = L"label1";
                    this->label1->Size = System::Drawing::Size(176, 16);
                    this->label1->TabIndex = 0;
                    this->label1->Text = L"S

                    M 1 Reply Last reply
                    0
                    • T Thilek

                      below is the full coding of my form :-

                      #pragma once

                      #include "Mainmenu.h"
                      #include "Scanner.cpp"

                      using namespace std;
                      using namespace System;
                      using namespace System::ComponentModel;
                      using namespace System::Collections;
                      using namespace System::Windows::Forms;
                      using namespace System::Data;
                      using namespace System::Drawing;

                      namespace trialcpp {

                      /// <summary>
                      /// Summary for Autoscanner
                      ///
                      /// WARNING: If you change the name of this class, you will need to change the
                      ///          'Resource File Name' property for the managed resource compiler tool
                      ///          associated with all .resx files this class depends on.  Otherwise,
                      ///          the designers will not be able to interact properly with localized
                      ///          resources associated with this form.
                      /// </summary>
                      public ref class Autoscanner : public System::Windows::Forms::Form
                      {
                      public:
                      	Autoscanner(void)
                      	{
                      		InitializeComponent();
                      		//
                      		//TODO: Add the constructor code here
                      		//
                      	}
                      
                      protected:
                      	/// <summary>
                      	/// Clean up any resources being used.
                      	/// </summary>
                      	~Autoscanner()
                      	{
                      		if (components)
                      		{
                      			delete components;
                      		}
                      	}
                      private: System::Windows::Forms::Label^  label1;
                      
                      private: System::Windows::Forms::Button^  start\_button;
                      private: System::Windows::Forms::Button^  close\_button;
                      protected: 
                      
                      private:
                      	/// <summary>
                      	/// Required designer variable.
                      	/// </summary>
                      	System::ComponentModel::Container ^components;
                      

                      #pragma region Windows Form Designer generated code
                      /// <summary>
                      /// Required method for Designer support - do not modify
                      /// the contents of this method with the code editor.
                      /// </summary>
                      void InitializeComponent(void)
                      {
                      this->label1 = (gcnew System::Windows::Forms::Label());
                      this->start_button = (gcnew System::Windows::Forms::Button());
                      this->close_button = (gcnew System::Windows::Forms::Button());
                      this->SuspendLayout();
                      //
                      // label1
                      //
                      this->label1->AutoSize = true;
                      this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
                      static_castSystem::Byte(0)));
                      this->label1->Location = System::Drawing::Point(22, 22);
                      this->label1->Name = L"label1";
                      this->label1->Size = System::Drawing::Size(176, 16);
                      this->label1->TabIndex = 0;
                      this->label1->Text = L"S

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #12

                      You need to get rid of the BeginUpdate() and EndUpdate() calls. Those are for when you DON'T want to see the listbox update until all items have been added. Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      T 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        You need to get rid of the BeginUpdate() and EndUpdate() calls. Those are for when you DON'T want to see the listbox update until all items have been added. Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        T Offline
                        T Offline
                        Thilek
                        wrote on last edited by
                        #13

                        thanks ya.. its working now... but jus that every file is selected.. but its ok for now.. thanks a lot..

                        M 1 Reply Last reply
                        0
                        • T Thilek

                          thanks ya.. its working now... but jus that every file is selected.. but its ok for now.. thanks a lot..

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #14

                          Thilek wrote:

                          every file is selected

                          You can handle selection the way you want....I was just using a single-selection control to test. :)

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          T 1 Reply Last reply
                          0
                          • M Mark Salsbery

                            Thilek wrote:

                            every file is selected

                            You can handle selection the way you want....I was just using a single-selection control to test. :)

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            T Offline
                            T Offline
                            Thilek
                            wrote on last edited by
                            #15

                            okies thanks ya... i try to add string matching function to it but the program exit with error msg :- Managed' has exited with code -1073741819 (0xc0000005). But it works if i scan directory will less files (about 160 files). hm... confusing....

                            T 1 Reply Last reply
                            0
                            • T Thilek

                              okies thanks ya... i try to add string matching function to it but the program exit with error msg :- Managed' has exited with code -1073741819 (0xc0000005). But it works if i scan directory will less files (about 160 files). hm... confusing....

                              T Offline
                              T Offline
                              Thilek
                              wrote on last edited by
                              #16

                              one more problem bro.. when i run the same program on my frieds laptop its n0t listing the file names and no error found... wierd :^)

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups