Search code examples
c#winformscomboboxbass

Combo Box Selected Index Property .NET C#


I'm toying around with BASS .NET and I'm having trouble figuring out how to get the Combo box in my project to return an integer when the SelectedIndexChanged event occurs. perhaps I'm missing something and it is, or some part of my code is unreachable.

my basic objective is to populate the list box with driver names based on which driver type is selected.

anyway I've been at it all day and have no idea what's happening. any advice is much appreciated

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//BASS references
using Un4seen.Bass;
using Un4seen.BassAsio;
using Un4seen.Bass.AddOn.Midi;

namespace VibeCraft
{
    public partial class vcSettings : Form
{
    BASS_DEVICEINFO dvInfo;
    public vcSettings()
    {
        InitializeComponent();
    }
    public void SetDriverType()
    {
        int driverList = Bass.BASS_GetDeviceCount();
        switch (cmbDrvType.SelectedIndex)
            {
                    //Clear Names when No sound is selected
                case 0:
                    for (int i = driverList; i >= 0; i--)
                    {
                        lstDriverList.Items.Remove(driverList);
                    }
                    break;
                    //get Device names for DirecX Driver type
                case 1:
                    for (int i = driverList; i < Bass.BASS_GetDeviceCount(); i++)
                    {
                        dvInfo = Bass.BASS_GetDeviceInfo(i);
                        Bass.BASS_GetDeviceInfo(i, dvInfo);
                        lstDriverList.Items.Add(dvInfo.name);
                    }
                    break;
                    //Get Device names for ASIO Driver type
                case 2:
                    //TODO:
                    //logic for populating the ASIO driver list.
                    break;

                default:
                    cmbDrvType.SelectedIndex = 1;
                    break;
            }
        }

        private void cmbDrvType_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetDriverType();
        }
    }
}

thanks for all your support, I figured out where I went wrong, in the for loop where I was incrementing my index, I began with a value that was equal to the sentinel value. The driverList value was already at 3, and so it was not incrementing because that was what I was comparing it against. Here is my updated code.

    public void SetDriverType(int selectedType)
    {
        int driverList = Bass.BASS_GetDeviceCount();
        switch (selectedType)
        {
                //Clear Names when No sound is selected
            case 0:
                for (int i = driverList; i >= 0; i--)
                {
                    lstDriverList.Items.Remove(driverList);
                }
                break;
                //get Device names for DirecX Driver type
            case 1:
                for (int i = 0; i < Bass.BASS_GetDeviceCount(); i++)
                {
                    dvInfo = Bass.BASS_GetDeviceInfo(i);
                    //Bass.BASS_GetDeviceInfo(i, dvInfo);
                    lstDriverList.Items.Add(dvInfo.name);
                }
                break;
                //Get Device names for ASIO Driver type
            case 2:
                //TODO:
                //logic for populating the ASIO driver list.
                break;
        }

Solution

  • I think is better to use an int variable inside the IndexChange to retrieve the index value and then pass it to your SetDriverType() Method. Try that and let me know how it woked.

    private void cmbDrvType_SelectedIndexChanged(object sender, EventArgs e)
    {
         SetDriverType((int)cmbDrvType.SelectedIndex);
    }