Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [VB] How to completely delete an item at a given index in a listbox?
New Posts  All Forums:Forum Nav:

[VB] How to completely delete an item at a given index in a listbox?

post #1 of 2
Thread Starter 
Ok, quick lowdown, I'm writing a program for the math tutors at my college to use for students with Dyscalculia (numlexia) that converts numerics into words, but it also has to handle mathematical operators.

My problem is inputing multiple parenthesis. If a line has a parenthesis open, it keeps taking input onto the same line until the parenthesis are closed. But when trying to open another set of parenthesis on the same line, it goes down to the next line.

Code:
    Sub lParenth(ByRef temp As String)

        If frmMain.lstOutput.Items.Count > 0 Then
            parenthIndex = frmMain.lstOutput.Items.Count - 1

            frmMain.lstOutput.Items.Insert(parenthIndex, frmMain.lstOutput.Items.Item(parenthIndex) + " ( ")
            frmMain.lstOutput.Items.RemoveAt(parenthIndex + 1)
        Else
            frmMain.lstOutput.Items.Add(" ( ")
        End If

        openParenth = True

    End Sub
As you can see, this essentially replaces the row with an updated version of itself. However, if I check the number of listbox items with each run of this specific code the number goes up. I thought that removing an item at an index completely removed that item, not just made the item at that index equal to nothing.

So if I run this code the first time it works, but if I try to add a second parenthesis to that line it goes to the next line because the number of items has increased by 1.

Is there any way to delete the final item in the listbox so that the count is correct and this doesn't happen?

Thanks.

~31337
Ganymede
(7 items)
 
  
CPUGraphicsRAMHard Drive
i7 4790T Nvidia GTX 860m 2GB 16GB Corsair DDR3L 1600Mhz 500GB Samsung 850 EVO 
OSCaseAudio
Windows 10 Pro x64 Alienware Steam Machine Creative Sound Blaster Omni 
  hide details  
Reply
Ganymede
(7 items)
 
  
CPUGraphicsRAMHard Drive
i7 4790T Nvidia GTX 860m 2GB 16GB Corsair DDR3L 1600Mhz 500GB Samsung 850 EVO 
OSCaseAudio
Windows 10 Pro x64 Alienware Steam Machine Creative Sound Blaster Omni 
  hide details  
Reply
post #2 of 2
Quote:
Originally Posted by 31337 View Post

Code:
            frmMain.lstOutput.Items.Insert(parenthIndex, frmMain.lstOutput.Items.Item(parenthIndex) + " ( ")

~31337
I think this may be your problem. Try this:

parenthIndex = frmMain.lstOutput.Items.Count - 1
dim tempListItem as listitem = frmMain.lstOutput.Items(parenthIndex)
frmMain.lstOutput.Items.RemoveAt(parenthIndex)
frmMain.lstOutput.Items.Add(tempListItem.Text & " (")

Edit:
Just thought of something... Why not just update the item there instead of removing it?

frmMain.lstOutput.Items(parenthIndex).Text += " ("
^ That should work
Edited by fball922 - 1/17/11 at 9:23pm
    
CPUMotherboardGraphicsRAM
Phenom II X6 1055T Asus M5A97 EVO 2 x Radeon 5870 2x4GB, 2x8GB 
Hard DriveHard DriveOSMonitor
120GB SSD 500GB WD 7300RPM x 2 Windows 7 Pro x64 2 x 23" Dell IPS 
PowerCase
750w OCZ A Desk 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Phenom II X6 1055T Asus M5A97 EVO 2 x Radeon 5870 2x4GB, 2x8GB 
Hard DriveHard DriveOSMonitor
120GB SSD 500GB WD 7300RPM x 2 Windows 7 Pro x64 2 x 23" Dell IPS 
PowerCase
750w OCZ A Desk 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [VB] How to completely delete an item at a given index in a listbox?