Maverick Posted October 13, 2003 Posted October 13, 2003 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 Quote
*Experts* mutant Posted October 13, 2003 *Experts* Posted October 13, 2003 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 :). Quote
Maverick Posted October 13, 2003 Author Posted October 13, 2003 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 Quote
*Experts* Volte Posted October 13, 2003 *Experts* Posted October 13, 2003 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. Quote
Maverick Posted October 13, 2003 Author Posted October 13, 2003 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? Quote
Maverick Posted October 13, 2003 Author Posted October 13, 2003 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 ?? Quote
Mohsen Posted October 14, 2003 Posted October 14, 2003 Why don't you delcare client and connect it inside the loading of form2? Mohsen Quote
*Experts* mutant Posted October 14, 2003 *Experts* Posted October 14, 2003 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. Quote
Maverick Posted October 14, 2003 Author Posted October 14, 2003 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 : ?? Quote
Administrators PlausiblyDamp Posted October 14, 2003 Administrators Posted October 14, 2003 What is the revised code you are using? I can't see anything from mutant's post that would have caused a new instance of form1 to be created. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Maverick Posted October 14, 2003 Author Posted October 14, 2003 !! 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...." Quote
Administrators PlausiblyDamp Posted October 14, 2003 Administrators Posted October 14, 2003 '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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.