Help edit a value in a ListBox

Currymunch

Member
Joined
Jan 21, 2008
Messages
13
Programming Experience
Beginner
Edit : title should read - Help with editing a value in a ListBox


Howdy all, I'm having a slight problem at the moment with setting up to edit a value in a listbox

When i have a value selected in a listbox and press edit, I aim to open another form with the details of the selected index already filled in. The ListBox is storing the ToString data about a class that i setup called CSpy(naming issues already highlighted in a previous post, it's to aid the marking apparently)



Image Below is the form. But i would like it to have the values entered already entered for an edit so in this case -- first name = joe, second name = Bloggs etc




I think it's going wrong when it tries to convert the Cspy to a string (if thats the case how do i extract the data in the listbox as a string so that i can maniplate it) or am I way off.

error ==>
'***********************************************

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Cast from type 'CSpy' to type 'String' is not valid.

'***********************************************


This is the culmination of my efforts thus far.

Form is loaded, if title bar = Edit Spy then try all that stuff below


'*****************************************************
'*The Code below aims to split the selected index of the listbox up by the '*commas.
'*****************************************************
Private Sub entrySpy_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Me.Text = "Edit Spy" Then
Dim myArray(8) As String

myArray = Split(mainSpy1.spyList.Items(mainSpy1.spyList.SelectedIndex)), ",")
txtFirstName.Text = myArray(0)
txtSurname.Text = myArray(1)
txtCodeName.Text = myArray(2)
comboAge.Text = myArray(3)
comboSex.Text = myArray(4)
txtDmarks.Text = myArray(5)
txtSkills.Text = myArray(6)
txtBankNumber.Text = myArray(7)

End If
'*******************************************

Any ideas how i should proceed? Am I going up totally the wrong line of attack and should add a method to split up the listbox in the class?

Thanks in advance
 
Last edited:
VB.NET:
dim s as cspy= spylist.selecteditem
Create the edit dialog instance, pass the value to the edit dialog instance, then show the edit dialog instance.
 
Thanks for the reply ^^

Couldn't get the way you said to work in the end i set it up this way, (thought i'd post if anyone was curious)

'***************************************************
'*
'*

Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click

If spyList.SelectedIndex >= 0 Then
entrySpyEdit.Text = "Edit Spy" 'sets the title bar to equal edit spy
Dim i As Integer
i = spyList.SelectedIndex ' i = index
spyList.Items(i).ToString()
entrySpyEdit.ShowDialog()

Else : MsgBox("No spy selected to edit")
End If

End Sub




''******************************************************
'*Form loading
'*
'******************************************************

If Me.Text = "Edit Spy" Then
txtAgeReadOnly.Visible = False
txtSexReadOnly.Visible = False


Dim s As String

i = mainSpy1.spyList.SelectedIndex
s = mainSpy1.spyList.Items(i).ToString()


Dim myArray(8) As String
myArray = Split(s, (","))

txtFirstName.Text = myArray(0)
txtSurname.Text = myArray(1)
txtCodeName.Text = myArray(2)
comboAge.Text = myArray(3)
comboSex.Text = myArray(4)
txtDmarks.Text = myArray(5)
txtSkills.Text = myArray(6)
txtBankNumber.Text = myArray(7)
 
I thought you had CSpy items in your listbox, that would be easier to handle than string representations of the CSpy data.
 
I had the ToString values of the class that i had created in the listbox - I think my explanations need work >_< when i tried to use

'*
dim s as cspy= spylist.selecteditem
'*

When it got fed to the edit form it passed an error, I maybe didn't persevere long enough down that line of thought but it's working now the way I am doing it (even if it's a scrappy botch job heh - gotta start somewhere though)
 
Back
Top