none
Reference label in another form RRS feed

  • Question

  • Hi,

    I'm trying to reference a label that's in a different form from the one that I'm trying to use. How do I reference that label? It's a user control form into a Windows form so don't know if that matters or not.

    It has been a while since I've used  .NET and first time with user control form. So any help would be great!

    Thanks!


    • Edited by mpwbs92 Friday, March 1, 2019 2:03 PM
    Thursday, February 28, 2019 9:22 PM

Answers

  • I created a new project with two Forms, Form1 and Form2.

    Form1 has a Button and a Label.  The label name is LabelForm1.

    Form2 has a Label and a TextBox.  The label name is LabelForm2.

    Here is the code for Form1

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2(Me) 'create a new instance of Form2 and show it
            f2.ShowDialog()
            f2.Dispose()
        End Sub
    End Class
    

    Note that the default instances of Form1 and Form2 are NOT being used

    Here is the code for Form2.

    Public Class Form2
    
        Public Sub New(Form1Instance As Form1)
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization AFTER the InitializeComponent() call.
            Form1Ref = Form1Instance 'keep the instance of Form1 for later
        End Sub
    
        Private Form1Ref As Form1 '<<<<<<<<<<
    
        Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            TextBox1.Text = Form1Ref.LabelForm1.Text 'the label on Form1
        End Sub
    End Class

    Notice how the constructor is used.  After you understand this you should be able to apply the concepts to your app.


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by mpwbs92 Friday, March 1, 2019 6:59 PM
    Friday, March 1, 2019 6:31 PM

