Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 

 
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

 
Public withevents Client as Xsock ' this is the original class name 

 

thnx for help in advance

  • *Experts*
Posted

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 http://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 :).

Posted

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?

 

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

  • *Experts*
Posted
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.
Posted

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?

Posted

i use this

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 ??

  • *Experts*
Posted

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:

DIm f1 As Form1

Then find the constructor for Form2 (Public Sub New) and edit it to look like this:

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:

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.

Posted

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 : ??

Posted

!!

 

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

 

 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...."

  • Administrators
Posted

'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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...