Results 1 to 4 of 4

Thread: Get Value from a Class to a TextBox in a Form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Get Value from a Class to a TextBox in a Form

    In this code I can Get SerialNumber and Name in MsgBox. I also would like to put them in three TextBox in a Form. How can I do this ?. Thank you.

    Class Code :

    Code:
    Public Class Item
    
        Public Shared Count As Integer = 1
        Private userNameValue As String
        Public Shared Sub ShareMethod()
            MsgBox("Current value of Count: " & Count)
            
        End Sub
    
        Public Sub New(ByVal Name As String)
            ' Use Count to initialize SerialNumber. 
            Me.SerialNumber = Count
            Me.Name = Name
            ' Increment the shared variable
            Count += 1
        End Sub
        Public SerialNumber As Integer
        Public Name As String
    
        Public Sub InstanceMethod()
            MsgBox("Information in the first object: " &
                Me.SerialNumber & vbTab & Me.Name)
            
        End Sub
    
    End Class
    Form Code :

    Code:
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TestShared()                
        End Sub
        
    Sub TestShared()
            ' Create two instances of the class. 
            Dim part1 As New Item("keyboard")
            Dim part2 As New Item("monitor")
    
            part1.InstanceMethod()
            part2.InstanceMethod()
            Item.ShareMethod()
        End Sub
    End Class
    Last edited by ebellounisoft; Aug 8th, 2017 at 08:03 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Get Value from a Class to a TextBox in a Form

    This seems like a rather odd question. If you want to get the SerialNumber and Name values then go ahead and do it. They're both Public. What's stopping you? If you want to then display those values in TextBoxes, what's stopping you? How do you usually display something in a Textbox? Why would this be any different? I can't help thinking that you have just copied some code form somewhere and given little thought to how it actually works, then asked how to do something without actually thinking about how you might do it first. The answer to your question should be quite obvious.

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Get Value from a Class to a TextBox in a Form

    Name and SerialNumber are public members so you can retrieve their value from outside the class like this
    VB.NET Code:
    1. Dim part1 As New Item("keyboard")
    2.        
    3.     TextBox1.Text = part1.Name
    4.     TextBox2.Text = part1.SerialNumber.ToString



  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Get Value from a Class to a TextBox in a Form

    Works !!!. Thank you 4x2y. Best Regards.
    Last edited by ebellounisoft; Aug 9th, 2017 at 09:14 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width