NumericUpDown.value 未保存在用户设置中
我在表单上有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您输入“NumericUpDown1.Value”时,您必须将
My.Settings.Heures
中的值设置为十进制。在 Form1_Load 中添加:
并添加到按钮或其他小部件的事件侦听器:
As you are putting "NumericUpDown1.Value" you have to set the value at
My.Settings.Heures
to decimal.In Form1_Load add:
and add to the event listener for your button or other widget:
我猜问题是
Leave
事件没有像您期望的那样被触发,特别是如果用户只是单击向上/向下箭头。我怀疑只有当用户实际单击值区域然后离开时才会触发它。您可以通过调试来查看您的代码是否被命中或通过显示该事件中的简单消息框来验证这一点。我认为如果您挂钩
LostFocus
或ValueChanged
事件,您会拥有更好的运气。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
orValueChanged
event.我也想为将来关注这个问题的人补充这一点。
通过输入保存您的设置,如图所示
My.Settings.Heures = NumericUpDownHeures.Value
到您的 ValueChanged 事件中,然后在表单加载事件中执行相反的操作。问题是,当您首次初始化时,此值更改事件会在表单加载之前触发,因此它将继续默认为您在设计器中设置的任何值,因为您正在使用设计器值覆盖设置值。
为了解决这个问题,您需要在代码顶部有一个私有/公共布尔值,只有在表单加载后才将其设置为 true(在 form_load 事件的底部设置为 true),然后您可以将条件添加到ValueChanged 事件检查表单是否已加载。如果是,则更改设置值,如果不是,则不更改。
一个例子:
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: