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. C#
  4. Problem with Listbox and removing items... [modified]

Problem with Listbox and removing items... [modified]

Scheduled Pinned Locked Moved C#
helpdata-structurestutorialquestion
4 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.
  • M Offline
    M Offline
    Matjaz xyz
    wrote on last edited by
    #1

    Hi. I'm making an application and have a lot of problems. First of all, i had trouble figuring out - how to get all the items to lower case. Ok, i fixed that... but the program doesnt work the way it should. So, the program should work this way: You start the app. Then u click the button to get all the files from directory and all subdirectories. This goes ok. Ok, but then, i want to remove all doubles in the list. If theres a file "BeBack.JPG" the app. should check if there's also "BeBack.PDF" or if theres "BlackWhite.PDF" and "BlackWhite_1.PDF" it should remove both entries from the listbox. My code doesnt do that :( i dont know why :( It also deosnt remove all .gif files :( Where did i go wrong? Please help? Here's the code:

    private void button1_Click(object sender, EventArgs e)
    {
    // for getting the items ToLower case
    string[] fajli = Directory.GetFiles(Application.StartupPath,"*.*",SearchOption.AllDirectories);
    string[] VsiFajli = Array.ConvertAll<string,>(fajli, delegate(string s) { return s.ToLower(); });

            listBox1.Items.AddRange(VsiFajli);
    
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                // checks if the file is .pdf
                if (listBox1.Items\[i\].ToString() == listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4, 4) + ".pdf")
                {
                    if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + "\_1.pdf"))
                    {
                        string temp = listBox1.Items\[i\].ToString();
                        
                        listBox1.Items.Remove(temp);
                        listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + "\_1.pdf");
    
    
                        //listBox1.Items.RemoveAt(i);
                    }
                }
                else // if its not pdf ...
                {
                    if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + ".pdf"))
                    {
                        string temp = listBox1.Items\[i\].ToString();
                        listBox1.Items.Remove(temp);
                        listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + ".pdf");
                        listBox1.Items.Remove(temp.Remove(temp.Length - 4, 4) + "\_1.pdf");
    
    
                        //listBox1.Items.RemoveAt(i);
                    }
                }
    
    L D 2 Replies Last reply
    0
    • M Matjaz xyz

      Hi. I'm making an application and have a lot of problems. First of all, i had trouble figuring out - how to get all the items to lower case. Ok, i fixed that... but the program doesnt work the way it should. So, the program should work this way: You start the app. Then u click the button to get all the files from directory and all subdirectories. This goes ok. Ok, but then, i want to remove all doubles in the list. If theres a file "BeBack.JPG" the app. should check if there's also "BeBack.PDF" or if theres "BlackWhite.PDF" and "BlackWhite_1.PDF" it should remove both entries from the listbox. My code doesnt do that :( i dont know why :( It also deosnt remove all .gif files :( Where did i go wrong? Please help? Here's the code:

      private void button1_Click(object sender, EventArgs e)
      {
      // for getting the items ToLower case
      string[] fajli = Directory.GetFiles(Application.StartupPath,"*.*",SearchOption.AllDirectories);
      string[] VsiFajli = Array.ConvertAll<string,>(fajli, delegate(string s) { return s.ToLower(); });

              listBox1.Items.AddRange(VsiFajli);
      
              for (int i = 0; i < listBox1.Items.Count; i++)
              {
                  // checks if the file is .pdf
                  if (listBox1.Items\[i\].ToString() == listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4, 4) + ".pdf")
                  {
                      if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + "\_1.pdf"))
                      {
                          string temp = listBox1.Items\[i\].ToString();
                          
                          listBox1.Items.Remove(temp);
                          listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + "\_1.pdf");
      
      
                          //listBox1.Items.RemoveAt(i);
                      }
                  }
                  else // if its not pdf ...
                  {
                      if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + ".pdf"))
                      {
                          string temp = listBox1.Items\[i\].ToString();
                          listBox1.Items.Remove(temp);
                          listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + ".pdf");
                          listBox1.Items.Remove(temp.Remove(temp.Length - 4, 4) + "\_1.pdf");
      
      
                          //listBox1.Items.RemoveAt(i);
                      }
                  }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The code is almost unreadable, please use the <pre> tags and use proper temporary variables instead of calling listBox1.Items[j].ToString() and others a hundred times. Also, did you know there's a EndsWith method? regards

      M 1 Reply Last reply
      0
      • L Lost User

        The code is almost unreadable, please use the <pre> tags and use proper temporary variables instead of calling listBox1.Items[j].ToString() and others a hundred times. Also, did you know there's a EndsWith method? regards

        M Offline
        M Offline
        Matjaz xyz
        wrote on last edited by
        #3

        Not until now, i didnt know about ends with. Thank you. Anyway... yes, the code is a mess here in CP... but still, can sombody give me a clue for a better algorythm or something?

        Regards, Matjaž

        1 Reply Last reply
        0
        • M Matjaz xyz

          Hi. I'm making an application and have a lot of problems. First of all, i had trouble figuring out - how to get all the items to lower case. Ok, i fixed that... but the program doesnt work the way it should. So, the program should work this way: You start the app. Then u click the button to get all the files from directory and all subdirectories. This goes ok. Ok, but then, i want to remove all doubles in the list. If theres a file "BeBack.JPG" the app. should check if there's also "BeBack.PDF" or if theres "BlackWhite.PDF" and "BlackWhite_1.PDF" it should remove both entries from the listbox. My code doesnt do that :( i dont know why :( It also deosnt remove all .gif files :( Where did i go wrong? Please help? Here's the code:

          private void button1_Click(object sender, EventArgs e)
          {
          // for getting the items ToLower case
          string[] fajli = Directory.GetFiles(Application.StartupPath,"*.*",SearchOption.AllDirectories);
          string[] VsiFajli = Array.ConvertAll<string,>(fajli, delegate(string s) { return s.ToLower(); });

                  listBox1.Items.AddRange(VsiFajli);
          
                  for (int i = 0; i < listBox1.Items.Count; i++)
                  {
                      // checks if the file is .pdf
                      if (listBox1.Items\[i\].ToString() == listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4, 4) + ".pdf")
                      {
                          if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + "\_1.pdf"))
                          {
                              string temp = listBox1.Items\[i\].ToString();
                              
                              listBox1.Items.Remove(temp);
                              listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + "\_1.pdf");
          
          
                              //listBox1.Items.RemoveAt(i);
                          }
                      }
                      else // if its not pdf ...
                      {
                          if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + ".pdf"))
                          {
                              string temp = listBox1.Items\[i\].ToString();
                              listBox1.Items.Remove(temp);
                              listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + ".pdf");
                              listBox1.Items.Remove(temp.Remove(temp.Length - 4, 4) + "\_1.pdf");
          
          
                              //listBox1.Items.RemoveAt(i);
                          }
                      }
          
          D Offline
          D Offline
          Dragonfly_Lee
          wrote on last edited by
          #4

          Perhaps you can make a tiny demo and debug the code. That will be faster to find out the problem.

          :) I Love KongFu~

          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