Text validation

Hi,



The nodes in my graph contain editable GoText objects which use comboboxes for editing. After editing they need validation (unless the user pressed Cancel). I tried to utilize the Validation event of the Control object. (This event can cancel focus change if some boolean parameter is set to “true”.)



This didn’t work because GoDiagram objects don’t know what to do when the editor control “doesn’t want” to lose its focus.



I would be happy to implement validation in some other way but I didn’t find any. The validation feature must:

- be silent when user presses Cancel;

- force the user to continue editing until the validation is OK, even if he/she presses Enter or clicks outside the control.

For example, GoText.DoEdit can’t be used for this because it can’t say “no, don’t commit, keep editing”. It seems to be no way to cancel anything between pressing Enter (or clicking outside) and committing changes.



Is there any simple workaround? If not, the validation issue is worth to be handled in future versions…

Yes, we’re thinking about making this easier by changing the signature of DoEdit to return a bool: false to indicate that the Control should remain editing, true to call DoEndEdit.
In the meantime, you can implement your own ComboBox control that is created by an instance of GoControl that you return from an override of GoText.CreateEditor. Here’s the definition of the ComboBox control that is built-in:
sealed internal class ComboBoxControl : System.Windows.Forms.ComboBox, IGoControlObject { // nested class
public ComboBoxControl() {}
public GoControl GoControl {
get { return myGoControl; }
set {
GoControl old = myGoControl;
if (old != value) {
myGoControl = value;
if (value != null) {
GoText gotext = value.EditedObject as GoText;
if (gotext != null) {
this.RightToLeft = gotext.RightToLeft ? System.Windows.Forms.RightToLeft.Yes : System.Windows.Forms.RightToLeft.No;
Font objfont = gotext.Font;
float fsize = objfont.Size;
if (this.GoView != null)
fsize *= this.GoView.DocScale / this.GoView.WorldScale.Height;
this.Font = gotext.makeFont(objfont.Name, fsize, objfont.Style);
foreach (Object x in gotext.Choices) {
this.Items.Add(x);
}

if (!gotext.Multiline) {
int newline = gotext.Text.IndexOf("\r\n");
if (newline >= 0)
this.Text = gotext.Text.Substring(0, newline);
else
this.Text = gotext.Text;
} else {
this.Text = gotext.Text;
}
if (gotext.DropDownList)
this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
else
this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
}
}
}
}
}

public GoView GoView {
get { return myGoView; }
set { myGoView = value; }
}
protected override bool ProcessDialogKey(System.Windows.Forms.Keys key) {
if (HandleKey(key))
return true;
else
return base.ProcessDialogKey(key);
}
protected override void OnLeave(EventArgs evt) {
AcceptText();
base.OnLeave(evt);
}
private bool HandleKey(System.Windows.Forms.Keys key) {
if (key == System.Windows.Forms.Keys.Escape) {
GoControl ctrl = this.GoControl;
if (ctrl != null)
ctrl.DoEndEdit(this.GoView);
this.GoView.InitFocus();
return true;
} else if (key == System.Windows.Forms.Keys.Enter || key == System.Windows.Forms.Keys.Tab) {
AcceptText();
this.GoView.InitFocus();
return true;
} else {
return false;
}
}
private void AcceptText() {
GoControl ctrl = this.GoControl;
if (ctrl != null) {
GoText gotext = ctrl.EditedObject as GoText;
if (gotext != null) {
gotext.DoEdit(this.GoView, gotext.Text, this.Text);
}
ctrl.DoEndEdit(this.GoView);
}
}
// ComboBoxControl state
private GoControl myGoControl = null;
private GoView myGoView = null;
} // end ComboBoxControl

Thanks for your response!

GoView.InitFocus() appears to be internal.

What alternative method is available for custom editor controls ?
try this.GoView.Focus();
Demo1 has samples of CheckBox and RichTextBox. search for IGoControlObject.
Thanks, that works.