how do i activate/set focus to loaded form?

couch612

Newcomer
Joined
Apr 28, 2003
Messages
9
Location
Brisbane, Australia
I have these declarations in my basMain code module (fMain and fChild are a Windows Forms object)

Public fMain as new fMain()
Public fChild as new fChild()

sub Main

fMain.Show()

End sub


This shows the Main form OK. I have another form that is displayed within the Main form when a menu is clicked. When the ok button is clicked on this new form, it performs the following code:

dim fDialog as new fDialog()

fDialog.Show()


Now within the fDialog module, the OK button event is as follows:

fChild.Show()


The call to fChild.Show seems to be out of scope. Why can't the code in the fDialog module see the fChild object I have declared as Public in the basMain module?

I will also want to make subsequent calls to fChild.Activate() to set the focus to the form. Am I going about this the wrong way?

Thanks in advance and in the meantime I will look up MSDN.
 
Guys I believe this will answer my question. Am I right?....



Public Class myForms
Private Shared m_CustomerForm As CustomerForm
Public Shared Property CustomerForm() As CustomerForm
Get
Return m_CustomerForm
End Get
Set(ByVal Value As CustomerForm)
m_CustomerForm = Value
End Set
End Property
End Class

When you first create an instance of your form you could store it into this class:

Dim myNewCust As New CustomerForm()
myNewCust.Show()
myForms.CustomerForm = myNewCust


After the CustomerForm property has been populated with an instance of your form, you could then use this class to access that same instance from anywhere in your code:


Module DoingStuffWithForms
Sub DoExcitingThings()
myForms.CustomerForm.Activate()
End Sub
End Module
 
That's one way to manage this.
But it is not really necessary to define a class, I would just put a global variable into a Module. Anyway I think the class is more OOP, so at last it's a matter of programming style and taste.
If there is more than one form to interact with, I like to use a collection like SortedList e.g. to hold my forms, and not a single variable for each form. This gives you the opportunity even to open more than one instance of a form and you don't have to know how many times you will open it at design-time.
 
Thanks for replying. But I tried using a Public variable in a code module like:

Public fChild as new fChild()

and when i tried to use fChild.Show() in the code of another form's OK button event it gave the shared reference error not available (or some message like that) at compile time. how do you declare a form as a global without using the class? I think I will just use the class if I can't figure it out.
 
Assuming the form's class name is frmMyForm, you first declare the variable that will hold your form in the module with

Visual Basic:
Public myF as frmMyForm

Then when you want to instanciate it (e.g. in a Button.Click event)

Visual Basic:
myF = New frmMyForm()
myF.Show()

Be sure you set myF to nothing when you close the form.
 
Back
Top