3 Replies - 1427 Views - Last Post: 15 February 2009 - 04:56 AM Rate Topic: -----

#1 TADS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 161
  • Joined: 09-August 08

replacing data between listboxes

Posted 13 February 2009 - 06:45 AM

hi all, how we doing?

i have come accross another problem....

im trying to replace data from listbox1.selecteditem to listbox2.selecteditem
i have a temp.txt text file that loads in to listbox1 (rlines) this is the methord i use to load each file in to a list box...

Try
			Dim file_name As String = tempdata()
			Dim stream_reader As New IO.StreamReader(file_name)
			Dim line As String

			line = stream_reader.ReadLine()

			Do While Not (line Is Nothing)
				'  make sure the line aint blank.
				line = line.Trim()



				If line.Length > 0 Then rlines.Items.Add(line)

				line = stream_reader.ReadLine()
			Loop
			filen.SelectedIndex = 0
			stream_reader.Close()
		Catch exc As Exception
			' if errors.
			MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read Error")
		End Try



now if i code in to the listbox2 load button (button that loads a text file in to listbox2)


				 If line.Contains("my line of text") Then
					 line = Replace(line, "my line of text", rlines.SelectedItem)
				 End If



it dose what it says and replaces that line of text from the data loading in to listbox2 with the data i selected in listboz1(rlines) but what im looking for is to have the data load in to listbox2 and then have a button so i can select a line in listbox1 and in listbox 2 and it will replace the line from listbox1 in to listbox2 now i can add a line in to listbox2 from listbox1 with

listbox2.iteams.add(listbox1.selecteditem)



but cant find a replace for it

if anyone knows how i can do this it would be excellent thanks for looking

TADS

Is This A Good Question/Topic? 0
  • +

Replies To: replacing data between listboxes

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: replacing data between listboxes

Posted 15 February 2009 - 02:13 AM

You're right there isn't a replace method, but you can achieve the effect by removing the old item and inserting the new item at the selectedIndex position.

See ListBox.ObjectCollection Methods
Was This Post Helpful? 1
  • +
  • -

#3 TADS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 161
  • Joined: 09-August 08

Re: replacing data between listboxes

Posted 15 February 2009 - 03:51 AM

thanks for the reply... having a read though that now... most of the time its knowing what to google not how long you google for

thanks again

TADS
Was This Post Helpful? 0
  • +
  • -

#4 TADS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 161
  • Joined: 09-August 08

Re: replacing data between listboxes

Posted 15 February 2009 - 04:56 AM

all sorted on that problem... just spent a little while trying different things with the info given by n8wxs and i have got it working... i put some comments in to show how i done it in the end and it look so simple just couldnt get it to start with

Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim returnvalue As String

		'set returnvalue to find indexof listbox1.selecteditem
		returnvalue = ListBox1.Items.IndexOf(ListBox1.SelectedItem)
		MsgBox(returnvalue) 'test to check indexof so i know
		'insert data from listbox2.selecteditem
		ListBox1.Items.Insert(returnvalue, filen.SelectedItem)
		'now remove the data that you wanted to replace
		ListBox1.Items.Remove(ListBox1.SelectedItem)
		'make sure its put the data in the same postion as the start data 
		MsgBox(returnvalue)
end sub




thanks again for the reply helped on end :D

TADS

This post has been edited by TADS: 15 February 2009 - 04:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1