How to Delete File(s) Listed in a ListBox (not ListBox1.Clear)
I am working on a program that can create and delete files within it. The files are located (for example) at: C:FilesForProgramXXXXXXX The Xs are for the name of the program.
Say, for example that I want to delete the following files:
- C:FilesForProgramXXXXXXXdeleteme.txt
- C:FilesForProgramXXXXXXXdeleteme2.exe
- C:FilesForProgramXXXXXXXTrashineedtobedeleted.txt
I have no way to do that. I can create the files, but not delete them. Here is a example of what the code I am currently using:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If ListBox3.Items.Count - 1 >= 0 Then
ListBox3.Enabled = True
Button3.Enabled = True
End If
For Each path As String In ListBox3.SelectedItems
System.IO.File.Delete(path)
Next
When I click the button to delete the files, nothing happens. If I remember correctly, the program will just enter into break mode. I do not know what is wrong with the code. To me it looks perfectly fine. But then again, I am still trying to learn VB.Net. Any help is greatly appreciated.
Thanks!
.net vb.net visual-studio visual-studio-2017
add a comment |
I am working on a program that can create and delete files within it. The files are located (for example) at: C:FilesForProgramXXXXXXX The Xs are for the name of the program.
Say, for example that I want to delete the following files:
- C:FilesForProgramXXXXXXXdeleteme.txt
- C:FilesForProgramXXXXXXXdeleteme2.exe
- C:FilesForProgramXXXXXXXTrashineedtobedeleted.txt
I have no way to do that. I can create the files, but not delete them. Here is a example of what the code I am currently using:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If ListBox3.Items.Count - 1 >= 0 Then
ListBox3.Enabled = True
Button3.Enabled = True
End If
For Each path As String In ListBox3.SelectedItems
System.IO.File.Delete(path)
Next
When I click the button to delete the files, nothing happens. If I remember correctly, the program will just enter into break mode. I do not know what is wrong with the code. To me it looks perfectly fine. But then again, I am still trying to learn VB.Net. Any help is greatly appreciated.
Thanks!
.net vb.net visual-studio visual-studio-2017
A ListBox does not contain files directly - butListViewItem
object instances which can represent anything - and whose lifetime is independent of the object they represent (such as a file on-disk or a business/domain object).
– Dai
Mar 24 '18 at 1:00
@Dai Thank you for the information. Is there a way to provide a example for me, to help me build from there. If so that would be greatly appreciated!
– TheCrafters001
Mar 24 '18 at 1:03
Can you add one sample item value from the list box to your question?
– Sunil
Mar 24 '18 at 7:35
@Sunil I have added that, the sample path is: C:TestFolderText.txt but I want to delete all files in the listbox, such as: C:TestFolderText.txt C:TestFolderText2.txt C:TestFolderText3.txt and so on
– TheCrafters001
Mar 24 '18 at 22:34
@Sunil, I have updated my question to make it more understandable, I hope that helps.
– TheCrafters001
Jan 5 at 14:38
add a comment |
I am working on a program that can create and delete files within it. The files are located (for example) at: C:FilesForProgramXXXXXXX The Xs are for the name of the program.
Say, for example that I want to delete the following files:
- C:FilesForProgramXXXXXXXdeleteme.txt
- C:FilesForProgramXXXXXXXdeleteme2.exe
- C:FilesForProgramXXXXXXXTrashineedtobedeleted.txt
I have no way to do that. I can create the files, but not delete them. Here is a example of what the code I am currently using:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If ListBox3.Items.Count - 1 >= 0 Then
ListBox3.Enabled = True
Button3.Enabled = True
End If
For Each path As String In ListBox3.SelectedItems
System.IO.File.Delete(path)
Next
When I click the button to delete the files, nothing happens. If I remember correctly, the program will just enter into break mode. I do not know what is wrong with the code. To me it looks perfectly fine. But then again, I am still trying to learn VB.Net. Any help is greatly appreciated.
Thanks!
.net vb.net visual-studio visual-studio-2017
I am working on a program that can create and delete files within it. The files are located (for example) at: C:FilesForProgramXXXXXXX The Xs are for the name of the program.
Say, for example that I want to delete the following files:
- C:FilesForProgramXXXXXXXdeleteme.txt
- C:FilesForProgramXXXXXXXdeleteme2.exe
- C:FilesForProgramXXXXXXXTrashineedtobedeleted.txt
I have no way to do that. I can create the files, but not delete them. Here is a example of what the code I am currently using:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If ListBox3.Items.Count - 1 >= 0 Then
ListBox3.Enabled = True
Button3.Enabled = True
End If
For Each path As String In ListBox3.SelectedItems
System.IO.File.Delete(path)
Next
When I click the button to delete the files, nothing happens. If I remember correctly, the program will just enter into break mode. I do not know what is wrong with the code. To me it looks perfectly fine. But then again, I am still trying to learn VB.Net. Any help is greatly appreciated.
Thanks!
.net vb.net visual-studio visual-studio-2017
.net vb.net visual-studio visual-studio-2017
edited Mar 22 at 22:07
TheCrafters001
asked Mar 24 '18 at 0:54


