PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2012 [RESOLVED] VB.NET 2012 Proper Way to Reference Controls on StartUp Form-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] VB.NET 2012 Proper Way to Reference Controls on StartUp Form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    21

    Resolved [RESOLVED] VB.NET 2012 Proper Way to Reference Controls on StartUp Form

    I know that generally to access a control on a form, say a label named lblSentence on a form named frmBackground, from another form, you would do the following:



    Code in frmJ (second form):

    Code:
    Dim frmNewBackground As New frmBackground
    
    Messagebox.Show ("Contents = " & frmNewBackground.lblSentence.Text)

    But what happens when frmBackground is your StartUp form? Is it proper to code:

    Code:
    Messagebox.Show ("Contents = " & frmBackground.lblSentence.Text)?

    The reason I ask is because I am using the second example, but I am converting the project to C# and C# doesn't like the frmBackground.lblSentence.Text.

    Thanks

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: VB.NET 2012 Proper Way to Reference Controls on StartUp Form

    If you use the type name to refer to a form instance, as you are in that second code snippet, then you are referring to the default instance. The default instance is an instance that the system manages for you so that it is accessible anywhere in your project. In VB projects, the startup form is the default instance of its type, so you can access the startup form using that default instance anywhere in your project. That makes it easy for beginners to communicate between forms, because they don;t have to care about the principles of object-oriented programming and maintain a reference to an object that they need to access elsewhere.

    The thing is, most experienced VB developers never use default instances anyway. There's no real need to if you understand how objects work. There's a little extra work involved in maintaining references between forms but it's usually very little and it's also second nature if, again, you understand how objects work.

    There are two significant differences between VB and C# at play here. Firstly and most importantly, C# doesn't support default instances. Secondly, when you add a control to a form in C#, the corresponding field is declared private, where they are Friend in VB. That means that, even if you have a reference to the form in C#, you can't access a control on that form directly by default.

    These differences examples of why VB is easier for beginners but those who want to be good developers should do things the right way regardless of language. The right way to do what you want to do depends on exactly what you want to do and what the relationship between the forms is. I'm going to take things at face value and assume that you want to display the contents of a Label from the startup form in another form and assume that this other form was created by the startup form. In that case, the most proper way would:

    1. The second form raises an event.
    2. The first form handles that event.
    3. The first form sets a property of the event args from the Text of the Label or, preferably, some better source.
    4. The second form gets that property value and displays it.


    There are other ways but this is the "best" way. I suggest that you follow the Blog link in my signature below and check out my posts on Data Among Multiple Forms. Be sure to read all three parts. The first part covers default instances in VB and the other two cover options available in both VB and C#, with part 2 covering the slightly dodgy way to do things and part 3 covering the proper way.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    21

    Re: VB.NET 2012 Proper Way to Reference Controls on StartUp Form

    Thank you jmcilhinney for the very detailed response. I have definitely found out the hard way that VB is more forgiving. I've been making many changes to follow the better coding techniques. Originally, I had a start up procedure. The application is designed for a touch screen tablet. My fat fingers, hitting the icon twice, like you would on a desktop, would launch two instances of the application, and they both weren't working properly because they both were accessing the same My.Settings. My research required me to use a form at Start Up and Enable Application Framework and move my Start Up code to Application.Events.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    21

    Re: VB.NET 2012 Proper Way to Reference Controls on StartUp Form

    Update. Thanks to John's response, I have followed his tutorial and used option 3.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: VB.NET 2012 Proper Way to Reference Controls on StartUp Form

    Glad you got it working. Would you mind posting your solution, so that we can check it to see whether it could be improved? Here's an example of what I would have done:
    vb.net Code:
    1. Public Class TextRequiredEventArgs
    2.     Inherits EventArgs
    3.  
    4.     Public Property Text As String
    5.  
    6. End Class
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         Dim f2 As New Form2
    5.  
    6.         'Register event handlers.
    7.         AddHandler f2.TextRequired, AddressOf f2_TextRequired
    8.         AddHandler f2.FormClosed, AddressOf f2_FormClosed
    9.  
    10.         f2.Show()
    11.     End Sub
    12.  
    13.     Private Sub f2_TextRequired(sender As Object, e As TextRequiredEventArgs)
    14.         'Make data available through event args to the object that raised the event.
    15.         e.Text = TextBox1.Text
    16.     End Sub
    17.  
    18.     Private Sub f2_FormClosed(sender As Object, e As FormClosedEventArgs)
    19.         Dim f2 = DirectCast(sender, Form2)
    20.  
    21.         'Unregister event handlers when form is disposed.
    22.         RemoveHandler f2.TextRequired, AddressOf f2_TextRequired
    23.         RemoveHandler f2.FormClosed, AddressOf f2_FormClosed
    24.     End Sub
    25.  
    26. End Class
    vb.net Code:
    1. Public Class Form2
    2.  
    3.     Public Event TextRequired As EventHandler(Of TextRequiredEventArgs)
    4.  
    5.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    6.         Dim tre As New TextRequiredEventArgs
    7.  
    8.         OnTextRequired(tre)
    9.  
    10.         MessageBox.Show(tre.Text)
    11.     End Sub
    12.  
    13.     Protected Sub OnTextRequired(e As TextRequiredEventArgs)
    14.         RaiseEvent TextRequired(Me, e)
    15.     End Sub
    16.  
    17. End Class

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    21

    Re: VB.NET 2012 Proper Way to Reference Controls on StartUp Form

    John, I used the first example in part 3.

    In my second form (frmAdjectives), I added:

    Code:
        'This code passed lblNewWord back to frmBackground
        Public ReadOnly Property NewAdjective As String
            Get
                Return lblNewWord.Text
            End Get
        End Property
    In my btnDone of the same form:

    Code:
      Private Sub btnDone_Click(sender As Object, e As EventArgs) Handles btnDone.Click
            'Set dialog result to yes so lblSentence strip is updated
            If lblNewWord.Text.Length > 0 Then
                gstrLastWord = lblNewWord.Text
            End If
            Me.Close()
    
        End Sub
    I had used the Dialog result example that you showed, but I had several forms that I was doing this with, some worked, some didn't, so I decided to check to see if the form was return anything.

    My main form, frmBackground had this:

    Code:
    Select Case gstrAlphaID
                    Case "A"
                        Using frmNouns As New frmNoun
                            frmNouns.ShowDialog(Me)
                            tlpNavigator.SendToBack()
                            If frmNouns.NewNoun.Length > 0 Then
                                lblSentence.Text = lblSentence.Text.Substring(0, intFoundPos) & frmNouns.NewNoun & " "
                            End If
                        End Using
                    Case "C"
                        Using frmPronouns As New frmPronoun
                            frmPronouns.ShowDialog(Me)
                            tlpNavigator.SendToBack()
                            If frmPronouns.NewPronoun.Length > 0 Then
                                lblSentence.Text = lblSentence.Text.Substring(0, intFoundPos) & frmPronouns.NewPronoun & " "
                            End If
                        End Using
                    Case "F"
                        Using frmAdjectives As New frmAdjective
                            frmAdjectives.ShowDialog(Me)
                            tlpNavigator.SendToBack()
                            If frmAdjectives.NewAdjective.Length > 0 Then
                                lblSentence.Text = lblSentence.Text.Substring(0, intFoundPos) & frmAdjectives.NewAdjective & " "
                            End If
                        End Using

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width