Calling public subs

S

ST

From Form1 I call a public sub in a module. The sub writes
on Form1.

Problem: the call creates a new Form1 and writes on it,
instead of writing on the one already shown Form1. I have
tried to avoid this by entering the following code at the
beginning of the sub:

Module Module1

Public MyForm As Form1
Public Sub Draw()
If MyForm Is Nothing Then
MyForm = New Form1
End If
MyForm.Show()

Apparently Form1 'is nothing' so a new one is created,,,by
why??
 
S

Sakharam Phapale

How did you created the form1 from where you are calling "Draw" procedure?
myForm variable should set to the form1 before calling the "Draw" method.

Hope it helps.

Regards,
Sakharam Phapale
 
G

Guest

I am not sure that I understand your help.
How did you created the form1 from where you are
calling "Draw" procedure?

Form1 is the start up form. The Draw procedure is called
from Form1 by a click event.
myForm variable should set to the form1 before calling
the "Draw" method.

By the statement 'Puclic MyForm as Form1' above the
procedure 'Draw' I set the MyForm variable to
Form1,,,,don't I ???
 
S

Sakharam Phapale

Dim MyForm as Form1

Above statement only declares the MyForm variable as type Form1.
To set variable (object creation) you have to use following statement
myForm = New Form1
or you can set variable to already created object not only declared one.

Since Form1 is the startup form .NET cretates the instance internally.
To set this instance to myForm variable do as follow in click event from
where you are calling Draw method in Form1.

Public Class Form1

Private sub btnDraw_Click(.......)
myForm = Me -------------- here Me is nothing but
Draw()
End Sub

End Class

Sakharam Phapale
 
C

Chris Dunaway

From Form1 I call a public sub in a module. The sub writes
on Form1.

Problem: the call creates a new Form1 and writes on it,
instead of writing on the one already shown Form1. I have
tried to avoid this by entering the following code at the
beginning of the sub:

Module Module1

Public MyForm As Form1
Public Sub Draw()
If MyForm Is Nothing Then
MyForm = New Form1
End If
MyForm.Show()

Apparently Form1 'is nothing' so a new one is created,,,by
why??

If you need to draw on the form, it is best to do any drawing in the form's
OnPaint method. Then your graphics will be drawn every time the form needs
painting.

In order to access the from from your global sub, however, you must have a
reference to the form. Either change yor sub so that the form is passed
in:

Public Sub Draw(frm As Form)
'Use the frm variable to draw on the form
End Sub

Or Declare your form variable in a global place such as in a Module. The
set your project to start with sub Main:

Module ModMain

Public MyForm As Form1

Public Sub Main()
MyForm = New Form1
Application.Run(MyForm)
End Sub
End Module

Now MyForm is a public variable which is available everywhere in the app.

Hope this helps

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top