All replies

  • Hi

    Maybe something like this:

    Dim s As String = Form2.Label1.Text


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, February 28, 2019 9:32 PM
    Thursday, February 28, 2019 9:31 PM
  • What have you tried so far?



    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, February 28, 2019 9:31 PM
  • Hello,

    Given you have created the form e.g. childForm As New Form2 (where Form2 is the child form) you can access as follows.

    Calling form (ShowDialog could be instead Show)

    Dim childForm As New Form2
    childForm.Label1.Text = "Karen Payne"
    childForm.ShowDialog()
    
    Dim result As String = childForm.Label1.Text
    MessageBox.Show(result)

    Form 2 code

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label1.Text = "Payne Karen"
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 28, 2019 10:16 PM
    Moderator
  • So, I've tried doing form.label.text, creating a variable for that to go into that label which then I could assign to another label. That doesn't work. It's a user control form so I don't know if that matters or not when trying to reference a label from there or not. Any help at this point would be great! 
    Friday, March 1, 2019 1:58 PM
  • Is the user control and the form in the same solution?

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 1, 2019 2:09 PM
  • Yes, they are...  That's why I'm trying to figure out why it's happening that I can't get it to populate in the form I'm trying to use.
    • Edited by mpwbs92 Friday, March 1, 2019 2:22 PM
    Friday, March 1, 2019 2:21 PM
  • So if you are using a UserControl, see the following code sample I wrote.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, March 1, 2019 2:32 PM
    Moderator
  • You will find that there are a good many ways to do this.

    One way is you can create a class of public objects that reference one or more object from the calling form then pass the class as a parameter to your user control.

    Here is an example of what I mean

    Public Class FormA
    
        Dim FormA_ExampleObj As New Object
        Public Class UserControlArgs
            Public MyLabel As Label = Nothing
            Public FormA_ExampleObj As Object = Nothing
        End Class
    
        Private Sub ButtonShowUserControl_Click(sender As Object, e As EventArgs) Handles ButtonShowUserControl.Click
    
            Dim _UCArgs As New UserControlArgs With {
                .FormA_ExampleObj = FormA_ExampleObj,
                .MyLabel = Label1}
    
            Dim _UserControl1 As New UserControl1(_UCArgs)
            _UserControl1.Dock = DockStyle.Fill
            PanelUserControl1.Controls.Add(_UserControl1)
    
        End Sub
    
    End Class

    The on your user control.vb

    Public Class UserControl1
        Dim _UserControlArgs As FormA.UserControlArgs
        Public Sub New(ByVal _UCArgs As FormA.UserControlArgs)
            InitializeComponent()
            _UserControlArgs = _UCArgs
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Use your args
            MsgBox(_UserControlArgs.MyLabel.Text)
        End Sub
    End Class
    


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    • Edited by Gtripodi Friday, March 1, 2019 2:34 PM
    Friday, March 1, 2019 2:32 PM
  • Okay, that seems to not work... I'll try to insert what I'm trying to do:

    Trying to take this label from this function and insert the label text into this form area. I'll loop this later on the 5 times you see to grab the OK or NG in the label text for each sensor trigger.

    Private Sub SensorResultUpdated(sender As Object, e As ToolResultUpdatedEventArgs)
                labelTotal.Text = If(e.TotalStatusResult, "OK", "NG")

      Public Sub Status()
            lblResult1.Text = "OK"
            lblResult1.Text = "OK"
            lblResult2.Text = "OK"
            lblResult3.Text = "OK"
            lblResult4.Text = "OK"
            lblResult5.Text = "OK"

            If lblResult1.Text = "OK" And lblResult2.Text = "OK" And lblResult3.Text = "OK" And lblResult4.Text = "OK" And lblResult5.Text = "OK" Then
                lblPFResult.Text = "PASS"
                lblPFResult.ForeColor = Color.Green
                lblPFResult.BackColor = Color.AntiqueWhite
            Else
                lblPFResult.Text = "FAIL"
                lblPFResult.ForeColor = Color.Red
                lblPFResult.BackColor = Color.AntiqueWhite
            End If
        End Sub

    Friday, March 1, 2019 3:40 PM
  • Actually it does work, what 'seems' to not work is the way you may have introduced it into your situation. 

    Perhaps you could show me how you have applied it into your Form/UC and we go from there.

    I have not looked at Karen's solution but am quite sure it is more thorough and may be easier to adapt to what you're doing


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 1, 2019 4:59 PM
  • Okay well, all I did was say lblResults1 = ResultsDisplayControl.labelTotal.Text.

    Also, tried dim x as ResultsDisplayControl.labelTotal.Text

    lblResults1.Text = x.

    They all show up red saying its private function and can't do that...



    • Edited by mpwbs92 Friday, March 1, 2019 5:59 PM
    Friday, March 1, 2019 5:30 PM
  • This matters very much: It's a user control form into a Windows form

    1.  That is only possible with a MDI form
    2.  Is the form in the same application otherwise it is not possible in a non hacking way?
    3.  If it is public instanced and just shown somewhere else you can always use it conform the way Leshay shows (the same with MDI). 

    Success
    Cor


    Friday, March 1, 2019 5:35 PM
  • 1. Okay, it is for sure.

    2. Yes, the form is in the same application, even in the same solution.

    3. Well, it's private and even if I tried to make it public and do Leshay's way. It still hits with an error...

    Friday, March 1, 2019 5:39 PM
  • Okay well, all I did was say lblResults1 = ResultsDisplayControl.labelTotal.Text.

    Also, tried dim x as ResultsDisplayControl.labelTotal.Text

    lblResults1.Text = x.

    They all show up red saying its private function and can't do that...

    For some reason I though you may have tried passing these objects to the UC as I have described, I didnt realize you were using leshay's method. 

    Enjoy your evening.


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 1, 2019 5:45 PM
  • Thanks! Nope, the other way from the UC to the Form

    Friday, March 1, 2019 5:46 PM
  • Maybe it is not available anymore. 

    You have to know what MDI Child it is from the parent form. 
    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.mdichildren?view=netframework-4.7.2

    Then you have to know if it is still alive (that goes strange)

    The property which you need for that is not in the documentation for Forms because that does not direct inherit from Controls. 

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.isdisposed?view=netframework-4.7.2

    How you use this depends in this case by the way how you have made your program. But with these handles it should be possible for you to do.


    Success
    Cor

    Friday, March 1, 2019 5:54 PM
  • Thanks! Nope, the other way from the UC to the Form

    If youre trying to change objects on the form from the UC then what I posted works just fine

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 1, 2019 5:55 PM
  • I created a new project with two Forms, Form1 and Form2.

    Form1 has a Button and a Label.  The label name is LabelForm1.

    Form2 has a Label and a TextBox.  The label name is LabelForm2.

    Here is the code for Form1

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2(Me) 'create a new instance of Form2 and show it
            f2.ShowDialog()
            f2.Dispose()
        End Sub
    End Class
    

    Note that the default instances of Form1 and Form2 are NOT being used

    Here is the code for Form2.

    Public Class Form2
    
        Public Sub New(Form1Instance As Form1)
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization AFTER the InitializeComponent() call.
            Form1Ref = Form1Instance 'keep the instance of Form1 for later
        End Sub
    
        Private Form1Ref As Form1 '<<<<<<<<<<
    
        Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            TextBox1.Text = Form1Ref.LabelForm1.Text 'the label on Form1
        End Sub
    End Class

    Notice how the constructor is used.  After you understand this you should be able to apply the concepts to your app.


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by mpwbs92 Friday, March 1, 2019 6:59 PM
    Friday, March 1, 2019 6:31 PM
  • I created a new project with two Forms, Form1 and Form2.

    Form1 has a Button and a Label.  The label name is LabelForm1.

    Form2 has a Label and a TextBox.  The label name is LabelForm2.

    Here is the code for Form1

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2(Me) 'create a new instance of Form2 and show it
            f2.ShowDialog()
            f2.Dispose()
        End Sub
    End Class

    Note that the default instances of Form1 and Form2 are NOT being used

    Here is the code for Form2.

    Public Class Form2
    
        Public Sub New(Form1Instance As Form1)
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization AFTER the InitializeComponent() call.
            Form1Ref = Form1Instance 'keep the instance of Form1 for later
        End Sub
    
        Private Form1Ref As Form1 '<<<<<<<<<<
    
        Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            TextBox1.Text = Form1Ref.LabelForm1.Text 'the label on Form1
        End Sub
    End Class

    Notice how the constructor is used.  After you understand this you should be able to apply the concepts to your app.


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    So when I show how to pass the entire form class I get yelled at for bad programming. Then I show how to create a subclass with only the values/objects you need from the parent class and it is completely overlooked. 

    Not saying your solution is unacceptable, just saying this is a weird forum


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 1, 2019 7:27 PM
  • So when I show how to pass the entire form class I get yelled at for bad programming. Then I show how to create a subclass with only the values/objects you need from the parent class and it is completely overlooked. 

    Not saying your solution is unacceptable, just saying this is a weird forum


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Don't know what you are referring to.  My post answers the original question.

    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Friday, March 1, 2019 7:38 PM
  • I am referring to post# uh...um oh yea ... never mind. 

    Basically among this mess I plopped a solution that passes the desired object reference inside a 'subclass' that can be passed as a parameter, much like what you're showing.

    It's all good.


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 1, 2019 7:58 PM