如何解决此问题?将用输入框输入的值打印到文本框,然后将值从文本框传输到数组

时间:2018-12-14 20:20:38

标签: vb.net

Public Class Form1

    Dim girilensayı As Double

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        girilensayı = InputBox("bir sayı gir")

        If TextBox1.Text = "" Then
            TextBox1.Text = girilensayı
        Else
            TextBox1.Text += " " & girilensayı
        End If

我需要将文本框值转移到数组中

2 个答案:

答案 0 :(得分:0)

严格启用

公共类表格1

Dim numberofValues As Integer
Dim Inputtext As String
Dim textArray(numberofValues) As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Inputtext = InputBox("Enter a Number")

    If TextBox1.Text = "" Then
        TextBox1.Text = Inputtext
        textArray(numberofValues) = Inputtext
        numberofValues += 1
    Else
        ReDim Preserve textArray(numberofValues)
        TextBox1.Text += " " & Inputtext
        textArray(numberofValues) = Inputtext
        numberofValues = numberofValues + 1
    End If

End Sub

结束班级

上面的代码将通过ReDim Preserve增加数组的大小,从而将添加到文本框中的所有值添加到数组中。我已经测试了代码,并且可以正常工作。它们不是从文本框中传输它们,而是将值添加到文本框中时将它们添加到数组中。您的表单将需要一个带有正确名称的按钮和文本框。

答案 1 :(得分:0)

在线发表评论和解释。

'I am using a List because we don't know how
    'many elements we will have. This avoids a
    'series of ReDim Preserve in your code.
    Private lstDouble As New List(Of Double)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'InputBox returns a string so girilensayı is a string
        Dim girilensayı = InputBox("bir sayı gir")
        Dim myNumber As Double
        'TryParse will return True or False and fill in myNumber
        If Double.TryParse(girilensayı, myNumber) Then
            'I used the .ToString method so I could use a 
            'format string. "N2" will display your number
            'to 2 decimal places.
            TextBox1.Text &= myNumber.ToString("N2") & ", "
            'Add the number to the list
            lstDouble.Add(myNumber)
        Else
            MessageBox.Show("Please enter a valid number.")
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'If you need an array later you can lstDouble.ToArray
        Dim arrDouble() As Double = lstDouble.ToArray()
    End Sub