Startup Form's Name

B

Bernie Hunt

This is probably a silly question, but I've gotten myself confused.

My app has two forms, form1 and form2. form1 is the start up object in the
propers. An event in form1 instantiates form2.

Dim myForm as HardwareStore
myForm = New HardwareStore
myForm.Show()

I understand that my form2 can be referenced by

myForm.blah

What is the form name for form1 so that I can reference it from form2
(myForm)?

form1.blah

Doesn't work.

Sorry for such a basic question, but I'm trying to learn here, hahaha.

Bernie
 
A

AlanT

You really don't have two forms. You have a form class 'HardwareStore'
and an instance of that class 'myForm'.

When setting a startup object you would select HardwareStore and the
runtime will generate an instance of it and display that instance. You
need to explictly create and show the form.

Depending upon where the lines above are placed either they will never
be run or you may have two instances of HardwareStore showing.


There are a few different ways to start a VB.Net application.

For a simple start.
Define the HardwareStore form. Mark it as your startup object. Run the
application and it will show.

For a more complicated start.
Create a module, say HardwareLoader, and put a sub main() in it


Module HardwareLoader

public sub main(args() as string)

Dim mainForm as new HardwareStore
Application.Run(mainForm)

end sub

end module


Set HardwareLoader as the startup object. Run application.


hth,
Alan.
 
G

Guest

You can set a property in Form 2 such as myform1 and set to the startup form
as;

dim myform2 as nrew form2
myform2.myform1 = me
myform2.show or myform2.showdialog
 
B

Bernie Hunt

Following your guidelines, I now have;

Module Main
Public Sub main()
Dim myFormStartup As New StartUp
Application.Run(myFormStartup)
End Sub

Class Startup
Dim myFormHardware As Hardware_Store
Dim myFormHardwareStatus As Boolean

Private Sub StartUp_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myFormHardwareStatus = False
End Sub

Private Sub StartUp_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myFormHardwareStatus = False
myFormHardware = New Hardware_Store
myFormHardware.Show()

End Sub

Public Function getMyFormHardwareStatus() As Boolean
Return myFormHardwareStatus
End Function

Public Sub setMyFormHardwareStatus(ByVal Status As Boolean)
myFormHardwareStatus = Status
End Sub


Class HardwareStore
Private Sub Hardware_Store_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load
' This line is where I'm having the problem
' The error says MyFormStartup is not declared

myFormStartup.setMyFormHardwareStatus(True)

End Sub

The problem is trying to reference a function or sub in the myFormStartup
that I instantiated in the sub Main().

What am I missing to be able to access a function in myFormStartup?

Thanks,
Bernie
 
C

Cor Ligthert [MVP]

Bernie,

It is not impossible. However you would try to avoid it. It can make your
project completely dependend from form2. If you change something in that
than you have forever to think that it can have effects on form1. And with
that ending one of one of the major reasons of OOP. Maintainability.

You should not try to do things in your class Form2, which are done in
Form1. Create than a special class for that, which you can use in both.

Just my thought,

Cor
 
A

AlanT

It looks like there was some misunderstanding on my part.

My interpretation was that you have one form HardwareStore class that
is your application.

from the above code you have 2
StartUp
Hardware_Store


What does startup do?
Do you need it?

Can you just replace Startup in main() with Hardware_Store?


Alan.
 
B

Bernie Hunt

The example I sent is a cut down version to show the problem. The startup
form contains buttons that will launch other programs. HardwareStore is
just one of them.

The issue is my syntax to call a function between the Startup form and
HardwareStore form. I can get the forms instatuated OK, I stuck at the
newbie point of how does one of them call a function from the other.

Sorry for such a silly problem, but I must be missing something basic.

Bernie
 
B

Bernie Hunt

Cor,

I understand and your point makes alot of sense. The basic problem will
still exist in your suggested design.

The problem is I'm having trouble accessing a function in an instatiated
form. Please see the code I posted in my other message. I show the exact
problem there.

If I can get past the amazinly simple syntax problem, then I can worry
about better design, hahahaha.

Bernie
 
C

Claes Bergefall

You need access to the instance of the StartUp form. myFormStartup that you
defined in Sub Main is local to that method and not acessable from any other
place. You HardwareStore class (or is it Hardware_Store?) needs to be
provided with a reference to the StartUp form instance. There are several
ways to do this. One of the easiest ways is to add a parameter to the
constructor of HardwareStore and pass a reference that way

Class HardwareStore
Private myStartUpForm as StartUp
Public Sub New(ByVal StartUpForm As StartUp)
' Save the reference to StartUp for later use
myStartUpForm = StartUpForm
End

Public Function getMyFormHardwareStatus() As Boolean
Return myStartUpForm.myFormHardwareStatus
End Function

Public Sub setMyFormHardwareStatus(ByVal Status As Boolean)
myStartUpForm.myFormHardwareStatus = Status
End Sub
End Class
....
Private Sub StartUp_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myFormHardwareStatus = False
' Pass a reference to this instance of StartUp to the new HardwareStore
instance
myFormHardware = New HardwareStore(Me)
myFormHardware.Show()
End Sub

You need to change myFormHardwareStatus to be Public (or preferable to a
property)

/claes
 
A

AlanT

You will need to include a reference to the startup form in the
HardwareStore et al.

A way is to create a base form from which to derive all you Store forms
and include in that a property parent

e.g.

Public class StoreBase
inherits System.Windows.Forms.Form

....

Public property ParentForm() as Form
Get
return _parentForm
end get
Set (value as Form)
_parentForm = value
end set
end property


private _parentForm as Form

...

end class


In Startup when you launch the store use

Dim hardware as New HardwareStore
hardware.ParentForm = me
hardware.Show()


Hardware (and all the other apps)

public class HardwareStore
inherits StoreBase


' some code that references startup

private Sub DoSomething()

ParentForm.xxx()

end sub

end class



hth,
Alan.
 
C

Cor Ligthert [MVP]

Bernie,

You want to share a function between two forms.
That the function is now on form1 is in my idea from not any importancy.

It can be a good reason to make a shared class as this function has as well
data in it.
If it has not data than it is bettter just to instance it when it is needed.
Therefore we get something as

Class Form1 etc
Sub Whatever
MyHelperClass.myStatus = true
end sub

Class Form2 ect
Sub Whatever
dim status = myHelperClass.MyStatus
end sub

Public Class myHelperClass
public shared MyTogle as boolean
Public Shared property MyStatus() as boolean
Get
return MyTogle
End Get
Set(byval value as boolean)
myTogle = value
End Set
End Class

I hope that this gives an idea

Cor
 

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