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. Web Development
  3. ASP.NET
  4. Open files from listbox file listing in c#

Open files from listbox file listing in c#

Scheduled Pinned Locked Moved ASP.NET
csharphelpquestion
6 Posts 2 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.
  • M Offline
    M Offline
    Member 12016106
    wrote on last edited by
    #1

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
    FileInfo[] Files = dinfo.GetFiles("*.txt");
    foreach (FileInfo file in Files)
    {
    ListBox1.Items.Add(file.Name);
    }

    I have the source code but its not displaying anything in the listbox.. can anyone help me why is this so?

    W 1 Reply Last reply
    0
    • M Member 12016106

      protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
      DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
      FileInfo[] Files = dinfo.GetFiles("*.txt");
      foreach (FileInfo file in Files)
      {
      ListBox1.Items.Add(file.Name);
      }

      I have the source code but its not displaying anything in the listbox.. can anyone help me why is this so?

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      If I understand the question correctly, the problem is that you have placed the code that adds items into the listbox inside a SelectedIndexChanged event handler. So the code will only be run when an item from the list box is selected so if you have no items, the code is never run. Try moving the code to a place which is executed when the listbox is shown. For example if this is Windows Forms, you can use load event:

      public Form1() {
      InitializeComponent();

      this.Load += Form1_Load;
      }

      private void Form1_Load(object sender, EventArgs e) {
      DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
      FileInfo[] Files = dinfo.GetFiles("*.txt");
      foreach (FileInfo file in Files)
      {
      ListBox1.Items.Add(file.Name);
      }
      }

      M 2 Replies Last reply
      0
      • W Wendelius

        If I understand the question correctly, the problem is that you have placed the code that adds items into the listbox inside a SelectedIndexChanged event handler. So the code will only be run when an item from the list box is selected so if you have no items, the code is never run. Try moving the code to a place which is executed when the listbox is shown. For example if this is Windows Forms, you can use load event:

        public Form1() {
        InitializeComponent();

        this.Load += Form1_Load;
        }

        private void Form1_Load(object sender, EventArgs e) {
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
        FileInfo[] Files = dinfo.GetFiles("*.txt");
        foreach (FileInfo file in Files)
        {
        ListBox1.Items.Add(file.Name);
        }
        }

        M Offline
        M Offline
        Member 12016106
        wrote on last edited by
        #3

        ya, its worked. Thank you so much for the kind share.

        1 Reply Last reply
        0
        • W Wendelius

          If I understand the question correctly, the problem is that you have placed the code that adds items into the listbox inside a SelectedIndexChanged event handler. So the code will only be run when an item from the list box is selected so if you have no items, the code is never run. Try moving the code to a place which is executed when the listbox is shown. For example if this is Windows Forms, you can use load event:

          public Form1() {
          InitializeComponent();

          this.Load += Form1_Load;
          }

          private void Form1_Load(object sender, EventArgs e) {
          DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
          FileInfo[] Files = dinfo.GetFiles("*.txt");
          foreach (FileInfo file in Files)
          {
          ListBox1.Items.Add(file.Name);
          }
          }

          M Offline
          M Offline
          Member 12016106
          wrote on last edited by
          #4

          I have one more question. is the files can be download to local from listbox? I have looked for it but I did not get any source code for it.

          W 1 Reply Last reply
          0
          • M Member 12016106

            I have one more question. is the files can be download to local from listbox? I have looked for it but I did not get any source code for it.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            I'm sorry but I don't quite understand the question. Also this seems to be a separate issue, so I suggest posting a new question with relevant details.

            M 1 Reply Last reply
            0
            • W Wendelius

              I'm sorry but I don't quite understand the question. Also this seems to be a separate issue, so I suggest posting a new question with relevant details.

              M Offline
              M Offline
              Member 12016106
              wrote on last edited by
              #6

              ok. thank you

              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