Radio 用于一组选项中只能选择一个的场景,例如性别、支付方式、主题模式。多个 Radio 放在同一个父容器内时通常按 WinForms 规则互斥。
方法一:用 aardio 控件作为 AntdUI.Radio 的宿主窗口
import win.ui;
import dotNet.AntdUI;
import System.Drawing;
/*DSG{{*/
var winform = win.form(text="AntdUI Radio";right=360;bottom=180)
winform.add(
radio1={cls="custom.radio";text="标准版";left=44;top=86;right=144;bottom=106;checked=1;z=1};
radio2={cls="custom.radio";text="专业版";left=44;top=47;right=144;bottom=67;z=2}
)
/*}}*/
var r1 = AntdUI.Radio(winform.radio1);
var r2 = AntdUI.Radio(winform.radio2);
r1.CheckedChanged = function(sender,e){
if(r1.Checked) winform.text = "标准版";
r2.Checked = !r1.Checked;
}
r2.CheckedChanged = function(sender,e){
if(r2.Checked) winform.text = "专业版";
r1.Checked = !r2.Checked;
}
winform.show();
win.loopMessage();
因为 r1,r2 的父窗口不同,因此需要显式同步多个 radio 的 Checked 属性。
方法二:显式创建 AntdUI.BaseForm 管理多个 AntdUI.Radio #
import win.ui;
import dotNet.AntdUI;
/*DSG{{*/
var winform = win.form(text="AntdUI Radio";right=360;bottom=180);
winform.add(
customHost={cls="custom";left=0;top=0;right=360;bottom=180;z=1}
);
/*}}*/
// 一个 aardio custom 宿主内需要放多个 AntdUI 控件时,建议先创建 BaseForm 容器。
var baseForm = AntdUI.BaseForm(winform.customHost);
var r1 = AntdUI.Radio();
r1.Text = "标准版";
r1.Checked = true;
r1.Location = System.Drawing.Point(24,24);
r1.Size = System.Drawing.Size(120,32);
baseForm.Controls.Add(r1);
var r2 = AntdUI.Radio();
r2.Text = "专业版";
r2.Location = System.Drawing.Point(24,64);
r2.Size = System.Drawing.Size(120,32);
baseForm.Controls.Add(r2);
r1.CheckedChanged = function(sender,e){
if(owner.Checked) winform.text = "标准版";
}
r2.CheckedChanged = function(sender,e){
if(owner.Checked) winform.text = "专业版";
}
winform.show();
win.loopMessage();
这种方式的好处是利用 AntdUI.BaseForm 自动创建 AntdUI.Radio 分组,不需要自己同步 Checked 属性以实现单选。
但这里有一个需要非常小心的地方: 必须在初始化好所有 AntdUI 控件以后再调用 winform.show() 显示窗体。
这是因为 AntdUI.BaseForm 在初始化时执行 DPI 自动缩放,如果错过了这个唯一的机会就需要显式调用 AutoDpi 方法缩放控件(这难以控制且容易出错,多次调用会重复放大)。
当调用 AntdUI.BaseForm(winform.customHost) 时 aardio 会自动调用 System.Windows.Forms.CreateEmbed(baseForm,winform.customHost)。在这个函数内又会自动调用 dotNet.setParent(baseForm,winform.customHost),而 dotNet.setParent 会调用 winform.customHost.ready 延迟初始化 baseForm 。
winform.customHost.ready 注册的回调在 winform.show()、winform.enableDpiScaling()、win.loopMessage() 调用时执行(保证执行一次并且只会执行一次),如果这些阶段都已经错过了则会延迟到下次处理窗口消息时执行。
因此正确的流程是: 创建 AntdUI.BaseForm » 往 AntdUI.BaseForm 上添加控件 » 调用 winform.show() 。也就是尽量在窗体完成初始化前添加 .NET 控件,以避免出现一些奇怪的问题。
Text:显示文本。Checked:是否选中。AutoCheck:点击自动切换。ForeColor、Fill:文字色与选中色。CheckedChanged:状态改变事件。