在vb.net中将整数转换为listbox中的字符串?

时间:2018-02-02 02:37:07

标签: .net vb.net

这就是我的意思

    For intGenNum = 1 To txtInput.Text
        lstDisplay.Items.Add(intGenNum)

    Next  

这是输出
  enter image description here

我的问题是如何将1,2和3更改为*,**等等?

3 个答案:

答案 0 :(得分:1)

试试这个:

Dim text = ""
For intGenNum = 1 To Integer.Parse(txtInput.Text)
    text = text + "*"
    lstDisplay.Items.Add(text)
Next

您还应该Option Strict On,因为像For intGenNum = 1 To txtInput.Text这样的行应该是语法错误,以帮助您更好地编码。

答案 1 :(得分:0)

您需要创建一个列表:

 Public Class listofObject
        Private num As String
        Public Property Number() As String
            Get
                Return num
            End Get
            Set(ByVal value As String)
                num = value
            End Set
        End Property
    End Class

Form_load中

 Dim lst As New List(Of listofObject)()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lstDisplay.HorizontalScrollbar = True
    End Sub

Button_Click

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim res As listofObject = New listofObject()
    lstDisplay.Items.Clear()
    lst.Clear()
    Try
        If Integer.Parse(TextBox1.Text) Then
            For m As Integer = 0 To Integer.Parse(TextBox1.Text)
                lst.Add(New listofObject() With {.Number = m})
            Next

            Dim result As String = String.Empty
            Dim ctr As Integer = 1

            Dim im As Integer = 1
            If lst.Count > 0 Then
                For Each num In lst
                    If lst.Count > 1 Then
                        For i As Integer = 0 To num.Number - 1
                            result += "*"
                        Next
                        If im > 1 Then
                            result += ","
                        End If
                        im += 1
                    Else
                            For i As Integer = 0 To num.Number - 1
                            result += "*"
                        Next
                        'result += num.Number.ToString()
                    End If
                Next
                If lst.Count > 1 Then
                    result = result.Substring(0, result.Length - 1)
                End If
                lstDisplay.Items.Clear()
                lstDisplay.Items.Add(result)
            End If
        End If
    Catch ex As Exception
        MessageBox.Show("Input is not a number")
    End Try

End Sub

结果如下:

输入为5,例如

*,**,***,****,*****

仅接受号码。

答案 2 :(得分:0)

对于您拥有的表单:TextBox1ListBox1Button1

Public Class Form2


    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not (e.KeyChar >= "0" And e.KeyChar <= "9") Then
            e.KeyChar = Chr(0)
        End If
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tNum As Integer 'Long - Integer required for StrDup
        Dim tStr As String = ""
        If Len(TextBox1.Text) > 0 Then
            tNum = CInt(TextBox1.Text)  ' could throw an exception if too long for Integer type
        Else
            tNum = 0
        End If

        For iterator = 1 To tNum
            tStr = tStr & StrDup(iterator, "*") & " "
        Next
        ListBox1.Items.Add(Trim(tStr))

    End Sub
End Class

TextBox1_KeyPress确保您只获得数字(即整数正整数)。这样可以减少尝试将字符串转换为数字时的错误检查。

您真正寻求的代码位于Button1_Click事件处理程序中。

Len(TextBox1.Text) > 0是一个错误检查,以确保返回某些内容(我们将知道它只包含数字,因为我们之前的验证)。一个选项是检查IsNumeric

下一个循环采用空字符串tStr并添加越来越多的&#34; *&#34;然后是一个空格。

ListBox1.Items.Add(Trim(tStr))使用Trim命令删除末尾的额外空格,然后添加&#34; *&#34;到列表框。

根据以下评论 - 如果所需结果为&#34; &#34;,&#34; &#34;和&#34; &#34;在单独的行上,而不是&#34; * ** ***&#34;,然后Button1_Click函数是:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tNum As Integer 'Long - Integer required for StrDup
        Dim tStr As String = ""
        If Len(TextBox1.Text) > 0 Then
            tNum = CInt(TextBox1.Text)  ' could throw an exception if too long for Integer type
        Else
            tNum = 0
        End If

        For iterator = 1 To tNum
            tStr = StrDup(iterator, "*")
            ListBox1.Items.Add(Trim(tStr))
        Next

    End Sub

简单的改变:ListBox1.Add进入循环内部而不是外部,tStr不再添加自身(也不需要空间)。