【问题标题】:Convert input of a textbox to the output of another textbox将一个文本框的输入转换为另一个文本框的输出
【发布时间】:2019-11-24 17:32:17
【问题描述】:

我是 VB 新手,已经学习了大约三个月。我想输入一个文本框,例如。 “1234”并在第二个文本框中将其转换为“一、二、三、四”(带逗号)。没有按钮,只有两个文本框。这是我一直在尝试的代码,它不起作用。我收到错误消息:

对象引用未设置为对象的实例

如果我能得到一个很棒的简单解释,那么任何超级复杂的东西都会让我大吃一惊,让我更难学习。你们太棒了,我已经准备好学习了,谢谢。

Dim boxOne = txtNumber.Text.Split(" "c)
Private Sub TxtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged
    For Each i As Integer In boxOne
        If txtNumber.Text = "1" Then
            lblMessage.Text = "one"
        End If
        If txtNumber.Text = "2" Then
            lblMessage.Text = "two"
        End If
        If txtNumber.Text = "3" Then
            lblMessage.Text = "three"
        End If
        If txtNumber.Text = "4" Then
            lblMessage.Text = "four"
        End If
        If txtNumber.Text = "5" Then
            lblMessage.Text = "five"
        End If
        If txtNumber.Text = "6" Then
            lblMessage.Text = "six"
        End If
        If txtNumber.Text = "7" Then
            lblMessage.Text = "seven"
        End If
        If txtNumber.Text = "8" Then
            lblMessage.Text = "eight"
        End If
        If txtNumber.Text = "9" Then
            lblMessage.Text = "nine"
        End If
        If txtNumber.Text = "10" Then
            lblMessage.Text = "ten"
        End If
    Next
    lblMessage.Text = boxOne
End Sub

【问题讨论】:

  • 你在哪一行得到这个错误?上面示例中的第一行似乎完全在任何类之外。这是不正确的,因为当您尝试拆分 boxOne 时,可能尚未创建它。此外,下面的代码是不合逻辑的。您逐个字符地循环 boxOne 字符,并且对于每个字符,您使用计算出的文本更改相同的标签,因此您只能从最后一个字符中获取值,最后您覆盖最后一行中的所有内容。
  • For..Each的业务是什么?
  • 这就是问题所在,我不知道自己在做什么。错误在第一行。当我只使用 If 语句时,我可以让这些框工作,但就像你说的那样,它每次都会覆盖。我有点认为这是完全错误的,但我不知道如何改正。我应该使用数组还是什么?我只是在黑暗中拍摄,希望有些东西能奏效,还有很多东西要学。 for each 是在拆分后遍历每个数字。
  • 请开启 Option Strict。这是一个两部分的过程。首先对于当前项目 - 在解决方案资源管理器中双击我的项目。选择左侧的编译。在 Option Strict 下拉列表中选择 ON。未来项目的第二个 - 转到工具菜单 -> 选项 -> 项目和解决方案 -> VB 默认值。在 Option Strict 下拉列表中选择 ON。这将使您避免在运行时出现错误。
  • For Each i As Integer In boxOne boxOne 中没有整数。 boxOne 是一个字符串数组。

标签: vb.net winforms error-handling textbox


【解决方案1】:

我建议创建一个映射器,一个可以将一个值映射/转换为另一个值的项目集合。
Dictionary 可用于此任务:给定 Key,它会返回关联的 Value

Private valueConverter As Dictionary(Of String, String) = New Dictionary(Of String, String) From {
    {"0", "Zero"}, {"1", "One"}, {"2", "Two"}, {"3", "Three"}, {"4", "Four"},
    {"5", "Five"}, {"6", "Six"}, {"7", "Seven"}, {"8", "Eight"}, {"9", "Nine"}}

字符串是字符的集合:要解析它,您只需要循环集合。
如果您需要直接处理字符集合,可以使用String.ToCharArray() 方法(例如,Dim charCollection = [TextBox].Text.ToCharArray())。

检查当前字符是否代表数字(Char.IsNumber(char)),如果是则map

Imports System.Text

Dim sb As New StringBuilder()
For Each part As Char In txtNumber.Text
    If Not Char.IsNumber(part) Then Continue For
    sb.Append(valueConverter(part) & ", ")
Next

lblMessage.Text = sb.ToString().TrimEnd({","c, " "c})

StringBuilder 类经常在我们需要连接字符串时使用。由于字符串是不可变的,因此每次将字符串添加到现有字符串时,实际上每次都会生成一个新字符串,这需要进行垃圾回收。
出于这个原因,使用 StringBuilder,代码的性能方式 更好。

【讨论】:

  • 是的,您的代码确实有效!我花了一分钟才弄清楚如何把它放进去。这还是新的。它需要 Imports Systems.text。我试图将您的代码与已经建议的其他代码结合起来。我不知道它是独立的。谢谢吉米
  • 如果您发现缺少程序集或 IDE 通知您,请单击类对象并按 ALT+ENTER 或单击快速操作(灯泡)。在打开的上下文菜单中,您可以选择Import [Missing Assembly]。无论如何我都会添加导入。
