/// <summary> /// Retrieves the user input from the value control and assigns it to Value /// </summary> internal override void ControlToValue() { //set value to null by default Value = null; //null values if (string.IsNullOrWhiteSpace(ValueControl.Text)) { return; } //search primary KeyNotFoundException string at the end of the Text, betewwn parenthesis //example: My Company Inc. (id=34) try { string pkString = ValueControl.Text.Substring(ValueControl.Text.IndexOf('(') + 1); pkString = pkString.TrimEnd(')'); pkString = pkString.Trim(); DataObject dobj = DataObject.From(this.ValueType); TypeConverter.ToDataValueInstances(pkString, dobj.PrimaryKey); Value = dobj; } catch { } //if last attempt was succesfull, load selected from database and exit if (!NullValues.IsNull(Value)) { //if select operation returns null, set value to null again if (DataBase.Current.Select((DataObject)Value) == null) { Value = null; } //otherwise value is already selected and loaded, so exit else { return; } } //otherwise search in the database for a match DataObjectCollection found = DataBase.Current.Search(ValueType, ValueControl.Text); //if found a match, get the first result, otherwise, set Value to null if (found.Count > 0) { Value = found[0]; } else { Value = null; } }
/// <summary> /// Assings the Value to the valuecontrol input /// </summary> internal override void ValueToControl() { if (!NullValues.IsNull(Value)) { DataObject selected; selected = (DataObject)Value; if (!selected.IsSaved) { selected.Select(); } ValueControl.Text = selected.ToString() + " (" + TypeConverter.ToString(selected.PrimaryKey) + ")"; } else { ValueControl.Text = null; } }