使用 ApplicationSettings 存储 WinForms RadioButtons 的 Checked 属性
我有一个包含 3 个单选按钮的 WinForms 对话框。 我使用 ApplicationSettings
绑定每个 RadioButton
控件的 Checked 属性,但它没有执行我期望的操作。 现在,我必须单击每个单选按钮两次才能检查它,并且所选单选按钮不会被保留。
关闭表单时是否需要执行一行代码来保存用户设置?
如何消除两次单击单选按钮的需要?
有没有更好的方法来保留这种类型的用户设置? 我确实在对话框类上有一个公共属性,它根据选中的单选按钮获取/设置枚举值,但我没有看到将该属性绑定到用户设置的简单方法。
编辑:应该指定我正在使用 vb.net。 我认为这意味着 My.Settings
而不是 Properties.Settings
。
I have a WinForms dialog box that contains 3 radio buttons. I am using ApplicationSettings
to bind the Checked property of each of these RadioButton
controls, but it doesn't do what I am expecting it to do. Now I have to click each radio button twice before it gets checked and the selected radio button is not being persisted.
Is there a line of code I need to execute when the form is closed that saves the user settings?
How do I eliminate the need for 2x clicking on the radio buttons?
Is there a better way to persist this type of user setting? I do have a public property on the dialog box class that gets/sets an enum value based on which radio button is checked, but I didn't see an easy way of binding that property to a user setting.
Edit: Should have specified that I'm using vb.net. I think that means My.Settings
instead of Properties.Settings
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我回答你问题的这一部分:
您可以将以下事件处理程序添加到 GroupBox 中三个 RadioButton(其中 Checked 属性绑定到应用程序设置)中每一个的 Click 事件中:
它可以工作,即使未选中的 RadioButton 需要半秒的时间。点击后检查。
两年前(2008 年)在 幸存 WinForms 数据绑定 发表在 Turbulent Intelect 博客上(谢谢 ohadsc 提供的链接) :
I answer this part of your question:
You can add the following event handler to the Click event of each of the three RadioButtons (of which the Checked properties are bound to Application Settings) in your GroupBox:
It works, even though it takes a half-second for an unckecked RadioButton to be checked after you click it.
The reason for the problem was explained two years ago (in 2008) in section 5 of the Surviving WinForms Databinding post on the Turbulent Intelect blog (Thank you, ohadsc, for the link):
感谢 Geoffrey Van Wyk 和 ohadsc(提供链接),我想出了以下自定义控件。 它基本上是一个自定义框架,将在其中包含的所有单选按钮上自动实现 Geoffrey 的代码。 优点是您现在可以使用ApplicationSettings 绑定控件的checked 属性,并且它将按预期工作。
这是我用于制作自定义控件的 C# 代码:
Thanks to Geoffrey Van Wyk and ohadsc (for the link), I came up with the following custom control. It's basically a custom frame that will implement Geoffrey's code automatically on all radiobuttons contained in it. The advantage is that you can now use ApplicationSettings to bind the checked property of the control, and it will work as it is supposed to.
Here's my c# code to make the custom control:
我可以回答你问题的这一部分:
应用程序设置存储在
Settings
类的Properties
命名空间中。Settings
类有一个名为Default
的静态属性,它表示应用程序的当前设置。 因此,在主窗体的 Closing 事件中,您可以调用:... 来保存设置。
同样,您可以使用设置名称以编程方式访问设置:
Properties.Settings.Default.MyRadioButtonState
(或您所称的任何名称)。I can answer this part of your question:
Application settings are stored in your
Settings
class, in theProperties
namespace. TheSettings
class has a static property calledDefault
, which represents the current settings for your application. So in your main form's Closing event, you call:... to save the settings.
Likewise you can get to the settings programatically using the setting's name:
Properties.Settings.Default.MyRadioButtonState
(or whatever you've called it).此行为在第 5 项中进行了解释:
http://turbulentintellect.blogspot .com/2008/07/surviving-winforms-databinding.html
一种可能的解决方案是自定义 GroupGox:
对一组单选按钮进行数据绑定的最佳方法在 WinForms 中
This behavior is explained in Item 5 here:
http://turbulentintellect.blogspot.com/2008/07/surviving-winforms-databinding.html
One possible solution would be a custom GroupGox:
Best way to databind a group of radiobuttons in WinForms