Skip to main content

WinForms Adapter

The WinForms adapter marshals channel events and point changes to the UI thread and provides control binding helpers so each form does not need custom InvokeRequired code.

var attachment = this.AttachZeus(builder => builder.AddVirtualChannel("meter"));
var meter = attachment.Host.Channels.Get("meter");
meter.BindState(stateLabel);
meter.BindTo(echoLabel);
meter.BindTo(hexLabel, ChannelTextFormatter.Hex);
meter.BindEnabled(sendButton);

Assembly: Zeus.Presentation.WinForms with target framework net8.0-windows.

APIs

APIPurpose
Form.AttachZeus(...)Starts on load and disposes on form close
BindTo / BindTextWrites received data to Control.Text
BindStateDisplays channel state
BindEnabledSets Control.Enabled, defaulting to enabled only when Open
AsBindingSource(control)Exposes an INotifyPropertyChanged projection
points.BindTo("temperature", label)Writes point value to a control
points.BindSnapshot(...)Pushes full point snapshots to the UI thread
points.BindHistory(...)Pushes recent successful samples to the UI thread
points.BindAlarmBackColor / BindAlarmForeColorChanges colors based on alarm/error state
points.BindWriteBackBinds a button click to Points.WriteAsync

Destroyed controls or controls without handles ignore updates.

Point Binding

points.BindTo("temperature", temperatureLabel, value => $"{value:0.0} C");
points.BindAlarmBackColor("temperature", temperatureLabel);
points.BindSnapshot("temperature", alarmLabel, snapshot =>
{
alarmLabel.Text = snapshot.AlarmState.ToString();
});

points.BindEnabled("setpoint", writeButton);
points.BindWriteBack(
"setpoint",
setpointTextBox,
writeButton,
text => double.Parse(text, CultureInfo.InvariantCulture),
ex => MessageBox.Show(this, ex.Message, "Write failed", MessageBoxButtons.OK, MessageBoxIcon.Warning));

If you already use WinForms data binding, bind the projection:

var source = points.AsBindingSource("temperature", this);
temperatureLabel.DataBindings.Add("Text", source, nameof(PointBindingSource.Text));
alarmLabel.DataBindings.Add("Text", source, nameof(PointBindingSource.AlarmState));

Common Issues

  • Cross-thread exception: use this package's BindTo instead of updating controls inside DataReceived.
  • Serial port remains busy after closing: use AttachZeus or dispose the host yourself.
  • Need to bind another property: use a specific helper when available, or map it yourself through BindSnapshot.