In a WinForms application I am showing a list of all available audio devices in a combo box. I want the user to be able to select one of these items and for that selection to be persisted so that the next time the application is loaded the same device from combo box will selected by default.
How can I solve this?
Code to get list of available audio devices:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form2 : Form
{
[DllImport("winmm.dll", SetLastError = true)]
static extern uint waveInGetNumDevs();
[DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint waveInGetDevCaps(uint hwo, ref WAVEOUTCAPS pwoc, uint cbwoc);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WAVEOUTCAPS
{
public ushort wMid;
public ushort wPid;
public uint vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string szPname;
public uint dwFormats;
public ushort wChannels;
public ushort wReserved1;
public uint dwSupport;
}
public void FillSoundDevicesInCombobox()
{
uint devices = waveInGetNumDevs();
WAVEOUTCAPS caps = new WAVEOUTCAPS();
for (uint i = 0; i < devices; i++)
{
waveInGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps));
CB1.Items.Add(caps.szPname);
}
}
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
FillSoundDevicesInCombobox();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
My expectation is that when the user clicks on the button the selected item will be saved. Then if I restart the application, the same device should be re-selected when the form loads.
