String value not showing in a textbox but prints it in msgbox

aliweb

Active member
Joined
Sep 14, 2005
Messages
40
Programming Experience
3-5
Take a look at the attached program. It is really simple just one textbox and one button.
What I am doing is when you click on the button it calls a subroutine which is inside a module. That sub passes a string value "hello" to another sub which is inside the form.
I can print the value using msgbox but it is not showing it in the textbox on that form. What am I doing wrong and what is the solution?
 

Attachments

  • TestProject.zip
    11.1 KB · Views: 25
The problem is in the SendMessage procedure in the Module. The line: "Dim objForm1 As New Form1" creates a new instance of form1. When you call the ReceiveMessage method, the new instance sets it's textbox1.Text property to the passed parameter value then opens a messageBox. The new instance isn't visible because it's Show method hasn't been called.
To fix this, change the SendMessage Procedure to this
VB.NET:
Module Module1

    Friend objForm1 As Form1

    Friend Sub SendMessage(ByVal strSMessage As String)
        'Dim objForm1 As New Form1
        objForm1.ReceiveMessage(strSMessage)
    End Sub

End Module

And in Form1, change the New method to this:
VB.NET:
    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        objForm1 = Me
        'Add any initialization after the InitializeComponent() call
    End Sub

This article gives a good explaination: Multiple Forms in VB.NET. Part 4 - Accessing Controls and Data ...
 
Back
Top