通过自己想办法已解决,分享下思路吧,让同样遭遇的人少走一些弯路:
1.延迟读取数据(我是通过调整触发DataReceived事件的字符串长度来弄的)
2.设置一个Boolean变量,用来标记此时的读数是否已经记录,
a.初始状态将这个变量的值设置为 False ,
b.每次记录后,将这个变量值更改为 True,
c.通过总结操作过程,自动记录当前重量时,当天平读取到一次当前重量时 0±0.02时,就标记这个变量的值为 false;同理自动记录重量差后,读取重量差文本框的读数是0±0.02时,标记变量为 false;在此代码下方执行判断后再决定是否记录读数到DGV中。
代码分享如下:
Private Sub TB_DQCZ_TextChanged(sender As Object, e As EventArgs) Handles TB_DQCZ.TextChanged
'Dim t As System.IO.StreamWriter = New System.IO.StreamWriter(RtxtPth, True, System.Text.Encoding.UTF8)
'当 当前称重 变化的时候
'计算并显示重量差
TB_ZLC.Text = Format(Str2Num(TB_SYCCZ.Text) - Str2Num(TB_DQCZ.Text), "0.000")
If CB_ZLQZ.Text = "当前重量" Then
If Str2Num(TB_DQCZ.Text) <= 0.02 Then
ZLYJL = False
End If
End If
If meLoaded = True And RB_zdms.Checked = True And RB_bzzd.Checked = True And CB_ZLQZ.Text = "当前重量" And AllowWritIn = True And Str2Num(TB_DQCZ.Text) > 0.02 And ZLYJL = False Then
WritWeightToDGV() '当前称重不是0 且当前重量未被记录
End If
End Sub
Private Sub TB_ZLC_TextChanged(sender As Object, e As EventArgs) Handles TB_ZLC.TextChanged
'当 重量差 有变化的时候
If CB_ZLQZ.Text = "重量差" Then
If Str2Num(TB_ZLC.Text) <= 0.02 Then
ZLYJL = False
Else
'ZLYJL = True
End If
End If
If meLoaded = True And RB_zdms.Checked = True And RB_bzzd.Checked = True And CB_ZLQZ.Text = "重量差" And AllowWritIn = True And Str2Num(TB_ZLC.Text) > 0.02 And ZLYJL = False Then
WritWeightToDGV() '当前称重不是0 且与上一次记录了的重量之差大于0.01
End If
End Sub
Private Sub TB_SYCCZ_TextChanged(sender As Object, e As EventArgs) Handles TB_SYCCZ.TextChanged
'显示重量差
TB_ZLC.Text = Format(Str2Num(TB_SYCCZ.Text) - Str2Num(TB_DQCZ.Text), "0.000")
End Sub
Private Sub WritWeightToDGV()
ZLYJL = True
Dim W As Decimal = 0
Select Case CB_ZLQZ.Text
Case "当前重量"
W = Str2Num(TB_DQCZ.Text) '记录当前称重及商品信息到 DGV 中
Case "重量差"
W = Str2Num(TB_ZLC.Text) '记录重量差及商品信息到 DGV 中
Case "输入重量"
W = Str2Num(TB_SRZL.Text) '记录输入重量及商品信息到 DGV 中
End Select
DGV_sj.Rows.Add()
DGV_sj.Rows(DGV_sj.RowCount - 2).Cells(DGV_sj_C_zl.Index).Value = Format(W, "0.000")
DGV_sj.FirstDisplayedScrollingRowIndex = DGV_sj.RowCount - 2
TB_SYCCZ.Text = TB_DQCZ.Text '将此次称重数据设为上次称重数据
'SYCJLZL = W
W = Nothing
End Sub