我有一個系結的 NumericUpDown 控制元件。
我希望在ValueChanged單擊向上或向下箭頭或用戶更改數字時觸發事件。
單擊向上或向下箭頭時,ValueChanged事件會觸發,但當我只是在控制元件中鍵入時不會觸發。
// numericUpDownDiscountRate
//
this.numericUpDownDiscountRate.DecimalPlaces = 4;
this.numericUpDownDiscountRate.Location = new System.Drawing.Point(133, 344);
this.numericUpDownDiscountRate.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.numericUpDownDiscountRate.Name = "numericUpDownDiscountRate";
this.numericUpDownDiscountRate.Size = new System.Drawing.Size(115, 23);
this.numericUpDownDiscountRate.TabIndex = 78;
this.numericUpDownDiscountRate.ValueChanged = new System.EventHandler(this.numericUpDownDiscountRate_ValueChanged);
我發現了一個建議使用KeyPress 事件的問題, 但是 keypress 事件發生在值更新之前。
如何從按鍵中獲取控制元件的值?
private void numericUpDownDiscountPercent_KeyPress(object sender, KeyPressEventArgs e)
{
if (loadingForm) return;
try
{
loadingForm = true;
var updn = sender as NumericUpDown;
// updn.value does not include the key press and I don't know how to construct the value
MyCalculateRate(updn?.Value ?? 0);
loadingForm = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Discount rate Key Press");
loadingForm = false;
}
}
uj5u.com熱心網友回復:
由于Value僅在驗證后才更改,當內部編輯控制元件失去焦點或檢索值時驗證輸入時會發生這種情況,因此我建議引入與默認屬性一起使用的自定義值。Value
請注意,當讀取 Value 屬性時,不僅驗證了輸入,而且還重置了 Text,因此Value在編輯時讀取屬性變得非常困難,因為在重置 Text 時會移動插入符號。
當按下向上/向下按鈕時,文本也被格式化,因此 OnTextChanged()被呼叫并設定自定義值。
在此處在自定義控制元件中實作,但您可以使用標準 NumericUpDown 控制元件的事件處理程式執行相同操作。出于前面解釋的原因,
請勿閱讀 中的Value屬性。在編輯文本時
使用。或者改為訂閱該事件。
自定義控制元件是 DataBinding 友好的。OnValueChanged()CurrentEditValueCurrentEditValueChanged
using System.ComponentModel;
using System.Windows.Forms;
[ToolboxItem(true), DesignerCategory("code")]
public class NumericUpDownEx : NumericUpDown {
private static readonly object Event_CurrentEditValueChanged = new object();
private decimal m_CurrentEditValue = 0M;
public NumericUpDownEx() { }
[Bindable(true), Browsable(false), EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual decimal CurrentEditValue {
get => m_CurrentEditValue;
internal set {
if (value != m_CurrentEditValue) {
m_CurrentEditValue = value;
OnCurrentEditValueChanged(this, EventArgs.Empty);
}
}
}
protected override void OnTextChanged(EventArgs e)
{
Console.WriteLine("NumericUpDownEx OnTextChanged");
base.OnTextChanged(e);
if (decimal.TryParse(Text, out decimal v)) {
CurrentEditValue = v;
OnValueChanged(e);
}
}
protected override void OnValueChanged(EventArgs e)
{
Console.WriteLine($"NumericUpDownEx OnValueChanged: CurrentEditValue={CurrentEditValue}");
base.OnValueChanged(e);
}
public event EventHandler CurrentEditValueChanged {
add {
Events.AddHandler(Event_CurrentEditValueChanged, value);
}
remove {
Events.RemoveHandler(Event_CurrentEditValueChanged, value);
}
}
protected virtual void OnCurrentEditValueChanged(object sender, EventArgs e)
{
if (Events[Event_CurrentEditValueChanged] is EventHandler evth) evth(this, e);
}
}
直接(單向)系結到CurrentEditValue屬性的示例:
[SomeTextBox].DataBindings.Add("Text", numericUpDpwnEx1, "CurrentEditValue", true, DataSourceUpdateMode.OnPropertyChanged);
uj5u.com熱心網友回復:
如果您將事件處理程式系結到訪問值的 KeyUp,這有點“騙局”但是(在 .netfw472 中 - 尚未在 netcore 中嘗試過):
private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
{
var v = numericUpDown1.Value; //I know it looks useless, but it seems to make ValueChanged fire more often
}
它會導致您的 ValueChanged 在每次按鍵時觸發,并且.Value在 ValueChanged 中訪問將為您提供當前值..
“但為什么?!” 你可能會問……“我不知道;我從來沒有看過……但是 NumericUpDown 不被認為是最可靠的控制元件……”
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/454929.html
上一篇:使用陣列以外的東西來實體化新物件
下一篇:時間:2019-05-10標簽:c#graphicanimationwithtimertickflickering