【解决方案2】:

公开课表1

Private Sub Str_TextChanged(sender As Object, e As EventArgs) Handles Str.TextChanged
    Dim value As String = ""
    For Each Str As String In Me.Str.Text
        If Str = "1" Then
            If value.Length = 0 Then
                value = "One"
            Else
                value = value & ",One"
            End If
        ElseIf Str = "2" Then
            If value.Length = 0 Then
                value = "Two"
            Else
                value = value & ",Two"
            End If
        ElseIf Str = "3" Then
            If value.Length = 0 Then
                value = "Three"
            Else
                value = value & ",Three"
            End If
        ElseIf Str = "4" Then
            If value.Length = 0 Then
                value = "Four"
            Else
                value = value & ",Four"
            End If
        ElseIf Str = "5" Then
            If value.Length = 0 Then
                value = "Five"
            Else
                value = value & ",Five"
            End If
        ElseIf Str = "6" Then
            If value.Length = 0 Then
                value = "Six"
            Else
                value = value & ",Six"
            End If
        ElseIf Str = "7" Then
            If value.Length = 0 Then
                value = "Seven"
            Else
                value = value & ",Seven"
            End If
        ElseIf Str = "8" Then
            If value.Length = 0 Then
                value = "Eight"
            Else
                value = value & ",Eight"
            End If
        ElseIf Str = "9" Then
            If value.Length = 0 Then
                value = "Nine"
            Else
                value = value & ",Nine"
            End If
        End If
    Next

    'Your needed value is in variable 'value', use it to set it to another TextBox :)
End Sub

结束类

【讨论】:

  • 我想我看到了一个未来的问题,单词前的逗号会使第一个单词显示“,one”,我需要它显示“one”,我认为有一些东西那将用逗号替换空格正确吗?这和修剪有关系吗?
  • 天啊!!有用!!但是您为什么不在原始帖子中添加“ lblMessage.Text = value ”?我以为您将 lblmessage 的名称更改为 value,但是谢谢,它确实有效,谢谢,谢谢
  • 现在我将通过它并尝试了解它的工作原理,再次感谢
【解决方案3】:

我不认为我们在 整数 值上使用 For Each 来获取单个值。尝试在 string 上使用 For Each

代码如下:

Private Sub TxtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged
    Dim value As String
    For Each Str As String In TxtNumber.Text
        If Str = "1" Then
            If value.Length = 0 Then
                value = "One"
            Else
                value = value & ",One"
            End If
        ElseIf Str = "2" Then
            If value.Length = 0 Then
                value = "Two"
            Else
                value = value & ",Two"
            End If
        ElseIf Str = "3" Then
            If value.Length = 0 Then
                value = "Three"
            Else
                value = value & ",Three"
            End If
        ElseIf Str = "4" Then
            If value.Length = 0 Then
                value = "Four"
            Else
                value = value & ",Four"
            End If
        ElseIf Str = "5" Then
            If value.Length = 0 Then
                value = "Five"
            Else
                value = value & ",Five"
            End If
        ElseIf Str = "6" Then
            If value.Length = 0 Then
                value = "Six"
            Else
                value = value & ",Six"
            End If
        ElseIf Str = "7" Then
            If value.Length = 0 Then
                value = "Seven"
            Else
                value = value & ",Seven"
            End If
        ElseIf Str = "8" Then
            If value.Length = 0 Then
                value = "Eight"
            Else
                value = value & ",Eight"
            End If
        ElseIf Str = "9" Then
            If value.Length = 0 Then
                value = "Nine"
            Else
                value = value & ",Nine"
            End If
        End If
    Next
    'Your needed value is in variable 'value', use it to set it to another TextBox :)
End Sub

然后……完成了。

【讨论】:

  • 我需要在底部添加 Next,我将第二个文本框名称更改为 value,去掉了底部的 End If 和 End For(它说我不需要它们)。所有错误都消失了,但我收到一条警告,指出变量“值”在被赋值之前已被使用。运行时可能会导致空引用异常。如此接近,有什么想法吗?也许我复制粘贴错了? (还在学复制粘贴,哈哈,开个玩笑)
  • @omgitscool 将 'Dim value As String' 更改为 'Dim value As String = ""'。
  • 没有错误或警告,但没有任何内容进入第二个文本框。尝试更改名称,但没有任何效果
  • @omgitscool 你能拍下你的代码并给我看看你的代码吗?或者只是粘贴您的新代码。
  • @omgitscool 不,你不需要这样做。该表达式 'If value.Length = 0' 检查字符串是否为空,它们设置其值而不使用逗号,但如果为假,则插入逗号。所以这不是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
相关资源
最近更新 更多