Reaching a listbox from a module

  • Thread starter Thread starter Murat Kelleci
  • Start date Start date
M

Murat Kelleci

How do i reach a listbox in form1 from a module?

i thinks it is not so easy as it seems,

i tried such like :
dim myform as new form1
form1.listbox1.

and
dim myform as form1
form1.listbox1

both didn't work
 
I haven't verified this, but do you need to do

Public myform as Form1?

Maybe it's not visible to the module because it's private by default.. Not
sure.

Andy.
 
Hello Muran Kelleci,

Take this as a example

Create a form in VB.NET solution and name it as Form1, put a ListBox on form
and name it ListBox1 and put one button on form and name it Button1

now, add one module in VB.NET solution and name it Module1

in that module create a new public function called AddList and pass
parameters for form, and then under that function declarea new List box as a
myList and assign it with the myForm's control and then use myList listbox
as you use the normal list box.

Here the example:
Public Function AddList(ByVal myForm As Form)
Dim myList As New ListBox
myList = myForm.Controls.Item(1)
myList.Items.Add("Test")
End Function

Now, in Form1, double click on the Button which you created and write the
following code in Button1_Click event which will call the AddList function
and will pass the current form as a parameter.

example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
AddList(Me)
End Sub

and then run your solution by pressing F5 Key, and then click on Button1
which you placed on the Form1 and check it, it will work.

Hope this helps, feel free to ask more.

Dhaval Faria.
Microsoft India Community Star
Founder, Programmer.
Hirdhav (http://www.hirdhav.com)
 
Dhaval,

I do know that method works because I am using it in my app all of the
time. However, I was wondering how resource intensive that is because
you are passing an entire copy of the form. If the form has a lot of
controls on it can't this require a lot of overhead? Is there a way to
simply make forms shared so modules can access them?

-Ivan
 
Ivan Weiss,

Don't think so there is any other way, I tried almost all possiblities,
which I can, but couldnt get to it... and ya you are right about the
overhead thing, it will eat lots of resourses.. I will find a way and will
le tyou know.

Dhaval Faria
Microsoft India Community Star
Founder, Programmer.
Hirdhav (http://www.hirdhav.com)
 
Doesn't it just pass a reference to the form rather than a copy of it? In
which case it shouldn't matter how complex the form is.

Am I wrong in thinking this?

Martin.
 
I am not sure if you are right or not. I know that by default it passes
the object value which based on my understanding means it is passing a
copy of it. If I changed that to ByRef than it would be passing a
pointer to the actual object however that is not preferred unless you
are purposefully meaning to change the passed object/variable for safety
reasons.

-Ivan
 
Dhaval Faria said:
Here the example:
Public Function AddList(ByVal myForm As Form)
Dim myList As New ListBox

It should not be decalred as *New* listbox because it will be overwritten
anyway.
myList = myForm.Controls.Item(1)

This does not work if Option Strict is On (and it should always be on). Why
not declare the Argument as Form1 instead of Form?
 
Ivan Weiss said:
I am not sure if you are right or not. I know that by default it
passes the object value which based on my understanding means it is
passing a copy of it. If I changed that to ByRef than it would be
passing a pointer to the actual object however that is not preferred
unless you are purposefully meaning to change the passed
object/variable for safety reasons.



A Form is a reference type. Passing something "ByVal" means passing a copy.
As the passed variable contains only the reference, only a copy of the
reference is passed to the sub. There is no overhead.

Additionally:
- If other controls on the form are not accessed, I recommend passing the
listbox only.
- I recommend even more to getting rid of the module. A class inherited from
Listbox containing the additional code encapsulates the listbox much better.
You don't have to pass *anything* around because the code will be part of
the Listbox, and that's where it belongs to.
 
Murat Kelleci said:
Thanks alot it worked for me but I have to admit that i expected a
simpler solution for this problem in VB.NET

I don't see a problem. If you want to access an object, the object must be
accessible. That has also been the case in previous VB versions. For
example, you had to declare them globally or pass them as arguments. This
was the case for about 99.9% of all objects. Forms have been an exception.
This exception is (luckily) not made anymore because a Form is a class like
all the other 1001 (thousandandone, not 9 *g*) classes in the Framework and
in your application. Though, you can still declare a variable containing a
reference to the Form at a place where it can be accessed in the whole
project, e.g. as a public variable in a module - but I do *not* recommend
this (see suggestion below).
Anyway how do i get the number that i have to enter in
controls.item(#) because for my project i had to use 0 and i found it
by trials.

Declare the argument as Form1, not as Form and you are able to access the
Listbox directly. Even better is to derive your own listbox from the
standard listbox (see also my other reply).
 
Back
Top