From 1aa83b7c1243ad7878bb5653582295c09dcd31ec Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Wed, 26 Mar 2025 05:31:29 +1000 Subject: [PATCH] Add and use `ComboBox.ReplaceItems` extension --- .../Extensions/ControlExtensions.cs | 12 ++++++++-- .../config/N64/N64VideoPluginconfig.cs | 4 +--- .../config/NES/NESSyncSettingsForm.cs | 2 +- .../config/NES/NesControllerSettings.cs | 7 +++--- .../tools/Cheats/CheatEdit.cs | 8 ++----- .../tools/Debugger/GenericDebugger.cs | 3 +-- .../tools/Lua/LuaDropDown.cs | 6 ++--- .../tools/Watch/WatchEditor.cs | 23 ++++--------------- 8 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs index 23563aab22..d0d8f79291 100644 --- a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs +++ b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs @@ -27,8 +27,7 @@ namespace BizHawk.Client.EmuHawk public static void PopulateFromEnum(this ComboBox box, T enumVal) where T : Enum { - box.Items.Clear(); - box.Items.AddRange(typeof(T).GetEnumDescriptions().Cast().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 items) + => dropdown.ReplaceItems(items: items.ToArray()); } public static class ListViewExtensions diff --git a/src/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs b/src/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs index d1dbf86d9c..b1934ecea5 100644 --- a/src/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs @@ -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().ToArray()); + VideoResolutionComboBox.ReplaceItems(items: ValidResolutions); // If the given resolution is in the table, pick it. // Otherwise find a best fit diff --git a/src/BizHawk.Client.EmuHawk/config/NES/NESSyncSettingsForm.cs b/src/BizHawk.Client.EmuHawk/config/NES/NESSyncSettingsForm.cs index c6ea8dbd7e..6f62b94dfd 100644 --- a/src/BizHawk.Client.EmuHawk/config/NES/NESSyncSettingsForm.cs +++ b/src/BizHawk.Client.EmuHawk/config/NES/NESSyncSettingsForm.cs @@ -46,7 +46,7 @@ namespace BizHawk.Client.EmuHawk InfoLabel.Visible = true; } - RegionComboBox.Items.AddRange(Enum.GetNames(typeof(NES.NESSyncSettings.Region)).Cast().ToArray()); + RegionComboBox.ReplaceItems(items: Enum.GetNames(typeof(NES.NESSyncSettings.Region))); RegionComboBox.SelectedItem = Enum.GetName(typeof(NES.NESSyncSettings.Region), _syncSettings.RegionOverride); var initWRAMPattern = _syncSettings.InitialWRamStatePattern; diff --git a/src/BizHawk.Client.EmuHawk/config/NES/NesControllerSettings.cs b/src/BizHawk.Client.EmuHawk/config/NES/NesControllerSettings.cs index 4de6b12d22..34a504c2e3 100644 --- a/src/BizHawk.Client.EmuHawk/config/NES/NesControllerSettings.cs +++ b/src/BizHawk.Client.EmuHawk/config/NES/NesControllerSettings.cs @@ -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().ToArray()); - comboBoxNESL.Items.AddRange(NESControlSettings.GetNesPortValues().Cast().ToArray()); - comboBoxNESR.Items.AddRange(NESControlSettings.GetNesPortValues().Cast().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; diff --git a/src/BizHawk.Client.EmuHawk/tools/Cheats/CheatEdit.cs b/src/BizHawk.Client.EmuHawk/tools/Cheats/CheatEdit.cs index 1d83535f48..ce27a1f963 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Cheats/CheatEdit.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Cheats/CheatEdit.cs @@ -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() diff --git a/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs b/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs index a0cfff101b..bc1a904c29 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs @@ -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().ToArray()); + c.ReplaceItems(items: Disassembler.AvailableCpus); c.SelectedItem = Disassembler.Cpu; c.SelectedIndexChanged += OnCpuDropDownIndexChanged; return c; diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaDropDown.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaDropDown.cs index 98e3ae72b8..3687a433d4 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaDropDown.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaDropDown.cs @@ -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 items) { - Items.AddRange(items.Cast().ToArray()); + this.ReplaceItems(items: items); SelectedIndex = 0; DropDownStyle = ComboBoxStyle.DropDownList; } public void SetItems(ICollection items) { - Items.Clear(); - Items.AddRange(items.Cast().ToArray()); + this.ReplaceItems(items: items); SelectedIndex = 0; } } diff --git a/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs b/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs index 0be7774f76..23bee52e2e 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs @@ -235,12 +235,7 @@ namespace BizHawk.Client.EmuHawk } _mode = mode; - - DomainDropDown.Items.Clear(); - DomainDropDown.Items.AddRange(MemoryDomains - .Select(d => d.ToString()) - .Cast() - .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; }