簡體   English   中英

如何在更改前注冊 numericUpDown 的值?

[英]How to register the value of numericUpDown before a change?

我對 C# 比較陌生(使用 Visual Studio)並且在表單上使用了幾個 numericUpDown,這些的數字范圍是 8 到 15。

當您更改這些 numericUpDown 中的值時,提高該值會產生點數成本,降低該值會增加點數...因為成本不同,您得到的越高,我試圖在更改前注冊該值,不幸的是我不知道我該如何管理。

誰能指出我正確的方向?

private int numericValue = 8;

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    //numericValue holds the before value
    ...
    numericValue = (int)numericUpDown1.Value;
}

這是我的方法,它相對簡單,應該可以很好地工作而沒有任何問題:

//First, store your values in an array, just a small one
int[] values = {0, 0};

//Then, when the value of the NumericUpDown is changed, move the values around the array
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    values[0] = values[1];
    values[1] = numericUpDown1.Value;
}

當程序第一次啟動時,數組將被初始化為0, 0並且你的 NumericUpDown 應該有一個值0 讓我們看看當我們改變 NumericUpDown 的值時會發生什么。

在我們更改值之前,數組的值為0, 0 當我們更改它時,數組 get 索引1處的最后一個0被移動到索引0 ,而 NumericUpDown get 的當前值被移動到索引1 假設我們將值更改為 55,數組的值現在是0, 55 假設我們再次將其更改為 68。數組的值現在是55, 68 數組的舊值始終存儲在數組的索引0處。

如果您希望您的 NumericUpDown 具有不同於0的默認值,只需更改數組的初始化,以反映您的 NumericUpDown 的默認值。 假設您希望默認值為100 ,則數組應初始化為:

int[] values = {0, 100};

您可以使用 numericUpDown1 檢索舊值。 numericUpDown1_ValueChanged 中的文本

這是我的解決方案:

private void nud_splitOrder_ordersQuantaty_ValueChanged(object sender, EventArgs e)
{
    var senderObject = (NumericUpDown)sender;
    MessageBox.Show($"from {((UpDownBase)sender).Text} to {senderObject.Value}");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM