我目前正在编写一个程序,这个程序花了我很长时间,因为我做了任何编程已经超过8年了。
我目前正在使用自定义格式的datetimepicker。
我想要做的是从文本框(tbAccTime)
添加分钟数据被放置在数据网格视图中。
这就是我得到的:
Public Class Form1
Friend myFormat As String = "HH:mm:ss" '"H:mm:ss tt" 'use this for 12 hour time
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myFormat As String = "HH:mm:ss" '"H:mm:ss tt" 'use this for 12 hour time
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = myFormat
DateTimePicker1.ShowUpDown = True
Private Sub bntcalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCalculate.Click
Dim i As Integer, j As Integer, dt As New DataTable, ts As DateTime
dt.Columns.Add("Bottle", Type.GetType("System.Double"))
dt.Columns.Add("Depth", Type.GetType("System.Double"))
dt.Columns.Add("Time", Type.GetType("System.String"))
dt.Columns.Add("Intervals", Type.GetType("System.Double"))
For j = 1 To (cmbBottles.SelectedIndex + 1)
Dim jrow As DataRow
jrow = dt.NewRow
jrow("Bottle") = j
dt.Rows.Add(jrow)
For k = 1 To (cmbBottles.SelectedIndex + 1)
Dim krow As DataRow
krow = jrow
krow("Depth") = k
For l = 1 To (cmbBottles.SelectedIndex + 1)
Dim lrow As DataRow
lrow = jrow
lrow("Intervals") = l
ts = DateTimePicker1.Value
If Double.TryParse(tbAccTime.Text, acc) Then
lrow("Time") = ts.ToString(myFormat)
ts = ts.AddMinutes(acc).ToString(myFormat)
End If
Next
Next
Next
DataGridView1.DataSource = dt
我确实尝试了以下内容:
Public Class Form1
Friend acc As double = tbAccTime
Private Sub bntcalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCalculate.Cl
Dim ts As DateTime
ts = DateTimePicker1.Value
lrow("Time") = ts.ToString(myFormat)
ts.addminutes(acc).ToString(myFormat)
此添加分钟可以随时更改,也可以更大的计算
编辑问题已解决!
Private Sub bntcalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCalculate.Click
Dim i As Integer, j As Integer, dt As New DataTable, ts As DateTime
dt.Columns.Add("Bottle", Type.GetType("System.Double"))
dt.Columns.Add("Depth", Type.GetType("System.Double"))
dt.Columns.Add("Time", Type.GetType("System.String"))
dt.Columns.Add("Intervals", Type.GetType("System.Double"))
ts = DateTimePicker1.Value
If Double.TryParse(tbAccTime.Text, acc) Then
ts = ts.AddMinutes(acc).ToString(myFormat)
End If
For j = 1 To (cmbBottles.SelectedIndex + 1)
Dim jrow As DataRow
jrow = dt.NewRow
jrow("Bottle") = j
dt.Rows.Add(jrow)
jrow("Time") = ts.ToString(myFormat)
此致
丹尼尔
答案 0 :(得分:0)
您正在将double变量的值设置为整个TextBox,而不是其值。
但最重要的是AddMinutes
返回新日期的事实。它不会更改您调用该方法的数据
因此,您需要回读计算结果并将值分配给新的日期时间变量或相同的输入变量。
Friend acc As Double
Private Sub bntcalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCalculate.Cl
Dim ts = DateTimePicker1.Value
if Double.TryParse(tbAccTime.Text, acc) Then
ts = ts.AddMinutes(acc)
lrow("Time") = ts.ToString(myFormat)
End If
End Sub
另请注意,如果您不确定最终用户在TextBox中输入的内容,则需要使用TryParse输入,因为这样可以消除异常的可能性,因为字符串的格式不是double值的正确
编辑要解决不增加的字段时间问题,您需要移动该行
ts = DateTimePicker1.Value
在循环之外。
ts = DateTimePicker1.Value
For j = 1 To (cmbBottles.SelectedIndex + 1)
....
For k = 1 To (cmbBottles.SelectedIndex + 1)
...
For l = 1 To (cmbBottles.SelectedIndex + 1)
...
' increment the ts and use its value for the time field
ts = ts.AddMinutes(acc)
lrow("Time") = ts.ToString(myFormat)
Next
Next
Next