How I write this logic to check if string in a list and also checked ?
-
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;
-
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;
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!
-
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!
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 codeif (drive not in chklist_drives.CheckedItems) continue;
}
Thats it I dont know how I can implement this if line of code
-
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 codeif (drive not in chklist_drives.CheckedItems) continue;
}
Thats it I dont know how I can implement this if line of code
As you have discovered,
drive not in chklist_drives.CheckedItems
is not valid C#. Assuming this is a Windows FormsCheckedListBox
control, theCheckedItems
collection provides aContains
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 thatforeach (drive in all drives)
is not valid C# either. Perhaps you're looking for theSystem.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
-
As you have discovered,
drive not in chklist_drives.CheckedItems
is not valid C#. Assuming this is a Windows FormsCheckedListBox
control, theCheckedItems
collection provides aContains
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 thatforeach (drive in all drives)
is not valid C# either. Perhaps you're looking for theSystem.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
Thanks alot. Second if is what I need.
-
Thanks alot. Second if is what I need.
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!