- 编译时把 Thread 的 Suspend / Resume / Abort、ThreadState、Sleep、new Thread 改写成平台的 协作式实现:被挂起或终止的线程在 Sleep、后台改控件、调设备类库、弹提示框时停下或结束; ThreadStateException 等行为与 .NET Framework 一致;编辑器不再提示"已过时" - ListBox:列表项、单选、SelectedIndexChanged 先于 Click、删除选中项后取消选中; ComboBox 与 ListBox 共用 ListControl 和列表项集合;设计器工具箱加"列表框" - 评分规则的"包含"对列表框检查所有列表项 - 示例:项目 5 任务 2(烟雾,线程)、任务 3(推送灾情,二维数组、两个窗体、ListBox), 参考答案修正书上直接 Abort 挂起线程、烟雾记成"发现火情"的问题 - 一直不换行的 Console 输出攒满 4096 字符先发出,避免占满内存 - 测试:旧版线程 API、ListBox 事件顺序、两个示例的端到端和评分测试 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
3.8 KiB
C#
114 lines
3.8 KiB
C#
using System.Drawing;
|
|
using System.Text.Json.Nodes;
|
|
using Sim.Host;
|
|
|
|
namespace System.Windows.Forms;
|
|
|
|
public class ComboBox : ListControl
|
|
{
|
|
private ComboBoxStyle _dropDownStyle = ComboBoxStyle.DropDown;
|
|
|
|
public ComboBox() => Items = new ObjectCollection(this);
|
|
|
|
protected override Size DefaultSize => new(121, 20);
|
|
|
|
public ObjectCollection Items { get; }
|
|
|
|
public bool FormattingEnabled { get; set; }
|
|
public int MaxDropDownItems { get; set; } = 8;
|
|
public int DropDownWidth { get; set; } = 121;
|
|
public int DropDownHeight { get; set; } = 106;
|
|
public bool Sorted { get; set; }
|
|
public bool DroppedDown { get; set; }
|
|
|
|
public ComboBoxStyle DropDownStyle
|
|
{
|
|
get => _dropDownStyle;
|
|
set => SetProp(ref _dropDownStyle, value, nameof(DropDownStyle));
|
|
}
|
|
|
|
public override int SelectedIndex
|
|
{
|
|
get => SelectedIndexValue;
|
|
set
|
|
{
|
|
if (value < -1 || value >= Items.Count)
|
|
throw new ArgumentOutOfRangeException(nameof(value), $"“{value}”的值对于“SelectedIndex”无效。");
|
|
if (value == SelectedIndexValue) return;
|
|
SetProp(ref SelectedIndexValue, value, nameof(SelectedIndex));
|
|
// 选中项变化时 Text 随之变化(先 TextChanged,再 SelectedIndexChanged,与 WinForms 一致)
|
|
Text = value >= 0 ? Items[value]?.ToString() ?? "" : "";
|
|
OnSelectedIndexChanged(EventArgs.Empty);
|
|
OnSelectedValueChanged(EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public object? SelectedItem
|
|
{
|
|
get => SelectedIndexValue >= 0 ? Items[SelectedIndexValue] : null;
|
|
set => SelectedIndex = value == null ? -1 : Items.IndexOf(value);
|
|
}
|
|
|
|
public string SelectedText { get; set; } = "";
|
|
|
|
public event EventHandler? SelectedIndexChanged;
|
|
public event EventHandler? DropDown;
|
|
public event EventHandler? DropDownClosed;
|
|
|
|
protected virtual void OnSelectedIndexChanged(EventArgs e) => SelectedIndexChanged?.Invoke(this, e);
|
|
protected virtual void OnDropDown(EventArgs e) => DropDown?.Invoke(this, e);
|
|
protected virtual void OnDropDownClosed(EventArgs e) => DropDownClosed?.Invoke(this, e);
|
|
|
|
public void BeginUpdate() { }
|
|
public void EndUpdate() { }
|
|
|
|
protected override void OnUserAction(UserAction action)
|
|
{
|
|
switch (action.Action)
|
|
{
|
|
case "dropDown":
|
|
// 教材在 Click 事件里刷新串口列表,所以 Click 必须在列表展开之前触发
|
|
OnClick(EventArgs.Empty);
|
|
OnDropDown(EventArgs.Empty);
|
|
break;
|
|
case "select":
|
|
if (action.Index >= 0 && action.Index < Items.Count) SelectedIndex = action.Index;
|
|
OnDropDownClosed(EventArgs.Empty);
|
|
break;
|
|
case "text" when _dropDownStyle != ComboBoxStyle.DropDownList:
|
|
SetTextFromUser(action.Value ?? "");
|
|
var match = Items.IndexOf(Text);
|
|
if (match != SelectedIndexValue) SelectedIndexValue = match;
|
|
break;
|
|
default:
|
|
base.OnUserAction(action);
|
|
break;
|
|
}
|
|
}
|
|
|
|
internal override void ItemsChanged()
|
|
{
|
|
NotifyChanged(nameof(Items), ItemTexts(Items));
|
|
NotifyChanged(nameof(SelectedIndex), SelectedIndexValue);
|
|
}
|
|
|
|
internal override void ItemsCleared()
|
|
{
|
|
SelectedIndex = -1;
|
|
ItemsChanged();
|
|
}
|
|
|
|
protected override void WriteProps(JsonObject props)
|
|
{
|
|
base.WriteProps(props);
|
|
props["Items"] = PropValue.From(ItemTexts(Items));
|
|
props["SelectedIndex"] = SelectedIndexValue;
|
|
props["DropDownStyle"] = _dropDownStyle.ToString();
|
|
}
|
|
|
|
public class ObjectCollection : ListItemCollection
|
|
{
|
|
public ObjectCollection(ComboBox owner) : base(owner) { }
|
|
}
|
|
}
|