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. How I write this logic to check if string in a list and also checked ?

How I write this logic to check if string in a list and also checked ?

Scheduled Pinned Locked Moved C#
question
6 Posts 3 Posters 44 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
    Martin Adams 2023
    wrote on last edited by
    #1

    Hello, I have list checked box has items like "C:\" , "D:\" etc... and each item user can check or not. I need to test if current loop string value (drive) is checked or not in the list box In addition I want to test "if not" condition for ex like this:

    if (drive not in chklist_drives.CheckedItems) continue;

    OriginalGriffO 1 Reply Last reply
    0
    • M Martin Adams 2023

      Hello, I have list checked box has items like "C:\" , "D:\" etc... and each item user can check or not. I need to test if current loop string value (drive) is checked or not in the list box In addition I want to test "if not" condition for ex like this:

      if (drive not in chklist_drives.CheckedItems) continue;

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      And? What have you tried? Where are you stuck? What help do you need? This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project. Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are? That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        And? What have you tried? Where are you stuck? What help do you need? This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project. Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are? That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        M Offline
        M Offline
        Martin Adams 2023
        wrote on last edited by
        #3

        Hold on. I did not that my question is vague . I have a list checked box that has items like "C:\", "D:\" etc... and I need to check current string value in a loop if that value is checked or not in the list box What I tried: I have a loop like this:

        foreach (drive in all drives)
        {
        // I need here to check if drive value (for example D:\) checked in the list or not . this is pseudo code

        if (drive not in chklist_drives.CheckedItems) continue;

        }

        Thats it I dont know how I can implement this if line of code

        Richard DeemingR 1 Reply Last reply
        0
        • M Martin Adams 2023

          Hold on. I did not that my question is vague . I have a list checked box that has items like "C:\", "D:\" etc... and I need to check current string value in a loop if that value is checked or not in the list box What I tried: I have a loop like this:

          foreach (drive in all drives)
          {
          // I need here to check if drive value (for example D:\) checked in the list or not . this is pseudo code

          if (drive not in chklist_drives.CheckedItems) continue;

          }

          Thats it I dont know how I can implement this if line of code

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          As you have discovered, drive not in chklist_drives.CheckedItems is not valid C#. Assuming this is a Windows Forms CheckedListBox control, the CheckedItems collection provides a Contains method to test whether an item is in the collection: CheckedListBox.CheckedItemCollection.Contains(Object) Method (System.Windows.Forms) | Microsoft Learn[^] But you're also going to find that foreach (drive in all drives) is not valid C# either. Perhaps you're looking for the System.IO.DriveInfo.GetDrives method[^]?

          foreach (DriveInfo drive in DriveInfo.GetDrives())
          {
          if (!drive.IsReady) continue;
          if (!chklist_drives.CheckedItems.Contains(drive.Name)) continue;

          ...
          

          }

          You'll need to adjust that code so that the value you check for in the list matches the value you added to the list in the first place.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            As you have discovered, drive not in chklist_drives.CheckedItems is not valid C#. Assuming this is a Windows Forms CheckedListBox control, the CheckedItems collection provides a Contains method to test whether an item is in the collection: CheckedListBox.CheckedItemCollection.Contains(Object) Method (System.Windows.Forms) | Microsoft Learn[^] But you're also going to find that foreach (drive in all drives) is not valid C# either. Perhaps you're looking for the System.IO.DriveInfo.GetDrives method[^]?

            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
            if (!drive.IsReady) continue;
            if (!chklist_drives.CheckedItems.Contains(drive.Name)) continue;

            ...
            

            }

            You'll need to adjust that code so that the value you check for in the list matches the value you added to the list in the first place.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            M Offline
            M Offline
            Martin Adams 2023
            wrote on last edited by
            #5

            Thanks alot. Second if is what I need.

            OriginalGriffO 1 Reply Last reply
            0
            • M Martin Adams 2023

              Thanks alot. Second if is what I need.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Now compare that against your original question and it should be obvious why it took you 8 hours to get an answer! If you give us details we can help a lot quicker!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              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