Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how to validate the input of controls on an Eto.Forms dialog.
using System;
using System.Collections.Generic;
using System.Linq;
using Eto.Forms;
using Eto.Drawing;
namespace MyValidationApp
{
// base validator that can be extended to do custom validation
public class ControlValidator
{
public Control Target { get; private set; }
public string Message { get; set; }
readonly Func<bool> validate;
public ControlValidator (Control target, Func<bool> validate, string message = null)
{
Target = target;
this.validate = validate;
Message = message;
}
protected virtual void SetValid()
{
Target.BackgroundColor = SystemColors.ControlBackground;
}
protected virtual void SetInvalid()
{
Target.BackgroundColor = Colors.Red;
}
public bool Validate()
{
if (validate())
{
SetValid ();
return true;
}
SetInvalid ();
return false;
}
}
// collection of validations
class ValidationHandler : List<ControlValidator>
{
public Action<IEnumerable<ControlValidator>> IsInvalid { get; set; }
public IEnumerable<ControlValidator> Validate()
{
var invalidItems = new List<ControlValidator>();
foreach (var item in this) {
if (!item.Validate ())
invalidItems.Add (item);
}
if (invalidItems.Count > 0 && IsInvalid != null)
IsInvalid (invalidItems);
return invalidItems;
}
}
// dialog that uses control validation
public class MyValidationDialog : Dialog<bool>
{
public MyValidationDialog ()
{
var validations = new ValidationHandler();
validations.IsInvalid = items => {
// focus the first item with an error
items.First().Target.Focus();
// show error
var message = string.Join("\n", items.Where(r => r.Message != null).Select(r => " > " + r.Message));
if (!string.IsNullOrEmpty(message))
MessageBox.Show("Errors in the form:\n" + message, null, MessageBoxButtons.OK, MessageBoxType.Error);
};
// input controls
var myTextBox = new TextBox ();
var myDropDown = new DropDown { Items = { "Male", "Female" } };
// validations
validations.Add (new ControlValidator (myTextBox, () => !string.IsNullOrEmpty(myTextBox.Text), "Name is required"));
validations.Add (new ControlValidator (myDropDown, () => myDropDown.SelectedValue != null, "Gender is required"));
// buttons
var btnOk = new Button { Text = "Ok" };
btnOk.Click += (sender, e) => {
if (!validations.Validate().Any())
Close (true);
};
var btnCancel = new Button { Text = "Cancel" };
btnCancel.Click += (sender, e) => Close(false);
// content
Content = new StackLayout {
HorizontalContentAlignment = HorizontalAlignment.Stretch,
Spacing = 10,
Items = {
new StackLayoutItem(new TableLayout {
Padding = 10,
Spacing = new Size(5, 5),
Rows = {
new TableRow("Name", myTextBox),
new TableRow("Gender", myDropDown)
}
}, expand: true),
new StackLayout {
Orientation = Orientation.Horizontal,
Spacing = 5,
Items = { null, btnOk, btnCancel }
}
}
};
Title = "My Dialog with Validation";
DefaultButton = btnOk;
AbortButton = btnCancel;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.