Hi all,
Is there a way to retreive the previously selected value from the UltraComboEditor after the user chooses a new value and before the value is committed?Or is there a way to cancel the user's selection?
And here is approximately the same code in VB.Net.I haven't used/verified this actual code since I did something different, but figured I'd post this as a starter help for anyone in the need for it.
Partial Public Class MyComboEditor Inherits UltraComboEditor ''' <summary> ''' Occurs when the value is about to be changed ''' </summary> Public Event ValueChanging As EventHandler(Of CancelEventArgs) ''' <summary> ''' Holds the previous value in order to allow rollback ''' </summary> Protected _OldValue As Object Private _SkipValueChanging As Boolean Public Sub New() InitializeComponent() Me._OldValue = Nothing End Sub ''' <summary> ''' Rollbacks the value changing if ValueChanging was cancelled ''' </summary> Protected Overrides Sub OnValueChanged(ByVal args As EventArgs) If Me._SkipValueChanging Then Return Dim e As CancelEventArgs = New CancelEventArgs() OnValueChanging(e) If e.Cancel Then 'Roll back the value change if ValueChanging was cancelled Me._SkipValueChanging = True If Me._OldValue Is Nothing Then Me.SelectedItem = Nothing Else Me.Value = _OldValue End If Me._SkipValueChanging = False End If MyBase.OnValueChanged(args) End Sub Protected Overridable Sub OnValueChanging(ByVal args As CancelEventArgs) RaiseEvent ValueChanging(Me, args) End Sub Public Overrides Property Value As Object Get Return MyBase.Value End Get Set(ByVal value As Object) MyBase.Value = value Me._OldValue = value End Set End Property End Class
ValueChanged is fired only when the user changes the value, not when you bind and the binding changes the value.
We already solved the problem like that:
public partial class MyComboEditor : UltraComboEditor
{
/// <summary>
/// Occurs when the value is about to be changed
/// </summary>
/// Holds the previous value in order to allow rollback
private bool m_SkipValueChanging;
InitializeComponent();
}
/// Rollbacks the value changing if ValueChanging was cancelled
/// <param name="args"></param>
OnValueChanging(e);
if (m_OldValue == null)
else
Value = m_OldValue;
get
set
m_OldValue = Value;
What exactly do you mean by "ValueChanged is not fired as a result of binding change"? ValueChanged should fire any time the Value of the control changes. Are you saying you are binding the control to a new data source? Or just that the value of the bound field changed?
My code still has a problem. If the control is bound, the OldValue should get the value of "Value", but ValueChanged is not fired as a result of binding change, so you always get a null value if you cancel the change. I'm using the combo editor inside a UserControl so I have a property that explicitly sets the Value property, but whoever want to use this need to fix it.
I can cache the old value and make my own ValueChanging event like that: (sample attached)
protected object oldValue;
private bool skipValueChanging;
this.SelectedItem = null;
Value = oldValue;
oldValue = Value;