Hey guys Just learning about List Boxes, I have a quick Question Im Creating the Program where I have 3 text boxes and when I hit the add button it adds them to the list box off to the right if they already Exist in the list box then it will bring up a message box saying that it already exists.

I have another button labled Replace button and its suppose to update or replace the string or line if changes are needed to be updated or replaced. How can I creat it to Replace or update the Data in the String or Line?

Here is my Code:

Public Class Form1

    Dim ID As Integer
    Dim AName As String
    Dim ABalance As Double

    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click

        ID = AccountIDTextBox.Text
        AName = AccountNameTextBox.Text
        ABalance = BalanceTextBox.Text

        If AccountsListBox.Items.Contains(ID & ", " & AName & ", $" & ABalance) = True Then  
        'Checks to see if the line or String Exists

            MessageBox.Show("That Account already Exists!") 
            'If it does Exist or is already created

        Else

            AccountsListBox.Items.Add(ID & ", " & AName & ", $" & ABalance) 
            'If it it dosent exist it inserts or adds the line to the listbox

        End If

    End Sub

    Private Sub ReplaceButton_Click(sender As System.Object, e As System.EventArgs) Handles ReplaceButton.Click

        If AccountsListBox.Items.Contains(ID & ", " & AName & ", $" & ABalance) = True Then 
        'If the Line Does Exist replace or update the String or line


        Else

            MessageBox.Show("You cant replace that account because the Account dosent Exist!") 
            'If line does not Exist does not update or add

        End If
    End Sub
End Class

The following code will replace the record that matches "Fred 17" with the new text if it exists in the ListBox.

Dim text As String = "Fred 17"
Dim ind As Integer = ListBox1.Items.IndexOf(text)

If ind >= 0 Then
    ListBox1.Items(ind) = "Jim 29"
Else
    'add the text
End If

would i add this:

AccountsListBox.Items(ID) = AccountIDTextBox.Text
AccountsListBox.Items(AName) = AccountNameTextBox.Text
AccountsListBox.Items(ABalance) = BalanceTextBox.Text

Your original code checks to see if you have a line in the ListBox consisting of

ID & ", " & AName & ", $" & ABalance

If the line doesn't exist you can add it by

AccountsListBox.Items.Contains(ID & ", " & AName & ", $" & ABalance)

If it does exist, what do you want to replace it with?

aaaa I got it thanks man

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.