Solved

How to change label text from a calling function

Posted on 2013-06-10
3
317 Views
Last Modified: 2013-06-11
I have a form that calls a routine (located in the general module.vb) and this routine suppose to change the label text that reside on the form.  I'm passing as one of the arguments of the routine, the calling form .  I don't know why, but an error is generated.  When I specify the passing argument to be of type form, the error generated is "Label1 is not a memeber of System.Windows.Forms.Form".  When I then change the argument type to be of type object, nothing is returned.

Can someone please tell me what I am doing wrong?  Thank you.

[within the calling FORM]

Call qtrByMth(frmProvider.cbxPeriod.Text.Substring(0, 2), Me)



[the function]

Public Sub qtrByMth(ByVal sQtr As String, ByVal objForm As Object)

Select Case sQtr

Case "Q3"
                objForm.Label1.Text = "July"
                objForm.Label4.Text = "July"
                CType(objForm.Controls.Find("Label1", True)(0), Label).Text = "January"

                objForm.Label2.Text = "August"
                objForm.Label5.Text = "August"

                objForm.Label3.Text = "September"
                objForm.Label6.Text = "September"

end select
0
Comment
Question by:CarmenMTorres
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39235763
A Form is just a blank Form...nothing more.  Form1, or Form2, though is a specific type of Form that will have anything that you added to it.

Either change Form to the name of your desired Form (Form1, Form2, etc...), or actually search the generic form instance for the control  with a matching name that you are looking for.  You could also Cast the generic Form instance to a specific type and access the controls that way.

It's not clear to me which approach is best for your situation.

If you only need this function to work with ONE specific type of Form then simply change Form to the specific Form name.
0
 

Author Comment

by:CarmenMTorres
ID: 39237629
The routine will be used by more than one Form.  Therefore, I need to be able to pass the form to the routine so that I can update several of the labels.  I tried passing the form's name as one of the parameters to the routine, but I still was unable to change the labels' text.

Still not clear at what I am doing wrong.  Any help is greatly appreciated.

(Note:  I'm using Framework NET version 1.1)
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39237721
There is another option.  If you need to update the same things on each form then create an Interface that all the forms Implement.  Then you pass a generic Form reference but Cast to the Interface Type to make each Form update the correct elements.

For example, here's a partial Interface:
*I assume the complete one would have 12 Labels?
Public Interface Labels
    ReadOnly Property Lbl1 As Label
    ReadOnly Property Lbl2 As Label
    ReadOnly Property Lbl3 As Label
    ReadOnly Property Lbl4 As Label
    ReadOnly Property Lbl5 As Label
    ReadOnly Property Lbl6 As Label
End Interface

Open in new window


Then, here's Form1 implementing the Interface.  It simply returns the appropriate Label that corresponds to each Label Property:
Public Class Form1
    Implements Labels

    Public ReadOnly Property Lbl11 As System.Windows.Forms.Label Implements Labels.Lbl1
        Get
            Return Me.Label1
        End Get
    End Property

    Public ReadOnly Property Lbl2 As System.Windows.Forms.Label Implements Labels.Lbl2
        Get
            Return Me.Label2
        End Get
    End Property

    Public ReadOnly Property Lbl3 As System.Windows.Forms.Label Implements Labels.Lbl3
        Get
            Return Me.Label3
        End Get
    End Property

    Public ReadOnly Property Lbl4 As System.Windows.Forms.Label Implements Labels.Lbl4
        Get
            Return Me.Label4
        End Get
    End Property

    Public ReadOnly Property Lbl5 As System.Windows.Forms.Label Implements Labels.Lbl5
        Get
            Return Me.Label5
        End Get
    End Property

    Public ReadOnly Property Lbl6 As System.Windows.Forms.Label Implements Labels.Lbl6
        Get
            Return Me.Label6
        End Get
    End Property

End Class

Open in new window

So each different type of Form would need to do the above; Implement the Interface to return the appropriate Labels.

Finally, over in the Module, we change the type of the parameter passed to the Interface type:
Module Module1

    Public Sub qtrByMth(ByVal sQtr As String, ByVal lblForm As Labels)
        Select Case sQtr

            Case "Q3"
                lblForm.Lbl1.Text = "July"
                lblForm.Lbl4.Text = "July"

                lblForm.Lbl2.Text = "August"
                lblForm.Lbl5.Text = "August"

                lblForm.Lbl3.Text = "September"
                lblForm.Lbl6.Text = "September"

        End Select
    End Sub

End Module

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.net Filesystem watcher not working 5 57
Format Transaction Number 19 50
MessageBox Appear behind a form in Runtime 1 32
Background worker passing messagebox yes no 13 26
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question