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?
Well, if the control is bound, you can probably get the old value from the data source and compare that to the value of the control. The control itself doesn't keep track of the old value.
And I don't know of any way to cancel the selection, but if you use UltraCombo, instead of UltraComboEditor, you can disable rows to prevent the user from selecting them.
What is the event that I should use? The control is bound so I need an event which when it happen the value in the data is different than the control value.
I can cache the old value and make my own ValueChanging event like that: (sample attached)
protected object oldValue;
private bool skipValueChanging;
{
OnValueChanging(e);
this.SelectedItem = null;
else
Value = oldValue;
}
oldValue = Value;
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.
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?
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>
if (m_OldValue == null)
Value = m_OldValue;
get
set
m_OldValue = Value;
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