NumericUpDown.value 未保存在用户设置中

发布于 2024-12-17 14:27:02 字数 415 浏览 2 评论 0原文

我在表单上有一个 NumericUpDown 控件。在应用程序设置/属性绑定中,对于值参数,我无法选择名为:Heures(整数/用户)的用户设置。

我尝试通过这种方式保存该值:

 Private Sub NumericUpDownHeures_Leave(sender As System.Object, e As System.EventArgs) Handles NumericUpDownHeures.Leave
        My.Settings.Heures = NumericUpDownHeures.Value
        My.Settings.Save()
    End Sub

但它没有保存。 其他设置(字符串/用户)没有问题。但我不明白为什么不保存设置(整数/用户)。

请帮忙,谢谢。

I have a NumericUpDown Control on a form. In the Application Settings / Properties Binding, for the value parameter, i can't select my USER setting called : Heures (Integer / User).

I tried to save the value by this way :

 Private Sub NumericUpDownHeures_Leave(sender As System.Object, e As System.EventArgs) Handles NumericUpDownHeures.Leave
        My.Settings.Heures = NumericUpDownHeures.Value
        My.Settings.Save()
    End Sub

But it's not saved.
No problem for other settings (String / User). But i don't understand why the settings (Integer / User) are not saved.

Please help, Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海夕 2024-12-24 14:27:02

当您输入“NumericUpDown1.Value”时,您必须将 My.Settings.Heures 中的值设置为十进制。

在 Form1_Load 中添加:

NumericUpDownHeures.Value = My.Settings.Heures

并添加到按钮或其他小部件的事件侦听器:

My.Settings.Heures = NumericUpDownHeures.Value

As you are putting "NumericUpDown1.Value" you have to set the value at My.Settings.Heures to decimal.

In Form1_Load add:

NumericUpDownHeures.Value = My.Settings.Heures

and add to the event listener for your button or other widget:

My.Settings.Heures = NumericUpDownHeures.Value
七禾 2024-12-24 14:27:02

我猜问题是 Leave 事件没有像您期望的那样被触发,特别是如果用户只是单击向上/向下箭头。我怀疑只有当用户实际单击值区域然后离开时才会触发它。您可以通过调试来查看您的代码是否被命中或通过显示该事件中的简单消息框来验证这一点。

我认为如果您挂钩 LostFocusValueChanged 事件,您会拥有更好的运气。

I would guess the issue is that the Leave event is not being fired as you expect it to be, especially if the user just clicks the up/down arrows. I suspect that it is only fired when the user actually clicks into the value area, then leaves. You could verify this by debugging to see if your code is ever hit or by showing a simple msgbox from that event.

I think that you will have better luck if you hook the LostFocus or ValueChanged event.

日暮斜阳 2024-12-24 14:27:02

我也想为将来关注这个问题的人补充这一点。

通过输入保存您的设置,如图所示
My.Settings.Heures = NumericUpDownHeures.Value 到您的 ValueChanged 事件中,然后在表单加载事件中执行相反的操作。

问题是,当您首次初始化时,此值更改事件会在表单加载之前触发,因此它将继续默认为您在设计器中设置的任何值,因为您正在使用设计器值覆盖设置值。

为了解决这个问题,您需要在代码顶部有一个私有/公共布尔值,只有在表单加载后才将其设置为 true(在 form_load 事件的底部设置为 true),然后您可以将条件添加到ValueChanged 事件检查表单是否已加载。如果是,则更改设置值,如果不是,则不更改。

一个例子:

Private IsFormLoaded As Boolean = False
    
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    NumericUpDown1.Value = My.Settings.SavedNumValue
    IsFormLoaded = True
End Sub

Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    If IsFormLoaded = False Then Exit Sub
    My.Settings.SavedNumValue = NumericUpDown1.Value
End Sub

OR

Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    If IsFormLoaded Then
        My.Settings.SavedNumValue = NumericUpDown1.Value
    End If
End Sub

I want to add to this as well for anyone looking at this in the future.

Save your settings as shown already by putting
My.Settings.Heures = NumericUpDownHeures.Value into your ValueChanged event, and then doing reverse in the form load event.

The problem is, this value changed event fires before the form load when you first initialize, so it will keep defaulting to whatever value you have set in the designer because you're overwriting the setting value with the designer value.

To get around this, you need a private/public boolean at the top of your code that is only set to true once your form has loaded (set to true at the bottom of your form_load event), then you can add the condition to the ValueChanged event checking if the form is loaded yet or not. If it is, then change the setting value, if not, then don't.

An example:

Private IsFormLoaded As Boolean = False
    
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    NumericUpDown1.Value = My.Settings.SavedNumValue
    IsFormLoaded = True
End Sub

Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    If IsFormLoaded = False Then Exit Sub
    My.Settings.SavedNumValue = NumericUpDown1.Value
End Sub

OR

Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    If IsFormLoaded Then
        My.Settings.SavedNumValue = NumericUpDown1.Value
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文