Switching from VB6 to VB.NET

Maverick

Newcomer
Joined
Oct 13, 2003
Messages
13
Location
London
Hello guys .. iam new to this forum and i have been coding for VB 6 for along time.. when i got my VS.NET i read a book about VB.NEt but didnt mention anything about my problem... i fully understand what is meant by Objects and members or object since i have been using these in my programs :D...
anywayz i am having a problem accessing the objects of a form from another..

in VB 6 u would do
Form1.Text1.text = "hi"

but when i do this in VB.NET it dosnt work...
so i searched for somethings
and i knew i have to declare a variable as form1 and set it to use it from form2...
i dont know whats the point of this
can somebody explain as much as possible about accessing objects with some code snipest so i can understand

my code is

Code:
Dim Frm as new Form1 
Frm.client.connect(host, port) ' client is a socket class
it says here after i run that i my client objects is nothing
although i declare it like this
Code:
Public withevents Client as Xsock ' this is the original class name

thnx for help in advance
 
The point of that is that it is a big part of Object Orientation. Everything needs to have an instance, you simply cannot use the class name itself to reference the object, but its instance. This gives a huge advantages. You can create multiple instances of each class, which beats procedure oriented programming in every aspect.
If you want to know more (Im sure you do :)) then just search www.google.com for "OOP".
In your example code you create a new instance of Form1 object. I assume thats not what you want. If not say and I will provide you with an example to use the instance of Form1 you already have in your Form2, assuming you show Form1 first :).
 
thnx

yeah.. Form1 is the start up form and i want to load form2 when i press a button lets say...
do i do this?

Code:
Dim Frm as Form2

sub command1_click()
Frm = new form2 
frm.show

do i do this?

and i have form2
i have client socket class declared in form1

do i have to declare and set the client and form1 objects from form 2 to actually be able to control them?????

ps: i understand OOP too :) i have read about it
 
As long as the thing on the forum is public (I think controls are private by default; set it from the Property Grid) you should be able to access it, provided you've stored the instance variable of the form you wish to use.
 
wops

WOps seems i added a new thread instead of replying the thread
this thread belongs to Switching from VB6 to VB.NET

alright now ... so i need to add declare the client calss inside the form2
and form1 variable too
what i dont want is that the form2 will create a new winsock class rather than using the one existing in the form1

what code would i use to do that?
 
i use this
Code:
Dim Frm as Frmmain

sub command1_click()
Frm = new frmmain
frm.client.connect()

but client is nothing when i run the program although i do set it in frmmain
do i have to set it in form2 ??
 
If you want to use objects from your instance of Form1 on your Form2 you need get that instance first. You can do it using constructors or shared variables. If you want to do it using construcotrs then:
In Form2 create a new variable of the Form1 type:
Visual Basic:
DIm f1 As Form1
Then find the constructor for Form2 (Public Sub New) and edit it to look like this:
Visual Basic:
Public Sub New(ByVal formone As Form1) 'get the instance
MyBase.New()
InitializeComponent()
f1 = formone 'set the passed in instance to the local variable
End Sub
Now, when declaring Form declare it like this:
Visual Basic:
Dim f2 As New Form2(Me) 'pass in the instance to the constructor
Now you will be able to access the instance you passed using the f1 variable in your Form2 class.
 
ok

alright i have tried that... but it opens a new form1 instead of using the existing one...
and it also when i hover the mouse over thw word client in
f1.client.connect
it says "Nothing" and that means its not set although i have set it in form1 : ??
 
!!

iam just trying get help here knowing that iam not a n00b with a copy paste abilities alright!

Code:
 Dim f1 As FrmMain
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        f1 = FrmMain
    End Sub
'this is part of the code

'this is put after a clickevent of a button
f1.History.Text = "Connecting...."
 
Visual Basic:
'Nicked from mutant a few posts up
Public Sub New(ByVal formone As Form1) 'get the instance
MyBase.New()
InitializeComponent()
f1 = formone 'set the passed in instance to the local variable
End Sub

You seem to have modified the standard Sub New but without adding the parameter as suggested by mutant a few posts back
 
Back
Top