Windows窗体组合框的默认值?

Tho*_*mas 1 combobox winforms

我有一个包含不同声音值的组合框.每当我选择不同的值时,播放器就会播放一次声音.

然而问题是当我启动程序时,在加载表单时,我在组合框中加载包含先前存储的声音值的设置文件,我使用组合框的SelectedIndex属性作为默认值.

使用SelectedIndex会导致播放器在程序启动时播放一次声音,这在某种程度上是奇怪的.

任何想法如何选择除selectedIndex之外的默认值?由于selectedIndex还运行该特定值的后端代码.

pun*_*r76 5

假设播放声音的代码位于SelectedIndexChanged事件处理程序方法中,解决方案是在设置默认选定索引仅将该处理程序方法附加到事件.

例如:

private void DoLoad()
{
  // Set the data source, and the default selection
  cbox.DataSource = YourDataSource;
  cbox.SelectedIndex = YourLastSelIndex;

  // Then attach the event handler method.
  cbox.SelectedIndexChanged += YourSelectedIndexChanged;
}
Run Code Online (Sandbox Code Playgroud)