share data between 2 combo boxes in 2 different forms

JohnDW

Well-known member
Joined
Jun 13, 2012
Messages
60
Location
Antwerp, Belgium
Programming Experience
1-3
Dear forummembers,

I have a Form1 with a combobox1 and knop1.
I have a Form2 with combobox2 and knop2.
combobox1 shows values ​​that can be selected.
when the button is pressed from Form1 I want Form2 opens with the
combobox2 the selected value from combobox1.
knop2 when pressed on the same happens with combox1 from Form1.

I've looked on the web, but couldn't find a solution.

Suggestions? How can I handle this?

Txs.

John
 
VB.NET:
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Form2.ComboBox1.SelectedItem = Me.ComboBox1.SelectedItem
        Form2.Show()
    End Sub
End Class

VB.NET:
Public Class Form2
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Form1.ComboBox1.SelectedItem = Me.ComboBox1.SelectedItem
        Me.Close()
    End Sub
End Class
 
It does work. I didn't just type it out of my head, I tested it! If it doesn't work in your project then it's either because you didn't apply it correctly or there are other factors that are affecting it. Had you bothered to include your version of the code rather than sitting there with your mouth open expecting another spoon of soup I might be able to determine that!
 
Maybe the reason Dunfiddlin's suggestion doesn't work on JohnDW project is because it assumes ComboBox1 and ComboBox2 have the same identical datasource, so that they have equally typed items as children, and one's .SelectedItem underlying object could be assigned to the other's .SelectedItem. Maybe replacing "Form2.ComboBox1.SelectedItem = Me.ComboBox1.SelectedItem" with "Form2.ComboBox1.SelectedValue = Me.ComboBox1.SelectedValue" would make it, but only if both comboboxes have its .ValueMember properties set to the same column of datasource. I hope it helps.
 
Yes, I did assume that the conboboxes were identical. It didn't make a lot of sense as an idea if they weren't but I suppose you gotta learn to expect the unexpected around these parts! :idea:
 
Back
Top