how to change the property of opening form after I get the handle of this form using findwindow API?

txu
txu used Ask the Experts™
on
Hi,

I need to change the property of an opening form (just change one command button from disable to enable) in the background thread. In the background thread, I search if this form is opened, if it is then change the command button1 in this form to enable otherwise do nothing (pretty simple).

the part of code I use in my background thread to do this is
   
Dim myform As New Form2
Dim myhwnd As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName _
 As String, ByVal lpWindowsName As String) As Long

myhwnd = FindWindow(vbNullString, "Form2")

if myhwnd <> 0 Then
       myform.hwnd = myhwnd
       myform.Command1 = True
End If

in above code, I was thinking to assign myhwnd to a new form2 then to hadle the openning form2... But I failed in the line "myform.hwnd = myhwnd" which give me compile error: Function or interface marked as restricted, or the function use an Automation type not supported in Visual Basic" .

I am kind of stuck here. Need your guys hand! Any hint about how to handle the property of a openning form after using findwindow api to get the handle is highly appreciated. Any recommendation to a good web site which give me knowledge in this area is also highly appreciated!

Many thanks in advance!

Tom
       

 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Development Manager
Commented:
Hi,
You will not be able to assign the form object using the hWnd property as you are trying to do.
Why not simply enumerate the Forms collection and check to see if the form.name is "Form2" :
Dim frm As Form
For Each frm in Forms
   If frm.Name = "Form2" Then
      frm.Command1.Enabled = True
   End If
Next

HTH,
Alon

Commented:
hi,

Actually this is an wrong API declaration

Findwindow finds for the opened forms in case if the form is not opened or made invisible or not loaded at all VB will say you the above error message

for this simple problem you don't have to go for API
try this code

assume that your form has Form2 as caption then

dim frm as form
for each frm in Forms
if frm is nothing then
  load frm    
end if
if frm.caption = "Form2" then
  <enable the controlas>
  frm.show
end if
next





txu

Author

Commented:
Alon and Javam,

Thanks for your answer which definitely help me out from the problem. By the way, do you guys have any good web site about the visual basic tutorial (mid-advance) level to recommend? I am pretty new to this area.

many thanks,

have a good day,

Tom
txu

Author

Commented:
Hi, Alon and Javam:

I have to ask for your help again. The question I asked above is to find the opened form in another process or object and then change the button property in it.

My main GUI has Form1 and Form2 and the main GUI will launch another Active exe using Createobject() in the background. Then in the launched Active exe, I want to find whether the Form2 is opened in the main gui and change its property if it's opened.

by using following code in the Active exe. I can not find out whether form is opened in main GUI. I think probably because the form is in another exe and it's not in the same process as the active exe is.  

Dim frm As Form
For Each frm in Forms
  If frm.Name = "Form2" Then
     frm.Command1.Enabled = True
  End If
Next

DO you guys have other suggestions about how to find out whether a form is opened in another process and change its property correspondingly?

Additional points will be 50 or more if it's necessary.

Thank you for you guys kind help!

Tom

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial