如何清除文字栏位 [英] how to clear the text fields

查看:92
本文介绍了如何清除文字栏位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个发票项目,我在这里面临的问题是-我有4个文本框,在其中输入2个值(例如在计算器中,您要添加3个数字),在第3个中,显示输出.现在,当我在前三个文本框中输入3个值时,显示的结果与上一个值(即第4个框)相同..有人可以帮忙吗
我正在使用VB.NET 2008版本.

如果您不明白,请告知.我会尝试使它更清晰..

hi Im doing an invoice project and the problem i face here is - I got 4 text boxes in which i type in 2 values [like in a calculator ,where u add 3 numbers] and in the 3rd one the output is displayed. now when i type in 3 values [in the first 3 text boxes] the result displayed is the same as the previous value ie 4th box..Can someone help
Im using VB.NET 2008 ver..

If you didn''t understand do inform .I''l try to make it more clear..

Private Sub q1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles q1.TextChanged
Dim amt As Integer
amt = Val(d1.Text) * Val(q1.Text) * Val((un1.Text) / 30)
amt1.Text = Int(amt) Val(d1.Text)
Me.Refresh()
End Sub

推荐答案

我"看到了4个文本框:
d1-输入值,
q1-输入值,
un1-输入值,
amt1-结果.

要解决您的问题,只需添加新按钮,然后将q1_TextChanged()过程的主体移至Button1_Click()过程.
要清除文本框,请在下面复制代码并将其粘贴在Me.Refresh()命令之前:
I "see" 4 textboxes:
d1 - input value,
q1 - input value,
un1 - input value,
amt1 - result.

To solve your problem, just add new button and move the body of q1_TextChanged() procedure into Button1_Click() procedure.
To clear textboxes, copy code below and paste it before Me.Refresh() command:
d1.text = ""
q1.text = ""
un1.text = ""


现在,您的文本框清晰了.


**************** 第二个解决方案-逐步使用自定义的类和接口 ****************

1)创建一个新的(Windows应用程序)项目
2)添加4个标签和4个texbox,并按如下所示更改它们的名称:TxtVal1,TxtVal2,TxtVal3,TxtVal4
3)添加1个按钮(名称:BtnCalc)
4)添加新的界面 [ 5)复制并粘贴以下代码:


Now, your textboxes are clear.


****************Second solution - step by step with custom Class and Interface****************

1) Create new (windows application) project
2) Add 4 lables and 4 texboxes and change them names as follows: TxtVal1, TxtVal2, TxtVal3, TxtVal4
3) Add 1 button (Name: BtnCalc)
4) Add new Interface[^] (menu Project->Add new item->Interface) and save it as "ITestCalc.vb". We need it to correspond with between class and form.
5) Copy and paste code below:

Public Interface ITestCalc
    Function Calc() As Integer
    Function Calc(ByVal _d1 As Integer, ByVal _q1 As Integer, ByVal _un1 As Integer) As Integer
    Property D1() As Integer
    Property Q1() As Integer
    Property Un1() As Integer
End Interface


6)添加新的
[ 7)复制以下代码并将其粘贴到您的班级中:


6) Add new Class[^] to the project (Menu Project->Add new item->Class) and save it as "TestCalc.vb"
7) Copy code below and paste it into your class:

Public Class TestCalc
    Implements ITestCalc

    Private d1 As Integer = 0
    Private q1 As Integer = 0
    Private un1 As Integer = 0

    'default constructor
    Public Sub New()
        'do nothing
    End Sub

    'constructor with input values
    Public Sub New(ByVal _d1 As Integer, ByVal _q1 As Integer, ByVal _un1 As Integer)
        d1 = _d1
        q1 = _q1
        un1 = _un1
    End Sub


    Public Function Calc() As Integer Implements ITestCalc.Calc
        Dim retVal As Integer = 0

        retVal = d1 * q1 * (un1 / 30)

        Return retVal
    End Function

    Function Calc(ByVal _d1 As Integer, ByVal _q1 As Integer, ByVal _un1 As Integer) As Integer Implements ITestCalc.Calc
        d1 = _d1
        q1 = _q1
        un1 = _un1
        Return Calc()
    End Function

    Public Property D11() As Integer Implements ITestCalc.D1
        Get
            Return d1
        End Get
        Set(ByVal value As Integer)
            d1 = value
        End Set
    End Property

    Public Property Q11() As Integer Implements ITestCalc.Q1
        Get
            Return q1
        End Get
        Set(ByVal value As Integer)
            q1 = value
        End Set
    End Property

    Public Property Un11() As Integer Implements ITestCalc.Un1
        Get
            Return un1
        End Get
        Set(ByVal value As Integer)
            un1 = value
        End Set
    End Property

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class


8)在形式(类)中:


8) In the form (class):

Public Class Form1
    'declare variable
    Private oCalc As ITestCalc = New TestCalc()

    Private Sub BtnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalc.Click
        Try
            oCalc.D1 = Int(Me.TxtVal1.Text)
            oCalc.Q1 = Int(Me.TxtVal2.Text)
            oCalc.Un1 = Int(Me.TxtVal3.Text)
            Me.TxtResult.Text = oCalc.Calc().ToString
            'or
            'Me.TxtResult.Text = oCalc.Calc(Int(Me.TxtVal1.Text), Int(Me.TxtVal2.Text), Int(Me.TxtVal3.Text)).ToString

        Catch ex As InvalidCastException
            MsgBox(ex.Message & vbCr & vbCr & _
                "Enter correct values!", MsgBoxStyle.Exclamation, "Error!")

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
        End Try
    End Sub

    Protected Overrides Sub Finalize()
        oCalc = Nothing
        MyBase.Finalize()
    End Sub
End Class



如果我的回答对您有帮助,请对其评分.



If my answer was helpful, please rate it.


您需要做的第一件事是检查您是否为两个文本框设置了处理程序,而不仅仅是一个文本框-您需要对其进行处理d1和d1(也可能是un1).
然后更改乘法以将分度放置在正确的位置...将Value而不是下一个Text属性进行分频将是一个好主意!
您还应该能够删除Me.Refresh-不需要它,因为Text Property分配应该使用它.

如果那不能解决问题,则需要使用调试器并执行单个步骤.从"amt = ..."行上的断点开始,然后继续进行下一步以检查发生了什么.每次在任何一个文本框中键入内容时,都应该获得一个断点.
The first thing you need to do is check that you have a handler set for two textboxes, not just one - you need to handle it for d1 and d1 (and probably un1 as well).
Then change the multiplication to put the divide in the right place...dividing the Value rather than the next Text property would be a good idea!
You should also be able to remove the Me.Refresh - you shouldn''t need it as the Text Property assignment should take take of it.

If that doesn''t fix it, you need to use the debugger and single step. Start with a breakpoint on the "amt = ..." line, and follow though with single step to check what is happening. You should get a breakpoint each time you type into either of the text boxes.


这篇关于如何清除文字栏位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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