Results 1 to 13 of 13

Thread: How do I alter a label on one form from another form?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    9

    How do I alter a label on one form from another form?

    Hi there!

    I have a label called 'Label1' on a form called 'Deck1Form' and I have another form called 'PlaylistForm' on this form I have a listbox with some data. I want to change 'Label1' on form 'Deck1Form' to the value that is currently selected,

    I've tried:

    Code:
    Deck1Form.Label1.Text = Playlist.SelectedItem
    But this doesn't want to work,

    Hope you can help!

    MrGeeza

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How do I alter a label on one form from another form?

    What happens when you execute that line of code. Is there an error or does it do nothing ?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    9

    Re: How do I alter a label on one form from another form?

    Nothing happens. I select an item from the list and it stay's as the default 'Label1' text

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How do I alter a label on one form from another form?

    Could you post more code so I can have an idea of what you're doing ?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    9

    Re: How do I alter a label on one form from another form?

    Well there isn't much to show, but here it is:

    Code:
    Public Class PlaylistForm
    
        Private Sub ImportToPlaylist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportToPlaylist.Click
            ImportDialog.ShowDialog()
        End Sub
    
        Public Sub ImportDialog_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ImportDialog.FileOk
            For Each Track As String In ImportDialog.FileNames
                Playlist.Items.Add(Track)
            Next
        End Sub
    
        Public Sub Playlist_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Playlist.Click
    
            ItemSelectedOnPlaylist = Playlist.SelectedItem
    
            Deck1Form.Label1.Text = Playlist.SelectedItem
    
        End Sub
    
    End Class

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How do I alter a label on one form from another form?

    I see no reason in your code why it shouldn't work.
    Place a break point in the Playlist's click event to make sure that the event is working right, also check the value of SelectedItem while you're there(assuming it breaks).
    Last edited by Niya; Jan 15th, 2012 at 08:15 PM. Reason: Re-factoring

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I alter a label on one form from another form?

    Is Deck1Form the startup form for the application? If not, how EXACTLY is it displayed? You should almost certainly not be handling the Click event of Playlist, but rather the SelectedIndexChanged event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How do I alter a label on one form from another form?

    Quote Originally Posted by jmcilhinney View Post
    You should almost certainly not be handling the Click event of Playlist, but rather the SelectedIndexChanged event.
    I endorse this 100% however it is not the reason that the code isnt working. Selecting an item in the list using the mouse should change the label.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    9

    Re: How do I alter a label on one form from another form?

    I've given it some thought. The program loads up a main window, with a menu bar and an MDI area. All the forms are child forms of the main mdi area. Would this effect it?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I alter a label on one form from another form?

    So:
    Is Deck1Form the startup form for the application? If not, how EXACTLY is it displayed?
    Can we assume that that's a "no" on the first one? If so, EXACTLY would include the code used.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    9

    Re: How do I alter a label on one form from another form?

    No sorry. It's not the startup form. The startup form is called 'MainWindowForm' then using the menu bar at the top called 'MenuStrip1' you select the link called 'PlaylistToolStripMenuItem' which opens up a child form called 'Deck1Form'. To open that form, this code runs,

    Code:
    Dim MDIForm As New PlaylistForm
    MDIForm.MdiParent = Me
    MDIForm.Show()
    Hope this helps

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I alter a label on one form from another form?

    I asked you (twice) how Deck1Form was displayed, not how PlaylistForm was displayed. That said, I'm guessing that it is displayed in basically the same way. If that is the case then the issue is exactly what I thought it was: you are explicitly creating an instance and displaying it and then trying to affect it using the default instance. That means that you have two form objects: one displayed to the user and one you're making changes to. I suggest that you follow the Blog link in my signature and check out my post on default instances. Once you understand the difference, you need to decide whether you want to use default instances or not and stick to that decision.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: How do I alter a label on one form from another form?

    There would be no real reason it wouldnt work, so long as that form was actually on display. if it hasnt been shown yet then it wont update.

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