VB.NET>文本框总和是背后的一个重点 [英] VB.NET > Text box sum is one keypress behind

查看:114
本文介绍了VB.NET>文本框总和是背后的一个重点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是VB.NET的新手还是学习曲线。



我正在申请工作,我们有10个文本框,5表示小时,5表示间接时间,我可以输入文本到所有正常,他们更新标签文本没问题。问题是当我在文本框中输入时,''净小时''文本框总是落后1个数字,例如,如果我输入1,然后是4,则它将在网络小时文本框中显示1我打字4.我对此感到非常困惑。



这是我的代码:

Hi, Firstly, I am fairly new to VB.NET and still in the learning curve.

I am working on an application for work and we have 10 text boxes, 5 that are for Hours and 5 for Indirect Hours, I can enter text in to them all fine and they update label text no problem. The problem is when I am typing in the text boxes, the ''Net Hour'' text box is always 1 number behind, for example, if I type 1, then 4, it will then show 1 in the Net Hour text box when I type 4. I am getting really confused by this.

Here is my code:

Private Sub txtClockHour_TextChanged(sender As Object, e As EventArgs) Handles txtClockHour.TextChanged, txtIndirectHour.TextChanged

    '   This changes the Clocked Hours text box to white when text changed, for errors / missing information.

    Dim TotNum1 As Integer
    Dim TotNum2 As Integer
    Dim TotNum3 As Integer
    Dim TotNum4 As Integer
    Dim TotNum5 As Integer

    TotNum1 = Integer.Parse(lblTot1.Text)
    TotNum2 = Integer.Parse(lblTot2.Text)
    TotNum3 = Integer.Parse(lblTot3.Text)
    TotNum4 = Integer.Parse(lblTot4.Text)
    TotNum5 = Integer.Parse(lblTot5.Text)

    txtClockHour.BackColor = Color.White

    '   This below calculates the Clocked Hours multiplied by the Indirect Hours text boxes, if no Indirect Hours, it makes the Net Hour box equal the same as Clocked Hours.

    If txtClockHour.Text > "" And txtIndirectHour.Text > "" Then
        lblTot1.Text = (CDec(txtClockHour.Text)) - (CDec(txtIndirectHour.Text))
        txtNetHour.Text = (CInt(TotNum1)) + (CInt(TotNum2)) + (CInt(TotNum3)) + (CInt(TotNum4)) + (CInt(TotNum5))
    Else
        txtNetHour.Text = (CInt(TotNum1)) + (CInt(TotNum2)) + (CInt(TotNum3)) + (CInt(TotNum4)) + (CInt(TotNum5))
    End If

End Sub





请帮忙。谢谢。



Please help. Thanks.

推荐答案

你不能这样做:

You can''t do that in this way:
If txtClockHour.Text > "" And txtIndirectHour.Text > "" Then



了解更多关于:运营商和Visual Basic中的表达式 [ ^ ]



使用 String.Length property [ ^ ]:


Read more about: Operators and Expressions in Visual Basic[^]

Check the length of string, using String.Length property[^]:

If txtClockHour.Text.Length > 0 And txtIndirectHour.Text.Length > 0 Then





我建议你创建课程。如何:在VB.NET中创建类 [ ^ ]。





感谢您的帮助,我设法使用MacieJ Los回答创建了一个''sub''而不是一个类并调用sub,如下所示。谢谢。



Sub

Hi,

Thank you both for you help, I managed to do this using MacieJ Los answer by creating a ''sub'' instead of a class and calling the sub, like below. Thank you.

Sub
Public Sub TotNum1()

    Dim TotNum1 As Integer
    Dim TotNum2 As Integer
    Dim TotNum3 As Integer
    Dim TotNum4 As Integer
    Dim TotNum5 As Integer

    TotNum1 = Integer.Parse(lblTot1.Text)
    TotNum2 = Integer.Parse(lblTot2.Text)
    TotNum3 = Integer.Parse(lblTot3.Text)
    TotNum4 = Integer.Parse(lblTot4.Text)
    TotNum5 = Integer.Parse(lblTot5.Text)

    txtNetHour.Text = (CInt(TotNum1)) + (CInt(TotNum2)) + (CInt(TotNum3)) + (CInt(TotNum4)) + (CInt(TotNum5))

End Sub





TextChanged事件



TextChanged event

Private Sub txtClockHour_TextChanged(sender As Object, e As EventArgs) Handles txtClockHour.TextChanged, txtIndirectHour.TextChanged

    '   This changes the Clocked Hours text box to white when text changed, for errors / missing information.

    txtClockHour.BackColor = Color.White

    '   This below calculates the Clocked Hours multiplied by the Indirect Hours text boxes, if no Indirect Hours, it makes the Net Hour box equal the same as Clocked Hours.

    If txtClockHour.Text.Length > 0 And txtIndirectHour.Text.Length > 0 Then
        lblTot1.Text = (CDec(txtClockHour.Text)) - (CDec(txtIndirectHour.Text))
        Call TotNum1()
    Else
        Call TotNum1()
    End If

End Sub


这篇关于VB.NET>文本框总和是背后的一个重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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