选择时,组合框不会计算 [英] Combo boxes will not calculate when selected

查看:47
本文介绍了选择时,组合框不会计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这给我带来了一些问题,我有两个在表单加载时填充的combox,其中包含一小时的开始时间和一个工作班次的结束时间,一旦选择了combo1时间和combo2时间,则时间差应为小时显示在一个标签,但它让我选择但只显示小时为00:00:00任何人都可以看到我做错了请它可能是这么简单我不能再看到的东西,我仍然是一个vb初学者所以任何建议会很有帮助的,谢谢



我尝试了什么:



my到目前为止的代码是:



在加载时填充组合

'设置开始时间值

昏暗的开始As DateTime = DateTime.ParseExact(00:00,HH:mm,Nothing)

'设置结束时间值

Dim [end] As DateTime = DateTime.ParseExact(23:59,HH:mm,Nothing)

'设置间隔时间

Dim interval As Integer = 15

'列表保持间隔值

Dim lstTimeIntervals As New List(Of String)()
'用15分钟间隔值填充列表

Dim i As DateTime = start

虽然我< = [end]

ComboBox1.Items.Add(i.ToString(HH:mm tt))

ComboBox2.Items.Add(i.ToString(HH:mm tt))

i = i.AddMinutes(interval)

End而

然后



Private Sub ComboBox2_SelectedIndexChanged(发送者为对象,e为EventArgs)处理ComboBox2.SelectedIndexChanged



昏暗的持续时间As New TimeSpan()

Dim MyVariable1 As TimeSpan

Dim MyVariable2 As TimeSpan



MyVariable2 = ComboBox2.SelectedValue()

MyVariable1 = ComboBox1.SelectedValue()



duration = MyVariable2.Subtract(MyVariable1 )



Label3.Text = duration.ToString



End Sub

So this is causing me some problems, i have 2 comboxes that are populated on form load with hours for a start time and an end time to a work shift, once combo1 time and combo2 time is selected the time difference in hours should be displayed in a label, but it lets me select but only displays the hours as 00:00:00 can anyone see what i have done wrong please its probably something so simple i can no longer see, im still a vb beginner so any advice would be helpful, thanks

What I have tried:

my code so far is :

populate the combos on load
'Set the Start Time Value
Dim start As DateTime = DateTime.ParseExact("00:00", "HH:mm", Nothing)
'Set the End Time Value
Dim [end] As DateTime = DateTime.ParseExact("23:59", "HH:mm", Nothing)
'Set the interval time
Dim interval As Integer = 15
'List to hold the values of intervals
Dim lstTimeIntervals As New List(Of String)()
'Populate the list with 15 minutes interval values
Dim i As DateTime = start
While i <= [end]
ComboBox1.Items.Add(i.ToString("HH:mm tt"))
ComboBox2.Items.Add(i.ToString("HH:mm tt"))
i = i.AddMinutes(interval)
End While
Then

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

Dim duration As New TimeSpan()
Dim MyVariable1 As TimeSpan
Dim MyVariable2 As TimeSpan

MyVariable2 = ComboBox2.SelectedValue()
MyVariable1 = ComboBox1.SelectedValue()

duration = MyVariable2.Subtract(MyVariable1)

Label3.Text = duration.ToString

End Sub

推荐答案

首先,使用描述性名称命名控件,即使它是测试应用程序,也需要花费更多时间,但从长远来看总会节省您的时间。

其次,了解如何使用调试器,您将从调试中学到更多东西,而不是其他任何东西,以下链接将有所帮助;

调试器基础知识| Microsoft Docs [ ^ ]

使用调试器浏览代码 [ ^ ]

关于对于您的问题,以下内容将有所帮助;

Firstly, name your controls with descriptive names, even if it is a test application, it takes a little more time but will always save you time in the long run.
Secondly, learn how to use a debugger, you will learn much more from debugging than pretty much anything else, the following links will help;
Debugger Basics | Microsoft Docs[^]
Navigating through Code with the Debugger[^]
In regards to your question, the following will help;
' Use Date object instead of TimeSpan to get your values
Dim datStart as New Date
Dim datEnd as New Date
' Convert the values from the ComboBox
' NOTE: Selected Value will not be valid unless you set the Value member, hence use Text property
datStart = DateTime.ParseExact(ComboBox1.Text, "HH:mm tt", Nothing)
datEnd = DateTIme.ParseExact(ComboBox2.Text, "HH:mm tt", Nothing)
' A timespan is returned by arithmetic operations on DateTime objects
Dim tsDuration as New TimeSpan()
tsDuration = datEnd - datStart





希望这有帮助



亲切的问候



Hope this helps

Kind Regards


这篇关于选择时,组合框不会计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