VB.NET App: Get Value from TextBox and stored in Array Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthickbabu
    New Member
    • Sep 2007
    • 33

    VB.NET App: Get Value from TextBox and stored in Array Variable

    Hi

    Is Possible to store a value to declared Variable from Text Box at run time. I want to store a value from Text Box in 2 dimension array. First Value can be sotred in variable(0,0). If i press enter text box will be cleared and get another number and it stored in variable(0,1) and so on

    I try the code as below but not working. I write this in Text Box KeyDown Event. If not in which event we write the code

    [code=vbnet]
    Try
    If e.KeyCode = Keys.Enter Then
    For i As Integer = 0 To 1
    For j As Integer = 0 To 1
    FirstMatrixArra y(i, j) = val(txtInput.Te xt)
    txtInput.Text = ""
    txtInput.Focus( )
    Next
    Next
    End If
    Catch ex As Exception
    MessageBox.Show (ex.Message())
    End Try
    [/code]
    hope ur reply
    Last edited by Frinavale; Nov 21 '07, 09:42 PM. Reason: Added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by karthickbabu
    Hi

    Is Possible to store a value to declared Variable from Text Box at run time. I want to store a value from Text Box in 2 dimension array. First Value can be sotred in variable(0,0). If i press enter text box will be cleared and get another number and it stored in variable(0,1) and so on

    I try the code as below but not working. I write this in Text Box KeyDown Event. If not in which event we write the code

    [code=vbnet]
    Try
    If e.KeyCode = Keys.Enter Then
    For i As Integer = 0 To 1
    For j As Integer = 0 To 1
    FirstMatrixArra y(i, j) = val(txtInput.Te xt)
    txtInput.Text = ""
    txtInput.Focus( )
    Next
    Next
    End If
    Catch ex As Exception
    MessageBox.Show (ex.Message())
    End Try
    [/code]
    hope ur reply

    I have a feeling that the string is getting lost when you clear the txtInput.Text. Remember that everything in VB.NET is a pointer...if you clear the value at the memory location where that pointer is pointing to....anything else referencing that memory location will be lost too.

    So I suggest creating a new string based on the txtInput.Text to store in your FirstMatrixArra y.

    Eg:
    [code=vbnet]
    FirstMatrixArra y(i, j) = New String(txtInput .Text)[/code]

    Hope this helps!
    -Frinny

    Comment

    • karthickbabu
      New Member
      • Sep 2007
      • 33

      #3
      Originally posted by Frinavale
      I have a feeling that the string is getting lost when you clear the txtInput.Text. Remember that everything in VB.NET is a pointer...if you clear the value at the memory location where that pointer is pointing to....anything else referencing that memory location will be lost too.

      So I suggest creating a new string based on the txtInput.Text to store in your FirstMatrixArra y.

      Eg:
      [code=vbnet]
      FirstMatrixArra y(i, j) = New String(txtInput .Text)[/code]

      Hope this helps!
      -Frinny



      No Frinny still it lose tha data in that Array Variable. I got only one value what i type last in that TextBox. But if i give msg box to display. It display with Exception

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by karthickbabu
        No Frinny still it lose tha data in that Array Variable. I got only one value what i type last in that TextBox. But if i give msg box to display. It display with Exception
        Hmm I took a look at your question again.
        Are you sure you want to use the Text Box KeyDown Event?
        Should you be using the KeyUp Event?

        Comment

        • karthickbabu
          New Member
          • Sep 2007
          • 33

          #5
          I try in key up event no improvements, If i use

          new string(textbox1 .text)

          it shows exception "converison from string to integer type is not valid ".
          [code=vbnet]
          For i As Integer = 0 To FirstMatrixArra y.GetUpperBound (0)
          For j As Integer = 0 To FirstMatrixArra y.GetUpperBound (0)
          'value = CInt(txtFirstIn put.Text)
          FirstMatrixArra y(i, j) = CInt((txtFirstI nput.Text).ToSt ring)
          MsgBox(FirstMat rixArray(i, j))
          txtFirstInput.T ext = ""
          txtFirstInput.F ocus()
          Next
          Next
          [/code]
          Just i test using msg box it displays in msgbox whatever i give in text box but with excetion, i used conversion type also but no changes in my result.

          i post the same question in some other forum. Some body replies you should not be programming, values cannot be stored at run time. They suggest like these only

          val(0,0)= 2
          val(0,1) = 3
          .
          .
          .and so on


          Originally posted by Frinavale
          Hmm I took a look at your question again.
          Are you sure you want to use the Text Box KeyDown Event?
          Should you be using the KeyUp Event?
          Last edited by Frinavale; Dec 4 '07, 02:32 PM. Reason: Added [code] tags

          Comment

          Working...