How do you add items to a WPF Combobox from a module in VB.net ?

Hi I have been moving some code from a single page winfom app into a WPF app written in VB.Net.

One of the code sections in my module that previously lived in the single form app is giving me problems, How do I populate a combo box with items from my module, I tried making the devicecombo shared but run into other issues.


 
Public Sub _session_SearchComplete(sender As Object, e As EventArgs)
        ' Cursor.Current = Cursors.[Default]


        ' populate devices
        Dim devices As Device() = _currentSession.GetDevices()
        If devices Is Nothing OrElse devices.Length = 0 Then
            MessageBox.Show("No devices found")
            Return
        Else
            ' note - this finds ALL bluetooth devices
            MessageBox.Show([String].Format("Found {0} device(s)", devices.Length))
        End If

        connectform.devicecombo.Items.Clear()

        For Each device As Device In devices
            ' the ToString function for Device is overridden to return the name
            connectform.devicecombo.Items.Add(device)
        Next
    End Sub

Open in new window


Thanks
rhill52Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
If you want to build it properly, it shouldn't be the module that feeds the combo. It should be the page containing the combo querying a property or a method in your class/module to fill the combo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rhill52Author Commented:
Thanks Eric how would you populate a collection and then reference in the other form replacing this line ?

connectform.devicecombo.Items.Add(device)
0
Éric MoreauSenior .Net ConsultantCommented:
first create a method that returns a list of string or devices in your module

second, if you have a list of string, set the datasource property of the combo in your wpf page OR if you have a list of devices, move your loop to your page.

The module should not be aware of the controls on any form/page.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rhill52Author Commented:
ok I can populate the combo correctly now  but the issue is now how to set this code needed in the module ?

 Public ReadOnly Property SelectedIDBlueDevice() As Device
        Get
            Dim index As Integer = devicecombo.SelectedIndex

            If index < 0 OrElse index >= devicecombo.Items.Count Then
                Return Nothing
            Else
               [b] Return TryCast(devicecombo.Items(index), Device)[/b]
            End If
        End Get


    End Property

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
The module should not be using any controls on any forms. All the code referencing controls needs to be in the forms/pages.

Then you start building methods that receives parameters in your modules/classes and you call these methods from your forms/pages.
0
rhill52Author Commented:
Thanks but I need an example of how to do this, I understand it is the wrong way around now.
0
Éric MoreauSenior .Net ConsultantCommented:
It is very clear that SelectedIDBlueDevice property all belongs to your WPF Page. All the lines are trying to access the combo.

Then what do you do with the Device? You will need something like this on your page:
YourModule.YourMethodNameAcceptingADeviceInArgument(SelectedIDBlueDevice)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.