Link to home
Start Free TrialLog in
Avatar of rhill52
rhill52

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rhill52
rhill52

ASKER

Thanks Eric how would you populate a collection and then reference in the other form replacing this line ?

connectform.devicecombo.Items.Add(device)
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.
Avatar of rhill52

ASKER

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

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.
Avatar of rhill52

ASKER

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