Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

adding an item to a list on a different form

i have a form on which i want to be able to add an item to a textbox. when clicking the add button, it should add this item entered in the textbox to the listbox on another form. how can this be done?
[200 byte] By [MonaVohra] at [2007-11-11 10:28:04]
# 1 Re: adding an item to a list on a different form
You would need a reference to the other form. How does the second form get created? If form1 creates form2 then you should declare a global variable in form1 and store the instance of form2 in that variable when you create it. Otherwise you may need to create a module which holds a global variable there which you can use to hold the reference to your second form. Another option might be to create a public shared variable in your second form which would be used to store the current instance.
TwoFaced at 2007-11-11 20:47:45 >
# 2 Re: adding an item to a list on a different form
on form 1 i have a button called next, which takes me to form 2. i am very new to vb and not sure about some of the words you are using. would you be able to provide an example please?

on form1 i have a textbox in which i enter 'Restaurant'. I click on the Add button that i have created on form1. this button should add the word Restaurant to the listbox on form2. when i click on the 'next' button i have created on form1, i will be taken to form2. the listbox on form2 should contain the word Restaurant
MonaVohra at 2007-11-11 20:48:46 >
# 3 Re: adding an item to a list on a different form
The problem with adding items directly to form2 is that if form2 is closed by the user the data is gone. So form1 should keep track of the items internally. When it displays form2 it adds those items to the listbox on form2. Additionally when a new item is added this code checks to see if form2 exists and if so will also add the new item to form2.Public Class Form1

Dim frm As Form2 'Our instance of form2
Dim items As New List(Of String) 'Internal collection of items

'Add button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newItem As String = TextBox1.Text
items.Add(newItem) 'Add new item to our list

'If Form2 exists then add the item to it's listbox
If frm IsNot Nothing AndAlso Not frm.IsDisposed Then frm.ListBox1.Items.Add(newItem)
End Sub

'Next button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'If we haven't created Form2 or it's been closed then create a new instance
If frm Is Nothing OrElse frm.IsDisposed Then
frm = New Form2 'Create new instance
frm.ListBox1.Items.AddRange(items.ToArray) 'Add items to listbox1 on form2
frm.Show() 'Show the form
End If
End Sub
End Class
TwoFaced at 2007-11-11 20:49:40 >
# 4 Re: adding an item to a list on a different form
this works. however, when i go back to form 1 and add another item to be added to the listbox on form2, when i go to form2 again there is nothing there
MonaVohra at 2007-11-11 20:50:41 >
# 5 Re: adding an item to a list on a different form
Did you make any changes to the code. As I gave it to you it should work in the manner you'd like. Whenever I add something from form1 it is added to the listbox on form2. If you made changes please post the code. If you didn't make changes then I'm not sure what to say. Maybe if you describe the exact steps you made so I could attemp to recreate the problem.
TwoFaced at 2007-11-11 20:51:41 >
# 6 Re: adding an item to a list on a different form
i have used the code you gave. this is what i did:

I am on form 1. I add 'Restaurant' in the textbox and click on the Add button. Then I click on the Next button so that I am taken to form 2. The word Restaurant is added to the list box. I click on the Back button so that i am taken back to form 1. I add the word 'Bank' in the textbox and click on the Add button. i click on the Next button to go to form 2 again. There is nothing in the listbox at all
MonaVohra at 2007-11-11 20:52:51 >
# 7 Re: adding an item to a list on a different form
Okay the problem must be in going 'back' to form1. What happens when you open form2. Is form1 closed at any point? What is the code to go 'back' to form1.

I think the solution for you would be to create a module and put the items declaration in the module. You can add a module by going to the Project menu and selecting add module. Everything in a module can be seen by all other classes in your program. Also a variable declared there will exist for the duration of your program.

Code for Form1
Public Class Form1
Dim frm As Form2 'Our instance of form2
'Add button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newItem As String = TextBox1.Text
items.Add(newItem) 'Add new item to our list

'If Form2 exists then add the item to it's listbox
'If form1 and form2 arent' open at the same time then the next line isn't necessary
If frm IsNot Nothing AndAlso Not frm.IsDisposed Then frm.ListBox1.Items.Add(newItem)
End Sub

'Next button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'If we haven't created Form2 or it's been closed then create a new instance
If frm Is Nothing OrElse frm.IsDisposed Then
frm = New Form2 'Create new instance

'This next line was moved to Form2
'frm.ListBox1.Items.AddRange(items.ToArray) 'Add items to listbox1 on form2

frm.Show() 'Show the form
End If
End Sub

End Class

Code for form2
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ListBox1.Items.AddRange(items.ToArray)
End Sub
End Class

Code for Module1
Module Module1
Public items As New List(Of String) 'Collection of items
End Module
TwoFaced at 2007-11-11 20:53:46 >
# 8 Re: adding an item to a list on a different form
Don't think the above would work.

