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 :cool:
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 ?
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
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 ?
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
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).
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.
Re: How do I alter a label on one form from another form?
Quote:
Originally Posted by
jmcilhinney
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.
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?
Re: How do I alter a label on one form from another form?
So:
Quote:
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.
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
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.
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.