I would like to alter the behavior of the calendar popup so that when the Equal button is clicked, it performs all the default actions of the equal button, but closes the popup calculator and the result is selected in the control.
Thanks,
Mark
5 Answers, 1 is accepted
I assume that you are using the RadCalculatorDropDown control. You can achieve the desired behavior by handling the click event of the Equals button and then closing the popup. Please check my code snippet:
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
public
RadForm1()
{
InitializeComponent();
this
.radCalculatorDropDown1.CalculatorElement.CalculatorContentElement.ButtonEquals.Click += ButtonEquals_Click;
}
private
void
ButtonEquals_Click(
object
sender, EventArgs e)
{
this
.radCalculatorDropDown1.CalculatorElement.ClosePopup();
}
}
I hope this will help. Let me know if you have other questions.
Regards,
Hristo
Progress Telerik
Thanks for the quick reply. And, you are correct in that I meant Calculator, not calendar.
This got me to the behavior I needed.
I also noticed that the number pad keys on the keyboard can manipulate the calculator while it is popped up. How would I trap for the Enter key so it also fires the ButtonEquals_Click event?
You can handle the KeyDown event of the content element and raise a boolean flag. Then in the ValueChanged event, you can close the popup:
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
public
RadForm1()
{
InitializeComponent();
this
.radCalculatorDropDown1.ValueChanged += RadCalculatorDropDown1_ValueChanged;
this
.radCalculatorDropDown1.CalculatorElement.CalculatorContentElement.KeyDown += CalculatorContentElement_KeyDown;
this
.radCalculatorDropDown1.CalculatorElement.CalculatorContentElement.ButtonEquals.Click += ButtonEquals_Click;
}
private
void
RadCalculatorDropDown1_ValueChanged(
object
sender, EventArgs e)
{
if
(
this
.isEnter)
{
this
.isEnter =
false
;
this
.radCalculatorDropDown1.CalculatorElement.ClosePopup();
}
}
bool
isEnter;
private
void
CalculatorContentElement_KeyDown(
object
sender, KeyEventArgs e)
{
this
.isEnter =
false
;
if
(e.KeyCode == Keys.Enter)
{
this
.isEnter =
true
;
}
}
private
void
ButtonEquals_Click(
object
sender, EventArgs e)
{
this
.radCalculatorDropDown1.CalculatorElement.ClosePopup();
}
}
I hope this will help. Let me know if you have other questions.
Regards,
Hristo
Progress Telerik
This seems to work, however the ValueChanged Event is firing an extra time recursively when ClosePopup() is fired from within it.
If I PopUp the calculator control and type from the keyboard, say 6 x 8, and press enter, I can see that it fires the keydown event and sets my flag, then fires the ValueChanged event (.Value now 48). However, the ClosePopup() then fires the ValueChanged event again recursively and my new value is now 6 x 48 = 288. Happens the same with other operators: 6 + 8 then enter sets value to 14, but ValueChange is fired again upon closepopup, then does 6 + 14 = 20 for value of control after popup finally closes up.
This does not happen if I just press the "=" button with the mouse on the popup. It sets the correct value and closes the popup.
Not sure you can recreate this on your end.
Private Sub InitLCQuantityCalcControl()
With Me.rcalcLCQuantity.CalculatorElement.CalculatorContentElement
.ButtonMplus.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonMminus.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonMS.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonMR.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonMC.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonSqrt.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonPercent.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonReciprocal.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
.ButtonSign.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
AddHandler .ButtonEquals.Click, AddressOf ButtonEquals_Click
AddHandler .KeyDown, AddressOf ButtonEquals_KeyDown
End With
End Sub
Private Sub ButtonEquals_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim EqualButton As RadCalculatorEqualsButtonElement = TryCast(sender, RadCalculatorEqualsButtonElement)
Dim CalcCtl As RadCalculatorContentElement = DirectCast(EqualButton.Parent.Parent, RadCalculatorContentElement)
CalcCtl.CalculatorElement.Value = CInt(CalcCtl.CalculatorElement.Value)
CalcCtl.CalculatorElement.ClosePopup()
End Sub
Private Sub ButtonEquals_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
mbIsEnterKey = False
If e.KeyCode = Keys.Enter Then
mbIsEnterKey = True
End If
End Sub
Private Sub rcalc_ValueChanged(sender As Object, e As EventArgs) Handles rcalcLCQuantity.ValueChanged
mbTrackChanges = True
If mbIsEnterKey Then
mbIsEnterKey = False
Dim ctl As RadCalculatorDropDownElement = TryCast(sender, RadCalculatorDropDownElement)
ctl.CalculatorContentElement.CalculatorElement.ClosePopup()
End If
End Sub
Indeed the approach I suggested with the flag will handle the scenario when you are not directly entering the input in the editor but instead in the popup. The desired behavior can be accomplished with a custom control and elements handling the keyboard. Please check below:
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
public
RadForm1()
{
InitializeComponent();
this
.radCalculatorDropDown1.CalculatorElement.CalculatorContentElement.ButtonEquals.Click += ButtonEquals_Click;
}
private
void
ButtonEquals_Click(
object
sender, EventArgs e)
{
this
.radCalculatorDropDown1.CalculatorElement.ClosePopup();
}
}
public
class
CustomRadCalculatorDropDown : RadCalculatorDropDown
{
public
override
string
ThemeClassName
{
get
{
return
typeof
(RadCalculatorDropDown).FullName;
}
}
protected
override
RadCalculatorDropDownElement CreateCalculatorDropDownElement()
{
return
new
CustomRadCalculatorDropDownElement();
}
}
public
class
CustomRadCalculatorDropDownElement : RadCalculatorDropDownElement
{
protected
override
Type ThemeEffectiveType
{
get
{
return
typeof
(RadCalculatorDropDownElement);
}
}
private
CustomRadCalculatorContentElement element;
protected
override
void
CreateChildElements()
{
base
.CreateChildElements();
element =
new
CustomRadCalculatorContentElement(
this
);
typeof
(RadCalculatorDropDownElement).GetField(
"calculatorContentElement"
, BindingFlags.Instance | BindingFlags.NonPublic).SetValue(
this
, element);
}
protected
override
RadPopupControlBase CreatePopupForm()
{
CustomRadCalculatorEditorPopupControlBase popup =
new
CustomRadCalculatorEditorPopupControlBase(
this
);
typeof
(RadCalculatorDropDownElement).GetField(
"popup"
, BindingFlags.Instance | BindingFlags.NonPublic).SetValue(
this
, popup);
popup.VerticalAlignmentCorrectionMode = AlignmentCorrectionMode.SnapToOuterEdges;
popup.HorizontalAlignmentCorrectionMode = AlignmentCorrectionMode.Smooth;
popup.SizingMode = SizingMode.UpDownAndRightBottom;
popup.SizingGrip.ShouldAspectRootElement =
false
;
popup.SizingGrip.ShouldAspectMinSize =
true
;
popup.RightToLeft =
this
.RightToLeft ? System.Windows.Forms.RightToLeft.Yes : System.Windows.Forms.RightToLeft.Inherit;
popup.MinimumSize =
new
Size(
this
.MinPopupWidth,
this
.MinPopupHeight);
this
.WirePopupFormEvents(popup);
popup.SizingGripDockLayout.Children.Add(element);
return
popup;
}
}
public
class
CustomRadCalculatorContentElement : RadCalculatorContentElement
{
private
RadCalculatorDropDownElement element;
protected
override
Type ThemeEffectiveType
{
get
{
return
typeof
(RadCalculatorContentElement);
}
}
public
CustomRadCalculatorContentElement(RadCalculatorDropDownElement element)
:
base
(element)
{
this
.element = element;
}
protected
override
void
OnKeyDown(KeyEventArgs e)
{
if
(e.KeyCode == Keys.Enter)
{
this
.element.ClosePopup();
return
;
}
base
.OnKeyDown(e);
}
}
public
class
CustomRadCalculatorEditorPopupControlBase : RadCalculatorEditorPopupControlBase
{
public
CustomRadCalculatorEditorPopupControlBase(RadItem owner)
:
base
(owner)
{
}
public
override
bool
OnKeyDown(Keys keyData)
{
if
(keyData == Keys.Enter)
{
this
.ClosePopup(RadPopupCloseReason.Keyboard);
return
true
;
}
return
base
.OnKeyDown(keyData);
}
}
Additional information on how controls and elements can be extended is available here: https://docs.telerik.com/devtools/winforms/telerik-presentation-framework/inherit-themes-from-radcontrols-derivatives.
I hope this will help. Let me know if you need further assistance.
Regards,
Hristo
Progress Telerik