When I go to form 2, form 1 hides so that it is not shown on the screen. when i go back to form 1, form 2 hides.
MonaVohra at 2007-11-11 20:54:51 >
# 9 Re: adding an item to a list on a different form
Don't think the above would work.

When I go to form 2, form 1 hides so that it is not shown on the screen. when i go back to form 1, form 2 hides.It would work, in fact if you are just hiding the forms the fist solution I gave you will work. The problem I feel is how your displaying form1 again. Please post the code from form2 that goes back to form1. I have a feeling you are creating a new instance of form1 instead of showing the already existing instance.
TwoFaced at 2007-11-11 20:55:49 >
# 10 Re: adding an item to a list on a different form
this is my code on form 2:

Imports SpeechLib
Public Class CategoryList
Dim answer

Private Sub CategoryList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.AddRange(items.ToArray)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PointsOfInterestCategory.Show()
Me.Hide()
' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

End Sub
End Class
MonaVohra at 2007-11-11 20:56:46 >
# 11 Re: adding an item to a list on a different form
Sorry, but I don't see any code in your button event to show your first form. All you did was hide the second form. If you hid the first form when this one was shown you would end up with two hidden forms. Maybe I'm missing something but posting the code in the first form might help me see the bigger picture.
TwoFaced at 2007-11-11 20:57:52 >
# 12 Re: adding an item to a list on a different form
my first form is called PointsOfInterestCategory. this is shown in the button event on form 2 as PointsOfInterestCategory.Show()
MonaVohra at 2007-11-11 20:58:56 >
# 13 Re: adding an item to a list on a different form
This is the code for my first form:

Imports SpeechLib
Public Class PointsOfInterestCategory

Dim frm As CategoryList 'Our instance of form2
Dim items As New List(Of String) 'Internal collection of items
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub PointsOfInterestCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form1.Hide()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim newItem As String = TextBox1.Text
items.Add(newItem) 'Add new item to our list

'If Form2 exists then add the item to it's listbox
If frm IsNot Nothing AndAlso Not frm.IsDisposed Then frm.ListBox1.Items.Add(newItem)

' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Form1.Show()
Me.Hide()
' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
CategoryList.Show()
Me.Hide()

Dim newItem As String = TextBox1.Text
items.Add(newItem) 'Add new item to our list

'If Form2 exists then add the item to it's listbox
'If form1 and form2 arent' open at the same time then the next line isn't necessary
If frm IsNot Nothing AndAlso Not frm.IsDisposed Then frm.ListBox1.Items.Add(newItem)

frm.Show() 'show the form

' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub

End Class
MonaVohra at 2007-11-11 20:59:58 >
# 14 Re: adding an item to a list on a different form
It seems you've got my code a little jumbled. I'm not sure if you just didn't add it in correctly of if you've tried to make changes to get it to work, but it's not correct. As it is now you should be getting an error when you click button3. Also I can't tell which button is supposed to add the names to the list becuase both button3 and button1 do that. I've tried to make the appropriate fixes. Assuming I guessed correctly on which button does what this code work now.

Code for PointsOfInterestCategory form
Imports SpeechLib

Public Class PointsOfInterestCategory

Dim frm As CategoryList 'Our instance of form2
Dim items As New List(Of String) 'Internal collection of items

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub PointsOfInterestCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form1.Hide()
End Sub

'This should be the button that adds items. If that's not the case
'this code should go in that button.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim newItem As String = TextBox1.Text
items.Add(newItem) 'Add new item to our list

'If Form2 exists then add the item to it's listbox
If frm IsNot Nothing AndAlso Not frm.IsDisposed Then frm.ListBox1.Items.Add(newItem)

' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Form1.Show()
Me.Hide()
' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

'This looks like it should be the button that shows the other form
'The only changes I made were in this method.
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Me.Hide()

If frm Is Nothing OrElse frm.IsDisposed Then
frm = New CategoryList
frm.ListBox1.Items.AddRange(items.ToArray)
frm.Show()
Else
frm.Visible = True
End If

'' sender parameter is the control that triggered
'' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

'' Now pass the button's Text property to the
'' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub
End Class

Code for CatagoryList form
Imports SpeechLib
Public Class CategoryList
Dim answer

Private Sub CategoryList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'The next line isn't necessary anymore
'You should remove this code.
'ListBox1.Items.AddRange(items.ToArray)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PointsOfInterestCategory.Show()
Me.Hide()
'' sender parameter is the control that triggered
'' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

'' Now pass the button's Text property to the
'' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

End Sub
End Class
TwoFaced at 2007-11-11 21:00:58 >
# 15 Re: adding an item to a list on a different form
that works - great! thanks. yeh i was trying to work around with your code. what it was doing was adding the item the first time round but when i closed the 2nd form to go back to the 1st form, and do the process again nothing would be added to the list. this is great - cheers! :)
MonaVohra at 2007-11-11 21:01:53 >