Help with vb.net forms and modules

devilmaster1

Member
Joined
Jul 29, 2007
Messages
16
Programming Experience
5-10
How do you access a forms objects in a module like a textbox object.

see i have a module that does not get called in the form codes it deals with .net.sockets.
So there for the recievedata function only needs to be placed in the module where the dll is imported at.

This makes a bit harder cause i can not access textbox2 from the module so when the data is recieved i can not have it displayed in textbox2 on the form.

is there any given way that you can access a forms textbox from a module where the recieve data will go into textbox2 when data is recieved ?
 
in visual basic 6.0 form1.textbox2.text would have worked yes but in vb.net in a module you can not access form1 because form1 is not in there i have tryed that when i first started vb.net about last year never been able to access text boxes or other objects on the form with inside the module thats why i asked if it was that simple i wouldnt have asked.
but thanks for replying.
 
well yes i am using the correct form name and i use vb.net 2003 i have noticed how ever that when i use the dim frm as new form1 and then pass the text to the text box on the form it makes a new form regardless it does not set the text on the active current form.
I dont need a new form i just need the form1s text box to have text lol.
i did this function in a module

VB.NET:
Public Function CheckText()
Dim Frm As New Form1
With Frm
.Textbox1.Text = "D"
MsgBox(.Textbox1.Text)
End With
End Function

now what happened was i loaded it up clicked a button and i did get a message box that said D the text was changed but not on the main form so i changed that around and allowed it to display a new form and i fount out that using the new keyword makes it make a new form1 so the text box was not changed on the main form it self it was only changed on the new form that was created.
I need away to set the text on the main form i dont need to create a clone of the main form.
I have searched google alltheweb and many other forums and i have fount that every one is having trouble with this.
as for the form1.textbox1.text = code i have tryed that Form1 is the main of the form when i try to do Form1.TextBox1.Text = "D"
I get back a error saying that it was not set like was not declared unless the form has diff names.
I my self created a new project and didnt even touch the forms name application name or the module name so basically i have a project named.

WindowsApplication30

a form named

Form1

and a module named

Module1

all default names and still get the same error messages.
Any more ideas i should try ?
 
Last edited by a moderator:
Ok this is the code in my module really simple function

VB.NET:
Module Module1
    Public Function ChangeTextBoxText()
        Dim frm As Form1
        frm.TextBox1.Text = ("")
    End Function
End Module

now this is the code in my button

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call ChangeTextBoxText()
    End Sub

now what happens when i click the button i get this error

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication23.exe

Additional information: Object reference not set to an instance of an object.
and i always end up getting it on this code

VB.NET:
        frm.TextBox1.Text = ("")

now like i said if i change the dim frm to dim frm as new form1 it makes a new form.
if i try to do Form1.TextBox1.Text i get a blue line under the Form1.TextBox1 as if the textbox1 does not exist on the form.

now i have tryed to do this many of diff ways but still end up with the same errors so any ideas ?
 
Last edited by a moderator:
i tryed to import the form that way that still didnt work man.
in fact even if i imported it said the same thing gave the same error.
any more suggestions.
I did install sp1 for vb.net 2003 havent tryed to see if that made any diff on this or not but if you have any more ideas plaese tell me and i try it.
 
I figured out how to access the text box though a moule and decided to help you out and others that may have the same problem i post how its done.

First you make a new project add a module to it on the form add a text box and 1 button.

In the module you do this

VB.NET:
Public TheForm As Form1

So the module is like this.

VB.NET:
Module Module1
Public TheForm As Form1
End Module

Then you go into your form codes and add a sub like this.

VB.NET:
Public Sub ChangeTextBoxText(ByVal Text As String)
TextBox1.Text = Text
End Sub

Then you add this to the form_load method

VB.NET:
TheForm = Me

Go back into the module add make a function that uses the sub like this

VB.NET:
Public Function DoBoxChange(ByVal TheText As String)
TheForm.ChangeTextBoxText(TheText)
End Function

Now in your button code you simply call to the dobox function

VB.NET:
DoBoxChange("Worked")

If all this is done correctly the textboxes text gets changed and this same method works for any object on the form.
But thanks for the help.
 
Last edited by a moderator:
I have also figured out that you dont need no public sub with in the form.
Make a new project add a module 1 button and 1 textbox.

In the module do this

VB.NET:
Module Module1

Public TheForm As Form1

Public Function ChangeTextBoxText(ByVal Text As String)
TheForm.TextBox1.Text = Text

End Module
In the form_load code put this.

VB.NET:
TheForm = Me

In the button code call it like this.

VB.NET:
Call ChangeTextBoxText("Worked")

and that works just the same any ways figured i post that in case someone else has a problem with this like i did.
 
Last edited by a moderator:
sorry about that johnh didnt mean to not add them in code blocks like that i remember next time that any codes i do post to add them like that.
 
sorry about that johnh didnt mean to not add them in code blocks like that i remember next time that any codes i do post to add them like that.
no prob... just makes it so much easier to read (if the format isn't playing us a trick) especially when there is some text and some code. It also so easy to apply formatting to posts, the advanced posting editor have buttons you can just select text and click one of the buttons to format and put codes and quotes in appropriate boxes. Try it out, use the Preview feature to see that the post appear fine before submitting.
 
Back
Top