【问题标题】:How to validate a parameter from another form in Visual Basic?如何在 Visual Basic 中验证另一个表单的参数?
【发布时间】:2020-03-03 05:53:00
【问题描述】:

我有两个表格,表格 1 和表格 2。我需要从 form2 中调用 form1 的一个参数,以便在 form2 中进行验证。

这是form1中的代码:

Public Sub New(ByVal parametro1 As Integer)

    InitializeComponent()
    TXB_MontoARetirar.Text = param1



End Sub

Public Sub New()

    InitializeComponent()
End Sub 

这是form2中的代码:

Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click

    Dim FRM As FRM_RetiroEfectivo2

    FRM = New FRM_RetiroEfectivo2()



    If FRM.TXB_MontoARetirar.Text = 100 Then

        FRM_RetireSuEfectivo1_100pesos.Show()
        Me.Hide()


    Else

        FRM_RetireSuEfectivo1.Show()
        Me.Hide()

    End If

End Sub

当我运行程序时,我收到以下错误:

System.InvalidCastException: '从字符串""到类型'Double'的转换无效。'

内部异常 FormatException: 输入字符串的格式不正确。

【问题讨论】:

  • 尝试:If CInt(FRM.TXB_MontoARetirar.Text) = 100 Then 但您可能必须执行其他验证以确保用户输入数字。
  • 我试过那个命令,但它没有用。现在我收到以下错误: System.InvalidCastException: 'Conversion from string"" to type 'Integer' is not valid.'内部异常 FormatException:输入字符串的格式不正确。
  • @JesusOvalles 这就是他在代码之后告诉你的部分:你需要在你投射之前验证用户输入了一个数字。
  • 其实我有几个表格,我想通过两个表格的例子来解释一下,以便更容易理解。问题是当我在文本框中输入值 100 时出现该错误。
  • 请开启 Option Strict。这是一个两部分的过程。首先对于当前项目 - 在解决方案资源管理器中双击我的项目。选择左侧的编译。在 Option Strict 下拉列表中选择 ON。未来项目的第二个 - 转到工具菜单 -> 选项 -> 项目和解决方案 -> VB 默认值。在 Option Strict 下拉列表中选择 ON。这将使您避免在运行时出现错误。

标签: vb.net forms validation button textbox


【解决方案1】:

尝试类似:

    If IsNumeric(FRM.TXB_MontoARetirar.Text) Then
         If CInt(FRM.TXB_MontoARetirar.Text) = 100 Then
            FRM_RetireSuEfectivo1_100pesos.Show()
            Me.Hide()
         End If
    Else
            FRM_RetireSuEfectivo1.Show()
            Me.Hide()         
    End If

【讨论】:

  • 当我尝试该命令时,它不允许我点击 PictureBox4。
  • @JesusOvalles 如果您将语句msgbox("'" & FRM.TXB_MontoARetirar.Text & "'") 添加到Else 块中,当您输入100 时您会看到究竟什么?似乎还有其他事情发生。
  • @daShier 在 .net 中最好使用 .TryParse 而不是 IsNumeric。
  • 我正在模拟 ATM。从一个特定的表格(不是从我输入数字 100 的表格),我需要用不同类型的账单(钱)调用几个表格(不是同时)。如果我输入100,我可以和其他人调用100的账单表格(钱)等等。
【解决方案2】:
Private Sub OPCode()
    If TXB_MontoARetirar.Text.Length = 0 Then
        MsgBox("Debe llenar el campo.")
        Return
    End If
    If TXB_MontoARetirar.Text = "0" Then
        MsgBox("Introduzca el monto a retirar.")
        Return
    End If
    Dim intMonto As Integer
    If Not Integer.TryParse(TXB_MontoARetrirar.Text, intMonto) Then
        MessageBox.Show("Please enter a number in the text box.")
        Return
    End If
    Select Case intMonto
        Case > 10000
            MsgBox("Solo es posible retirar hasta RD$10000.00 en los cajeros.")
            Return
        Case > 6000
            MsgBox("Fondos Insuficientes. Favor intentarlo otra vez.")
            Return
    End Select

    Dim NumMultiplo2 = intMonto Mod 100
    Dim NumMultiplo3 = intMonto Mod 200
    Dim NumMultiplo4 = intMonto Mod 500
    Dim NumMultiplo5 = intMonto Mod 1000
    Dim NumMultiplo6 = intMonto Mod 2000
    If NumMultiplo2 = 0 OrElse NumMultiplo3 = 0 OrElse NumMultiplo4 = 0 OrElse NumMultiplo5 = 0 OrElse NumMultiplo6 = 0 Then
        Dim Respuesta = MessageBox.Show("Desea imprimir un recibo?", "Imprimir Recibo", MessageBoxButtons.YesNo)
        If Respuesta = DialogResult.Yes Then
            FRM_RetireSuTarjeta.Show()
        Else
            FRM_RetireSuTarjeta1.Show()
        End If
        TXB_MontoARetirar.Text = ""
        Me.Hide()
    Else
        MsgBox("Solo se pueden realizar retiros en multiplos de billetes de RD$100.00, RD$200.00, RD$500.00, RD$1000.00 y RD$2000.00 pesos.")
    End If
End Sub

我不知道这段代码在做什么,但也许,看到它的简化将帮助您了解它哪里出了问题。那些嵌套的 If 语句失控了。当您一遍又一遍地编写相同的代码时,请创建一个 Sub 或 Function。在这种情况下,我只是将它们全部组合到一个 IF 中。

【讨论】:

  • 我正在模拟 ATM。从一个特定的表格(不是从我输入数字 100 的表格),我需要用不同类型的账单(钱)调用几个表格(不是同时)。如果我输入100,我可以和其他人调用100的账单表格(钱)等等。
猜你喜欢
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多