Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › Visual Basic Programming › Deleting Listbox Items?

Deleting Listbox Items?

Posts 1–9 of 9 · Page 1 of 1
WI
williamph
Deleting Listbox Items?
I'm using this code to save/load listbox items:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim w As IO.StreamWriter
Dim r As IO.StreamReader

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
w = New IO.StreamWriter("c:\test.txt")
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
r = New IO.StreamReader("c:\test.txt")
While (r.Peek() > -1)
ListBox1.Items.Add(r.ReadLine)
End While
r.Close()
End Sub
End Class

from:http://www.mpgh.net/forum/33-visual-...-contents.html
now i need to know how to remove the items without manually going into the text file and editing it. so i need a code for a button to remove the selected item.
#1 · 15y ago
nathanael890
nathanael890
Its simple :P
Code:
ListBox1.Items.Remove(ListBox1.SelectedItem)
#2 · 15y ago
WI
williamph
Quote Originally Posted by nathanael890 View Post
Its simple :P
Code:
ListBox1.Items.Remove(ListBox1.SelectedItem)
but if i reload the app the item comes up again so i also need to delete it from the txt file but idont know the code
#3 · 15y ago
LY
Lyoto Machida
Just ListBox1.Items.Remove(ListBox1.SelectedItem)
and then Re-Save the file..
#4 · 15y ago
nathanael890
nathanael890
Quote Originally Posted by williamph View Post
but if i reload the app the item comes up again so i also need to delete it from the txt file but idont know the code
I just written this code today and I hope it would help you

Code:
    Public Sub RemoveItem(ByVal List As ListBox, ByVal Item As String, ByVal Filename As String)
        Try
            Dim xText As New RichTextBox With {.Text = IO.File.ReadAllText(Filename)}
            List.Items.AddRange(xText.Lines)
            List.Items.Remove(Item)
            xText.Text = ""
            For Each ITems As String In ListBox1.Items
                xText.Text &= ITems & vbCrLf
            Next
            IO.File.WriteAllText(Filename, xText.Text)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
How to use:
Code:
'Note: Make sure that the listbox has contents so you can remove it
RemoveItem(ListBox1, ListBox1.SelectedItem, "<filename>")
I'm out.. I have no more time I hope it would help you.
Or ask others if there are more easier.
#5 · 15y ago
Blubb1337
Blubb1337
Quote Originally Posted by nathanael890 View Post
I just written this code today and I hope it would help you

Code:
    Public Sub RemoveItem(ByVal List As ListBox, ByVal Item As String, ByVal Filename As String)
        Try
            Dim xText As New RichTextBox With {.Text = IO.File.ReadAllText(Filename)}
            List.Items.AddRange(xText.Lines)
            List.Items.Remove(Item)
            xText.Text = ""
            For Each ITems As String In ListBox1.Items
                xText.Text &= ITems & vbCrLf
            Next
            IO.File.WriteAllText(Filename, xText.Text)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
How to use:
Code:
'Note: Make sure that the listbox has contents so you can remove it
RemoveItem(ListBox1, ListBox1.SelectedItem, "<filename>")
I'm out.. I have no more time I hope it would help you.
Or ask others if there are more easier.
He just wants to remove a selected item, so your code is completely unnecessary, lulz.

Previous posts should work.
#6 · 15y ago
Jason
Jason
Quote Originally Posted by Blubb1337 View Post
He just wants to remove a selected item, so your code is completely unnecessary, lulz.

Previous posts should work.
And remove the line from the textfile too

More to the point, use this code for saving/reloading

[highlight=vb.net]
Private Sub SaveListBox(ByVal saveLoc As String, ByVal listBox As ListBox)
If Not (IO.File.Exists(saveLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
IO.File.WriteAllLines(saveLoc, listBox.Items.Cast(Of Object).Select(Function(o As Object) o.ToString()).ToArray())
End Sub

Private Sub RetrieveListBox(ByVal loadLoc As String, ByVal listBox As ListBox)
listBox.Items.Clear()
If Not (IO.File.Exists(loadLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
listBox.Items.AddRange(IO.File.ReadAllLines(loadLo c))
End Sub
[/highlight]
#7 · 15y ago
Blubb1337
Blubb1337
Quote Originally Posted by Cho Chang View Post


And remove the line from the textfile too

More to the point, use this code for saving/reloading

[highlight=vb.net]
Private Sub SaveListBox(ByVal saveLoc As String, ByVal listBox As ListBox)
If Not (IO.File.Exists(saveLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
IO.File.WriteAllLines(saveLoc, listBox.Items.Cast(Of Object).Select(Function(o As Object) o.ToString()).ToArray())
End Sub

Private Sub RetrieveListBox(ByVal loadLoc As String, ByVal listBox As ListBox)
listBox.Items.Clear()
If Not (IO.File.Exists(loadLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
listBox.Items.AddRange(IO.File.ReadAllLines(loadLo c))
End Sub
[/highlight]
That line shouldn't show up in the textfile when resaving eh?!
#8 · 15y ago
sythe179
sythe179
already said...but this is my 2 cents...
againced blubb...not worth a brass razoo...(actuly own a brass razoo...)



top :
Code:
Imports System.IO
on form close:
Code:
Dim File As StreamWriter = File.CreateText("file.txt")
        For Each strline As String In lst.Items
            SuburbFile.WriteLine(Suburb)
        Next
        File.Close()
on remove event :
Code:
lstsuburbs.Items.Remove(lst.selecteditem)

on form load:
Code:
try
Dim File As StreamReader = File.OpenText("file.txt")
        Do While File.Peek <> -1
            lst.Items.Add(File.ReadLine())
        Loop
        File.Close()
catch ex as exeption
msgbox("file not created yet")
end try
#9 · edited 15y ago · 15y ago
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Tags for this Thread

None