adding an item to a list on a different form


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: adding an item to a list on a different form

  1. #1
    Join Date
    Feb 2007
    Posts
    58

    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?

  2. #2
    Join Date
    Jan 2007
    Posts
    112
    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.

  3. #3
    Join Date
    Feb 2007
    Posts
    58
    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

  4. #4
    Join Date
    Jan 2007
    Posts
    112
    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.
    Code:
    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

  5. #5
    Join Date
    Feb 2007
    Posts
    58
    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

  6. #6
    Join Date
    Jan 2007
    Posts
    112
    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.

  7. #7
    Join Date
    Feb 2007
    Posts
    58
    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

  8. #8
    Join Date
    Jan 2007
    Posts
    112
    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
    Code:
    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
    Code:
    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
    Code:
    Module Module1
        Public items As New List(Of String)    'Collection of items
    End Module
    Last edited by TwoFaced; 02-25-2007 at 03:25 PM.

  9. #9
    Join Date
    Feb 2007
    Posts
    58
    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.

  10. #10
    Join Date
    Jan 2007
    Posts
    112
    Quote Originally Posted by MonaVohra
    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.

  11. #11
    Join Date
    Feb 2007
    Posts
    58
    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

  12. #12
    Join Date
    Jan 2007
    Posts
    112
    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.

  13. #13
    Join Date
    Feb 2007
    Posts
    58
    my first form is called PointsOfInterestCategory. this is shown in the button event on form 2 as PointsOfInterestCategory.Show()

  14. #14
    Join Date
    Feb 2007
    Posts
    58
    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

  15. #15
    Join Date
    Jan 2007
    Posts
    112
    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
    Code:
    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
    Code:
    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

Similar Threads

  1. dynamically adding controls to form
    By arunak_1988 in forum VB Classic
    Replies: 1
    Last Post: 11-06-2006, 03:28 AM
  2. Adding a link to a form
    By jconan in forum VB Classic
    Replies: 5
    Last Post: 03-20-2006, 10:15 PM
  3. Adding on to a ComboBox list
    By miss_jess in forum VB Classic
    Replies: 12
    Last Post: 05-31-2005, 09:33 AM
  4. Replies: 2
    Last Post: 01-26-2001, 10:42 AM
  5. Draw each item in list box
    By Mohamad Ali in forum VB Classic
    Replies: 2
    Last Post: 10-17-2000, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center