Results 1 to 12 of 12

Thread: Showing TextBox Text in a Label which is in another Form [Strange Issue]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    90

    Thumbs up Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Hey, i need your help with this simple but strange issue.
    In Form1 i have the textbox: Form1.Video_Content1.VideoGame.text
    In Form2 i have the label: Game.Text

    PHP Code:
     Private Sub Form2_Load(sender As ObjectAs EventArgsHandles MyBase.Load
            Game
    .Text Form1.Video_Content1.VideoGame.Text

    End Sub 
    Now if i change the text of the TextBox (Form1) from the Properties, it's workings, like this:


    But if i edit the text after i opened the program, The program simply ignore my edits.

    Pleast let me know what do you think.

  2. #2
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    hmm.. i misread the code, so i deleted my reply

  3. #3
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    ok, im going to guess when you open form2, you are creating a new instance.. and thats why it's ignoring your edits..

    if you are creating a new instance (which you dont have to), set the variable of that instance to friend withevents.. dont need the withevents part, but you might later on in development..

    so form1 creates new instance:

    VB.NET Code:
    1. public class form1
    2.  
    3. friend withevents gameOverView as new form2
    4.  
    5. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6. 'code
    7. End Sub
    8.  
    9. end class

    in form2:

    VB.NET Code:
    1. Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         gameOverView.Game.Text = Form1.Video_Content1.VideoGame.Text
    3.  
    4. End Sub


    or you can just not create a new instance, and then the code as you have it will work..

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Whats Video_Content1? why do you assume form2 even knows about form1? You dont pass it's instance.

  5. #5
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Quote Originally Posted by ident View Post
    Whats Video_Content1
    im going to be a betting man and say that is a panel or groupbox...


    Quote Originally Posted by ident View Post
    ? why do you assume form2 even knows about form1? You dont pass it's instance.
    im going to assume again..

    form2 gets opened from form1..

    if it is exposed, he shouldnt need to pass it, in order to access it...

  6. #6
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    O.o i was looking at this a little backwards.. while this isnt the "best" way, this will work without confusing you..

    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     Private Sub OpenForm2(sender As System.Object, e As System.EventArgs) Handles OpenForm2Button.Click
    4.         Dim gameOverView  = New Form2
    5.         gameOverView .Show()
    6.     End Sub
    7.  
    8. End Class

    VB.NET Code:
    1. Public Class Form2
    2.     Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    3.         Me.Game.Text = Form1.Video_Content1.VideoGame.Text
    4.     End Sub
    5. End Class

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    90

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Quote Originally Posted by elielCT View Post
    O.o i was looking at this a little backwards.. while this isnt the "best" way, this will work without confusing you..

    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     Private Sub OpenForm2(sender As System.Object, e As System.EventArgs) Handles OpenForm2Button.Click
    4.         Dim gameOverView  = New Form2
    5.         gameOverView .Show()
    6.     End Sub
    7.  
    8. End Class

    VB.NET Code:
    1. Public Class Form2
    2.     Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    3.         Me.Game.Text = Form1.Video_Content1.VideoGame.Text
    4.     End Sub
    5. End Class
    First of all thank you very much for trying help, I appreciate it.
    I just tried your code and as you can see here i'm getting the same response from the program



    Maybe any suggestions will help me a lot.

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Sounds/looks sort of like like an instance issue, where you think you are setting one form but actually setting another / the default instance,
    Few way you can try, make a Public string property in form2, set it after creating a new instance of the form.

    Or just pass the string in the New instance call, like...
    Code:
    Public Class Form1
    
        Private Sub OpenForm2Button_Click(sender As System.Object, e As System.EventArgs) Handles OpenForm2Button.Click
            Dim gameOverView = New Form2(Me.VideoGame.Text)
            gameOverView.Show()
        End Sub
    
    End Class
    Code:
    Public Class Form2
    
        Private Property gameText As String
    
        Public Sub New(game_text As String)
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            Me.gameText = game_text
        End Sub
    
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Game.Text = Me.gameText
        End Sub
    
    End Class

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    90

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Quote Originally Posted by Edgemeal View Post
    Sounds/looks sort of like like an instance issue, where you think you are setting one form but actually setting another / the default instance,
    Few way you can try, make a Public string property in form2, set it after creating a new instance of the form.

    Or just pass the string in the New instance call, like...
    Code:
    Public Class Form1
    
        Private Sub OpenForm2Button_Click(sender As System.Object, e As System.EventArgs) Handles OpenForm2Button.Click
            Dim gameOverView = New Form2(Me.VideoGame.Text)
            gameOverView.Show()
        End Sub
    
    End Class
    Code:
    Public Class Form2
    
        Private Property gameText As String
    
        Public Sub New(game_text As String)
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            Me.gameText = game_text
        End Sub
    
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Game.Text = Me.gameText
        End Sub
    
    End Class
    unbelievable, I Love You Brah!

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    I told you this in post 4

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    90

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Quote Originally Posted by ident View Post
    I told you this in post 4
    Also thank you, i just didn't understand you

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Showing TextBox Text in a Label which is in another Form [Strange Issue]

    Just for future reference the textbox should be private. You should not access it directly.

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