Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a single form program and I would like to control text boxes on the form via a class. How can I create a proper reference to the form from the class?

 

Thanks!

 

-Falcon

  • Leaders
Posted

Pass the form reference to the class's constructor.

If the class is coming first, then you can create the form from the class. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Administrators
Posted

Easiest way is give the class a Sub New that accepts the form as a parameter.

 

public class Test

private frm as Form1

sub new (f as Form1)
   frm = f
end sub

public sub Something ()
   'You could now refer to the form via the variable frm
    frm.Text = "Hello"
end sub

 

from within form1 you could then do

dim c as new class1(me)

c.something()

 

Is there any reason you need to control the textboxes from a class? Could the code not be part of the form? Could the class not simply return data from it's functions that methods in the form use to update the textboxes?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

That worked great!

 

Thanks guys - that did the trick. I appreciate the code example too! Now I understand how this works.

 

PlausiblyDamp - in response to your questions... my main form class has become massive with about 60+ subs/functions - and I'm not even close to finishing. I have already grouped most of the code into regions so I can close them off - but I wanted to use classes for the sake of breaking out my code more.

 

Will I suffer any type of performance loss by doing this? I guess my background in C++ is making me feel like I need to write classes for everthing... haha.

 

-Falcon

  • Moderators
Posted

Instead of simply Encapsulating your code, why not divide your project into let's say Data Layer, Business Logic and UI.

 

The data layer can handle all of your data access without any knowledge of the other classes, the business logic can inherit or instantiate from the data layer and so on.

Visit...Bassic Software
Posted

Not sure...

 

I'm not sure I understand how you would implement the data layer, etc that you recommended. Can you elaborate on what you use to create these... e.g. forms, classes, modules, etc?

 

-Falcon

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