TheCrafters001TheCrafters001
5911
5911
A ListBox does not contain files directly - butListViewItem
object instances which can represent anything - and whose lifetime is independent of the object they represent (such as a file on-disk or a business/domain object).
– Dai
Mar 24 '18 at 1:00
@Dai Thank you for the information. Is there a way to provide a example for me, to help me build from there. If so that would be greatly appreciated!
– TheCrafters001
Mar 24 '18 at 1:03
Can you add one sample item value from the list box to your question?
– Sunil
Mar 24 '18 at 7:35
@Sunil I have added that, the sample path is: C:TestFolderText.txt but I want to delete all files in the listbox, such as: C:TestFolderText.txt C:TestFolderText2.txt C:TestFolderText3.txt and so on
– TheCrafters001
Mar 24 '18 at 22:34
@Sunil, I have updated my question to make it more understandable, I hope that helps.
– TheCrafters001
Jan 5 at 14:38
add a comment |
A ListBox does not contain files directly - butListViewItem
object instances which can represent anything - and whose lifetime is independent of the object they represent (such as a file on-disk or a business/domain object).
– Dai
Mar 24 '18 at 1:00
@Dai Thank you for the information. Is there a way to provide a example for me, to help me build from there. If so that would be greatly appreciated!
– TheCrafters001
Mar 24 '18 at 1:03
Can you add one sample item value from the list box to your question?
– Sunil
Mar 24 '18 at 7:35
@Sunil I have added that, the sample path is: C:TestFolderText.txt but I want to delete all files in the listbox, such as: C:TestFolderText.txt C:TestFolderText2.txt C:TestFolderText3.txt and so on
– TheCrafters001
Mar 24 '18 at 22:34
@Sunil, I have updated my question to make it more understandable, I hope that helps.
– TheCrafters001
Jan 5 at 14:38
A ListBox does not contain files directly - but
ListViewItem
object instances which can represent anything - and whose lifetime is independent of the object they represent (such as a file on-disk or a business/domain object).– Dai
Mar 24 '18 at 1:00
A ListBox does not contain files directly - but
ListViewItem
object instances which can represent anything - and whose lifetime is independent of the object they represent (such as a file on-disk or a business/domain object).– Dai
Mar 24 '18 at 1:00
@Dai Thank you for the information. Is there a way to provide a example for me, to help me build from there. If so that would be greatly appreciated!
– TheCrafters001
Mar 24 '18 at 1:03
@Dai Thank you for the information. Is there a way to provide a example for me, to help me build from there. If so that would be greatly appreciated!
– TheCrafters001
Mar 24 '18 at 1:03
Can you add one sample item value from the list box to your question?
– Sunil
Mar 24 '18 at 7:35
Can you add one sample item value from the list box to your question?
– Sunil
Mar 24 '18 at 7:35
@Sunil I have added that, the sample path is: C:TestFolderText.txt but I want to delete all files in the listbox, such as: C:TestFolderText.txt C:TestFolderText2.txt C:TestFolderText3.txt and so on
– TheCrafters001
Mar 24 '18 at 22:34
@Sunil I have added that, the sample path is: C:TestFolderText.txt but I want to delete all files in the listbox, such as: C:TestFolderText.txt C:TestFolderText2.txt C:TestFolderText3.txt and so on
– TheCrafters001
Mar 24 '18 at 22:34
@Sunil, I have updated my question to make it more understandable, I hope that helps.
– TheCrafters001
Jan 5 at 14:38
@Sunil, I have updated my question to make it more understandable, I hope that helps.
– TheCrafters001
Jan 5 at 14:38
add a comment |
1 Answer
1
active
oldest
votes
If you want to delete items from a listbox using a loop you need to remove the items from the highest index and go down to 0. That way the indices of the items won't be changed during removal..
if you want to remove all items use the clear command:
listBox1.Items.Clear()
Sorry, @minimalist, but I am looking for a different answer, I updated my question so you could understand it better.
– TheCrafters001
Jan 5 at 14:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49460155%2fhow-to-delete-files-listed-in-a-listbox-not-listbox1-clear%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to delete items from a listbox using a loop you need to remove the items from the highest index and go down to 0. That way the indices of the items won't be changed during removal..
if you want to remove all items use the clear command:
listBox1.Items.Clear()
Sorry, @minimalist, but I am looking for a different answer, I updated my question so you could understand it better.
– TheCrafters001
Jan 5 at 14:36
add a comment |
If you want to delete items from a listbox using a loop you need to remove the items from the highest index and go down to 0. That way the indices of the items won't be changed during removal..
if you want to remove all items use the clear command:
listBox1.Items.Clear()
Sorry, @minimalist, but I am looking for a different answer, I updated my question so you could understand it better.
– TheCrafters001
Jan 5 at 14:36
add a comment |
If you want to delete items from a listbox using a loop you need to remove the items from the highest index and go down to 0. That way the indices of the items won't be changed during removal..
if you want to remove all items use the clear command:
listBox1.Items.Clear()
If you want to delete items from a listbox using a loop you need to remove the items from the highest index and go down to 0. That way the indices of the items won't be changed during removal..
if you want to remove all items use the clear command:
listBox1.Items.Clear()
edited Mar 24 '18 at 9:33
answered Mar 24 '18 at 9:23
minimalistminimalist
774
774
Sorry, @minimalist, but I am looking for a different answer, I updated my question so you could understand it better.
– TheCrafters001
Jan 5 at 14:36
add a comment |
Sorry, @minimalist, but I am looking for a different answer, I updated my question so you could understand it better.
– TheCrafters001
Jan 5 at 14:36
Sorry, @minimalist, but I am looking for a different answer, I updated my question so you could understand it better.
– TheCrafters001
Jan 5 at 14:36
Sorry, @minimalist, but I am looking for a different answer, I updated my question so you could understand it better.
– TheCrafters001
Jan 5 at 14:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49460155%2fhow-to-delete-files-listed-in-a-listbox-not-listbox1-clear%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
A ListBox does not contain files directly - but
ListViewItem
object instances which can represent anything - and whose lifetime is independent of the object they represent (such as a file on-disk or a business/domain object).– Dai
Mar 24 '18 at 1:00
@Dai Thank you for the information. Is there a way to provide a example for me, to help me build from there. If so that would be greatly appreciated!
– TheCrafters001
Mar 24 '18 at 1:03
Can you add one sample item value from the list box to your question?
– Sunil
Mar 24 '18 at 7:35
@Sunil I have added that, the sample path is: C:TestFolderText.txt but I want to delete all files in the listbox, such as: C:TestFolderText.txt C:TestFolderText2.txt C:TestFolderText3.txt and so on
– TheCrafters001
Mar 24 '18 at 22:34
@Sunil, I have updated my question to make it more understandable, I hope that helps.
– TheCrafters001
Jan 5 at 14:38