Results 1 to 15 of 15

Thread: VB.NET: Adding Items to ComboBox from previous form....[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Adding Items to ComboBox from previous form....[Resolved]

    Say i have Form1 with comboBox1 and Form2 with comboBox2..

    When i click on Form1 button, Form2 will appear on top of Form1...

    My Code:
    Code:
    ' Form1 button 
     Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
            Dim form2 As New Form2
            form2.ShowDialog()
        End Sub
    How to add the items from Form2 's comboBox to Form1 's comboBox...

    My Code:
    Code:
    ' Form2 button 
      Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
           Dim form1 As New Form1
           form1.comboBox1.Items.Add(Me.comboBox2.Text)
        End Sub
    These code will not have any effect on comboBox1 in Form1..

    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 07:43 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    You are not seeing combo2 being added to combo1 because
    your creating another instance of form1 from the form2 button
    click event (which isnt visible).

    So, to correct this we need to add a standard module to the
    project as the start up procedure of Sub Main. This is where we
    will create our instances of Form1 and Form2. Then when you go
    to Form2 its already instanciated so you just need to show it.

    When you go back to Form1 you also just show it and we will be
    dealing with one instance of each form all the way through.


    VB Code:
    1. 'NOTE: Set your startup object to Sub Main
    2.  
    3. Module Module1
    4.  
    5.     Public goForm1 As New Form1
    6.     Public goForm2 As New Form2
    7.  
    8.     Public Sub Main()
    9.  
    10.         goForm1.ShowDialog()
    11.  
    12.     End Sub
    13.  
    14. End Module
    15.  
    16.  
    17. Public Class Form1
    18.     Inherits System.Windows.Forms.Form
    19.  
    20. [color=silver]"Windows Form Designer generated code"[/color]
    21.  
    22.     ' Form1 button
    23.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    24.         goForm1.Hide()
    25.         goForm2.ShowDialog()
    26.     End Sub
    27.  
    28. End Class
    29.  
    30.  
    31. Public Class Form2
    32.     Inherits System.Windows.Forms.Form
    33.  
    34. [color=silver]"Windows Form Designer generated code"[/color]
    35.  
    36.     ' Form2 button
    37.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    38.         goForm1.ComboBox1.Items.Add(Me.ComboBox2.Text)
    39.         goForm2.Dispose()
    40.         goForm1.Show()
    41.     End Sub
    42.  
    43. End Class
    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    thanks it works....

    but if i click on another button to go to Form3...

    Form1 and Form2 cannot closed..

    and it gives an error :
    Code:
    An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll
    
    Additional information: Cannot access a disposed object named "goToForm2".
    Code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    "Windows Form Designer generated code"
    
        ' Form1 button 
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            goForm1.Hide()
            goForm2.ShowDialog() 'The error point here 
        End Sub
    End Class
    Last edited by toytoy; Dec 3rd, 2004 at 07:43 AM.

  4. #4
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    here's what i got.
    in Form1
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim f As New Form2()
    3.         Me.AddOwnedForm(f)
    4.         f.ShowDialog()
    5.     End Sub
    in Form2
    VB Code:
    1. Dim f As Form1
    2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.         f = Me.Owner
    4.         f.ComboBox1.Items.Add(Me.ComboBox1.Text)
    5.         Close()
    6.     End Sub
    hope that helps.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    fret's code is simpler. Try it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    it could works....

    but if only i turn OFF the option strict...

    I am working on the codes where option strict is ON...

    The error happen here:
    Code:
    ' Inside Form2 
        Dim f As Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            f = Me.Owner 'The error is on this line 
            f.ComboBox1.Items.Add(Me.ComboBox1.Text)
            Close()
        End Sub
    any other ideas how to deal with it.....
    Last edited by toytoy; Dec 3rd, 2004 at 07:44 AM.

  7. #7
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    hi toytoy
    you replace with this:
    VB Code:
    1. f = CType(Me.Owner, Form1)

  8. #8

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    thanks.....it works

    do you know how not to close the form2 immediately after i enter some information....

    that means i can choose not to close form2....and keep on adding instead of adding only once and have to click the form2 button again...
    Last edited by toytoy; Nov 29th, 2004 at 02:19 AM.

  9. #9
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    remove the "Close()" statment and make a button that will trigger as your close event something like that.

  10. #10

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    I found another way of doing.....but it will still close Form2

    I can only add it once..

    My code:
    Code:
    ' Form1 button  
     Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
         Dim form2 As New Form2 
         form2.ShowDialog() 
         Me.ComboBox1.Items.Add(form2.ComboBox2.Text) 
         form2.Dispose() 
     End Sub
    
    ' Form2 button  
      Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
          Me.Close() 
      End Sub
    Last edited by toytoy; Dec 3rd, 2004 at 07:44 AM.

  11. #11
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    Originally posted by toytoy
    I found another way of doing.....but it will still close Form2

    I can only add it once..

    My code:
    VB Code:
    1. [COLOR=blue]' Form1 button [/COLOR]
    2.  Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
    3.       Dim form2 As New Form2
    4.      form2.ShowDialog()
    5.      Me.ComboBox1.Items.Add(form2.ComboBox2.Text)
    6.       form2.Dispose()
    7.  End Sub
    8.  
    9. [COLOR=blue]' Form2 button [/COLOR]
    10.   Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
    11.       Me.Close()
    12.   End Sub
    you mean, you want to click how many times on button in form2 and add in the form1's combobox? is that what you trying to do?

  12. #12

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    yeah....

    if can at the same time can minimize Form2 and view Form1, that will be much better...

  13. #13
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    this one?
    in Form1:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim f As Form
    3.         For Each f In Me.OwnedForms
    4.             If f.Name = "Form2" Then
    5.                 f.Activate()
    6.                 Exit Sub
    7.             End If
    8.         Next
    9.         f = New Form2()
    10.         Me.AddOwnedForm(f)
    11.         f.Show()
    12.     End Sub
    in the Form2:
    VB Code:
    1. Dim f As Form1
    2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.         f = CType(Me.Owner, Form1)
    4.         f.ComboBox1.Items.Add(Me.ComboBox1.Text)
    5.     End Sub
    6.     'close button
    7.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    8.         Close()
    9.     End Sub
    this is not perfect of what you seek but it's close, you can do add your stuff or whatever.
    Last edited by fret; Nov 29th, 2004 at 02:43 AM.

  14. #14

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    thanks fret....

    It is something that i want...

    like you say though it is not perfect, it can add my stuff...

    you are great.

  15. #15
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    no probz toytoy i myself also needs also some advice, but anyway happy coding.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width