还原 "ComboBox 绑定数据"

This commit is contained in:
ChsBuffer
2020-08-25 04:49:58 +08:00
parent 41751db06f
commit b061a00057
8 changed files with 36 additions and 67 deletions

View File

@@ -764,7 +764,7 @@ namespace Netch.Forms
private System.Windows.Forms.ToolStripMenuItem ImportServersFromClipboardToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem ManageSubscribeLinksToolStripMenuItem;
private System.Windows.Forms.MenuStrip MenuStrip;
public System.Windows.Forms.SearchComboBox ModeComboBox;
private System.Windows.Forms.SearchComboBox ModeComboBox;
private System.Windows.Forms.Label ModeLabel;
private System.Windows.Forms.ToolStripMenuItem ModeToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem HelpToolStripMenuItem;

View File

@@ -83,7 +83,7 @@ namespace Netch.Forms
Enabled = false;
try
{
Modes.Load();
InitMode();
NotifyTip(i18N.Translate("Modes have been reload"));
}
catch (Exception)

View File

@@ -16,6 +16,17 @@ namespace Netch.Forms
{
#region Server
private void InitServer()
{
var comboBoxInitialized = _comboBoxInitialized;
_comboBoxInitialized = false;
ServerComboBox.Items.Clear();
ServerComboBox.Items.AddRange(Global.Settings.Server.ToArray());
SelectLastServer();
_comboBoxInitialized = comboBoxInitialized;
}
private static void TestServer()
{
try
@@ -50,6 +61,18 @@ namespace Netch.Forms
#region Mode
private void InitMode()
{
var comboBoxInitialized = _comboBoxInitialized;
_comboBoxInitialized = false;
ModeComboBox.Items.Clear();
Modes.Load();
ModeComboBox.Items.AddRange(Global.Modes.ToArray());
SelectLastMode();
_comboBoxInitialized = comboBoxInitialized;
}
public void SelectLastMode()
{
// 如果值合法,选中该位置

View File

@@ -37,12 +37,9 @@ namespace Netch.Forms
// 计算 ComboBox绘制 目标宽度
_eWidth = ServerComboBox.Width / 10;
Modes.Load();
ServerComboBox.DataSource = Global.Settings.Server;
SaveSelectIndex = true;
SelectLastMode();
SelectLastServer();
InitMode();
InitServer();
_comboBoxInitialized = true;
// 加载翻译
InitText();
@@ -340,7 +337,9 @@ namespace Netch.Forms
return;
}
Modes.Delete(ModeComboBox.SelectedItem as Models.Mode);
var selectedMode = (Models.Mode) ModeComboBox.SelectedItem;
this.ModeComboBox.Items.Remove(selectedMode);
Modes.Delete(selectedMode);
SelectLastMode();
}
@@ -431,17 +430,17 @@ namespace Netch.Forms
#endregion
private bool SaveSelectIndex = false;
private bool _comboBoxInitialized = false;
private void ModeComboBox_SelectedIndexChanged(object sender, EventArgs o)
{
if (!SaveSelectIndex) return;
if (!_comboBoxInitialized) return;
Global.Settings.ModeComboBoxSelectedIndex = ModeComboBox.SelectedIndex;
}
private void ServerComboBox_SelectedIndexChanged(object sender, EventArgs o)
{
if (!SaveSelectIndex) return;
if (!_comboBoxInitialized) return;
Global.Settings.ServerComboBoxSelectedIndex = ServerComboBox.SelectedIndex;
}
}

View File

@@ -203,6 +203,6 @@ namespace Netch
/// <summary>
/// 用于存储模式
/// </summary>
public static readonly SortableBindingList<Models.Mode> Modes = new SortableBindingList<Models.Mode>();
public static readonly List<Models.Mode> Modes = new List<Models.Mode>();
}
}

View File

@@ -147,7 +147,7 @@ namespace Netch.Models
/// <summary>
/// 服务器列表
/// </summary>
public readonly BindingList<Server> Server = new BindingList<Server>();
public readonly List<Server> Server = new List<Server>();
/// <summary>
/// 全局绕过 IP 列表

View File

@@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
public class SortableBindingList<T> : BindingList<T>
{
public SortableBindingList(IList<T> list) : base(list)
{
}
public SortableBindingList()
{
}
public void Sort()
{
Sort(null, null);
}
public void Sort(IComparer<T> comparer)
{
Sort(comparer, null);
}
public void Sort(Comparison<T> comparison)
{
Sort(null, comparison);
}
private void Sort(IComparer<T> comparer, Comparison<T> comparison)
{
// if (typeof(T).GetInterface(nameof(IComparable)) == null) return;
var raiseListChangedEvents = this.RaiseListChangedEvents;
this.RaiseListChangedEvents = false;
try
{
var items = (List<T>) this.Items;
if (comparison != null) items.Sort(comparison);
else items.Sort(comparer);
}
finally
{
this.RaiseListChangedEvents = raiseListChangedEvents;
ResetBindings();
}
}
}

View File

@@ -17,10 +17,6 @@ namespace Netch.Utils
/// </summary>
public static void Load()
{
var raiseListChangedEvents = Global.Modes.RaiseListChangedEvents;
Global.Modes.RaiseListChangedEvents = false;
Global.MainForm.ModeComboBox.DataSource = null;
Global.Modes.Clear();
if (!Directory.Exists(MODE_DIR)) return;
@@ -45,8 +41,6 @@ namespace Netch.Utils
}
Sort();
Global.MainForm.ModeComboBox.DataSource = Global.Modes;
Global.Modes.RaiseListChangedEvents = raiseListChangedEvents;
}
private static void LoadModeFile(string path)