Add and use `ComboBox.ReplaceItems` extension

This commit is contained in:
YoshiRulz 2025-03-26 05:31:29 +10:00
parent e8df779cc7
commit 1aa83b7c12
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
8 changed files with 24 additions and 41 deletions

View File

@ -27,8 +27,7 @@ namespace BizHawk.Client.EmuHawk
public static void PopulateFromEnum<T>(this ComboBox box, T enumVal)
where T : Enum
{
box.Items.Clear();
box.Items.AddRange(typeof(T).GetEnumDescriptions().Cast<object>().ToArray());
box.ReplaceItems(items: typeof(T).GetEnumDescriptions());
box.SelectedItem = enumVal.GetDescription();
}
@ -179,6 +178,15 @@ namespace BizHawk.Client.EmuHawk
menu.DropDownItems.Clear();
menu.DropDownItems.AddRange(items);
}
public static void ReplaceItems(this ComboBox dropdown, params object[] items)
{
dropdown.Items.Clear();
dropdown.Items.AddRange(items);
}
public static void ReplaceItems(this ComboBox dropdown, IEnumerable<object> items)
=> dropdown.ReplaceItems(items: items.ToArray());
}
public static class ListViewExtensions

View File

@ -1,4 +1,3 @@
using System.Linq;
using System.Windows.Forms;
using BizHawk.Common.StringExtensions;
@ -183,8 +182,7 @@ namespace BizHawk.Client.EmuHawk
}
// Change resolution list to the rest
VideoResolutionComboBox.Items.Clear();
VideoResolutionComboBox.Items.AddRange(ValidResolutions.Cast<object>().ToArray());
VideoResolutionComboBox.ReplaceItems(items: ValidResolutions);
// If the given resolution is in the table, pick it.
// Otherwise find a best fit

View File

@ -46,7 +46,7 @@ namespace BizHawk.Client.EmuHawk
InfoLabel.Visible = true;
}
RegionComboBox.Items.AddRange(Enum.GetNames(typeof(NES.NESSyncSettings.Region)).Cast<object>().ToArray());
RegionComboBox.ReplaceItems(items: Enum.GetNames(typeof(NES.NESSyncSettings.Region)));
RegionComboBox.SelectedItem = Enum.GetName(typeof(NES.NESSyncSettings.Region), _syncSettings.RegionOverride);
var initWRAMPattern = _syncSettings.InitialWRamStatePattern;

View File

@ -1,4 +1,3 @@
using System.Linq;
using System.Windows.Forms;
using BizHawk.Emulation.Common;
@ -20,9 +19,9 @@ namespace BizHawk.Client.EmuHawk
Icon = Properties.Resources.GameControllerIcon;
// TODO: use combobox extension and add descriptions to enum values
comboBoxFamicom.Items.AddRange(NESControlSettings.GetFamicomExpansionValues().Cast<object>().ToArray());
comboBoxNESL.Items.AddRange(NESControlSettings.GetNesPortValues().Cast<object>().ToArray());
comboBoxNESR.Items.AddRange(NESControlSettings.GetNesPortValues().Cast<object>().ToArray());
comboBoxFamicom.ReplaceItems(items: NESControlSettings.GetFamicomExpansionValues());
comboBoxNESL.ReplaceItems(items: NESControlSettings.GetNesPortValues());
comboBoxNESR.ReplaceItems(items: NESControlSettings.GetNesPortValues());
comboBoxFamicom.SelectedItem = _syncSettings.Controls.FamicomExpPort;
comboBoxNESL.SelectedItem = _syncSettings.Controls.NesLeftPort;

View File

@ -38,12 +38,8 @@ namespace BizHawk.Client.EmuHawk
{
return;
}
DomainDropDown.Items.Clear();
DomainDropDown.Items.AddRange(MemoryDomains
.Where(d => d.Writable)
.Select(d => (object) d.ToString())
.ToArray());
DomainDropDown.ReplaceItems(items: MemoryDomains.Where(static d => d.Writable)
.Select(static d => d.ToString()));
DomainDropDown.SelectedItem = MemoryDomains.HasSystemBus
? MemoryDomains.SystemBus.ToString()

View File

@ -1,5 +1,4 @@
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using BizHawk.Client.Common;
@ -60,7 +59,7 @@ namespace BizHawk.Client.EmuHawk
Location = new(UIHelper.ScaleX(35), UIHelper.ScaleY(17)),
Width = UIHelper.ScaleX(121),
};
c.Items.AddRange(Disassembler.AvailableCpus.Cast<object>().ToArray());
c.ReplaceItems(items: Disassembler.AvailableCpus);
c.SelectedItem = Disassembler.Cpu;
c.SelectedIndexChanged += OnCpuDropDownIndexChanged;
return c;

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace BizHawk.Client.EmuHawk
@ -8,15 +7,14 @@ namespace BizHawk.Client.EmuHawk
{
public LuaDropDown(ICollection<string> items)
{
Items.AddRange(items.Cast<object>().ToArray());
this.ReplaceItems(items: items);
SelectedIndex = 0;
DropDownStyle = ComboBoxStyle.DropDownList;
}
public void SetItems(ICollection<string> items)
{
Items.Clear();
Items.AddRange(items.Cast<object>().ToArray());
this.ReplaceItems(items: items);
SelectedIndex = 0;
}
}

View File

@ -235,12 +235,7 @@ namespace BizHawk.Client.EmuHawk
}
_mode = mode;
DomainDropDown.Items.Clear();
DomainDropDown.Items.AddRange(MemoryDomains
.Select(d => d.ToString())
.Cast<object>()
.ToArray());
DomainDropDown.ReplaceItems(items: MemoryDomains.Select(static d => d.ToString()));
DomainDropDown.SelectedItem = domain.ToString();
SetTitle();
@ -272,27 +267,17 @@ namespace BizHawk.Client.EmuHawk
private void SetDisplayTypes()
{
string oldType = DisplayTypeDropDown.Text;
DisplayTypeDropDown.Items.Clear();
switch (SizeDropDown.SelectedIndex)
{
default:
case 0:
foreach (WatchDisplayType t in ByteWatch.ValidTypes)
{
DisplayTypeDropDown.Items.Add(Watch.DisplayTypeToString(t));
}
DisplayTypeDropDown.ReplaceItems(items: ByteWatch.ValidTypes.Select(Watch.DisplayTypeToString));
break;
case 1:
foreach (WatchDisplayType t in WordWatch.ValidTypes)
{
DisplayTypeDropDown.Items.Add(Watch.DisplayTypeToString(t));
}
DisplayTypeDropDown.ReplaceItems(items: WordWatch.ValidTypes.Select(Watch.DisplayTypeToString));
break;
case 2:
foreach (WatchDisplayType t in DWordWatch.ValidTypes)
{
DisplayTypeDropDown.Items.Add(Watch.DisplayTypeToString(t));
}
DisplayTypeDropDown.ReplaceItems(items: DWordWatch.ValidTypes.Select(Watch.DisplayTypeToString));
break;
}