|
-
Nov 28th, 2004, 11:48 AM
#1
Thread Starter
Addicted Member
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.
-
Nov 28th, 2004, 01:24 PM
#2
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:
'NOTE: Set your startup object to Sub Main
Module Module1
Public goForm1 As New Form1
Public goForm2 As New Form2
Public Sub Main()
goForm1.ShowDialog()
End Sub
End Module
Public Class Form1
Inherits System.Windows.Forms.Form
[color=silver]"Windows Form Designer generated code"[/color]
' Form1 button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
goForm1.Hide()
goForm2.ShowDialog()
End Sub
End Class
Public Class Form2
Inherits System.Windows.Forms.Form
[color=silver]"Windows Form Designer generated code"[/color]
' Form2 button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
goForm1.ComboBox1.Items.Add(Me.ComboBox2.Text)
goForm2.Dispose()
goForm1.Show()
End Sub
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 28th, 2004, 09:06 PM
#3
Thread Starter
Addicted Member
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.
-
Nov 28th, 2004, 09:09 PM
#4
Hyperactive Member
here's what i got.
in Form1
VB Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2()
Me.AddOwnedForm(f)
f.ShowDialog()
End Sub
in Form2
VB Code:
Dim f As Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
f = Me.Owner
f.ComboBox1.Items.Add(Me.ComboBox1.Text)
Close()
End Sub
hope that helps.
-
Nov 28th, 2004, 10:42 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 29th, 2004, 01:47 AM
#6
Thread Starter
Addicted Member
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.
-
Nov 29th, 2004, 01:58 AM
#7
Hyperactive Member
hi toytoy
you replace with this:
VB Code:
f = CType(Me.Owner, Form1)
-
Nov 29th, 2004, 02:15 AM
#8
Thread Starter
Addicted Member
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.
-
Nov 29th, 2004, 02:22 AM
#9
Hyperactive Member
remove the "Close()" statment and make a button that will trigger as your close event something like that.
-
Nov 29th, 2004, 02:27 AM
#10
Thread Starter
Addicted Member
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.
-
Nov 29th, 2004, 02:31 AM
#11
Hyperactive Member
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:
[COLOR=blue]' Form1 button [/COLOR]
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
[COLOR=blue]' Form2 button [/COLOR]
Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
Me.Close()
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?
-
Nov 29th, 2004, 02:35 AM
#12
Thread Starter
Addicted Member
yeah....
if can at the same time can minimize Form2 and view Form1, that will be much better...
-
Nov 29th, 2004, 02:40 AM
#13
Hyperactive Member
this one?
in Form1:
VB Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As Form
For Each f In Me.OwnedForms
If f.Name = "Form2" Then
f.Activate()
Exit Sub
End If
Next
f = New Form2()
Me.AddOwnedForm(f)
f.Show()
End Sub
in the Form2:
VB Code:
Dim f As Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
f = CType(Me.Owner, Form1)
f.ComboBox1.Items.Add(Me.ComboBox1.Text)
End Sub
'close button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
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.
-
Nov 29th, 2004, 02:54 AM
#14
Thread Starter
Addicted Member
thanks fret....
It is something that i want...
like you say though it is not perfect, it can add my stuff...
you are great.
-
Nov 29th, 2004, 02:56 AM
#15
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|