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

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")
            ' note - this finds ALL bluetooth devices
            MessageBox.Show([String].Format("Found {0} device(s)", devices.Length))
        End If


        For Each device As Device In devices
            ' the ToString function for Device is overridden to return the name
    End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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 ?

É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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
            Dim index As Integer = devicecombo.SelectedIndex

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

    End Property

Open in new window

É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.
rhill52Author Commented:
Thanks but I need an example of how to do this, I understand it is the wrong way around now.
É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:

Open in new window

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.