从 ComboBox 获取 DayOfWeek

发布于 2024-11-26 12:38:50 字数 361 浏览 1 评论 0原文

我试图找到一种使用 DayOfWeek.Monday 的方法,但通过在表单上选择它来实现。

因此,如果用户从下拉组合框中选择“星期二”,那么以编程方式它将是DayOfWeek.Tuesday

这是当前的代码:

Do Until dtpEndMonth.DayOfWeek = DayOfWeek.Friday

我想说的是:

Do Until dtpEndMonth.DayOfWeek = DayOfweek.Me.Controls("ComboBox1").Value

有什么想法吗?

I'm trying to find a way of using DayOfWeek.Monday but by selecting it on a form.

So if the user selects Tuesday from a drop down combo box, then programmatically it will be DayOfWeek.Tuesday.

This is the current code:

Do Until dtpEndMonth.DayOfWeek = DayOfWeek.Friday

I want to say something like:

Do Until dtpEndMonth.DayOfWeek = DayOfweek.Me.Controls("ComboBox1").Value

Any ideas?

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

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

发布评论

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

评论(2

ぽ尐不点ル 2024-12-03 12:38:50

如果这种静态方法可行,您可以将字符串直接解析为 DayOfWeek-Enum:

Dim dayOfWeekValue = ComboBox1.SelectedValue
Dim dayOfWeek = DirectCast(System.Enum.Parse(GetType(DayOfWeek), dayOfWeekValue), DayOfWeek)

If this static approach is viable, you could parse the string directly to the DayOfWeek-Enum:

Dim dayOfWeekValue = ComboBox1.SelectedValue
Dim dayOfWeek = DirectCast(System.Enum.Parse(GetType(DayOfWeek), dayOfWeekValue), DayOfWeek)
断舍离 2024-12-03 12:38:50

实现此目的的一种方法是执行以下操作:

' On Form_Load()
Dim daysOfWeek = {New KeyValuePair(Of DayOfWeek, String)(DayOfWeek.Sunday, "Sunday"),
                  New KeyValuePair(Of DayOfWeek, String)(DayOfWeek.Monday, "Monday"),
                  ...,
                  New KeyValuePair(Of DayOfWeek, String)(DayOfWeek.Saturday, "Satruday")}

Me.ComboBox1.DataSource = daysOfWeek
Me.ComboBox1.ValueMember = "Key"
Me.ComboBox1.DisplayMember = "Value"
...

然后,当您想要获取所选值时:

Dim selectedDayOfWeek = CType(Me.ComboBox1.SelectedValue, DayOfWeek)

这种方法利用了让 ComboBox 具有 Display 和 Value 成员的能力(通过DataSource 属性),以便当用户选择一个人性化的显示值,它们实际上是在幕后选择一个“数据值”,这就是您的代码处理的内容。

注意:我没有测试此代码,因此可能存在拼写错误。如果您需要任何澄清或更正,请告诉我。

One way to do this would be by doing something like this:

' On Form_Load()
Dim daysOfWeek = {New KeyValuePair(Of DayOfWeek, String)(DayOfWeek.Sunday, "Sunday"),
                  New KeyValuePair(Of DayOfWeek, String)(DayOfWeek.Monday, "Monday"),
                  ...,
                  New KeyValuePair(Of DayOfWeek, String)(DayOfWeek.Saturday, "Satruday")}

Me.ComboBox1.DataSource = daysOfWeek
Me.ComboBox1.ValueMember = "Key"
Me.ComboBox1.DisplayMember = "Value"
...

Then when you want to get the selected value:

Dim selectedDayOfWeek = CType(Me.ComboBox1.SelectedValue, DayOfWeek)

This approach leverages the ability to have a ComboBox have both a Display and a Value Member (via the DataSource property), so that when the user selects a human-friendly display value, they are actually selecting a "data value" under the covers, which is what your code deal with.

Note: I didn't test this code, so there may be typos. If you need any clarifications or corrections, let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文