Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General / July 2009



Tip: Looking for answers? Try searching our database.

Is my VB6 app form open?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
William - 03 Jul 2009 03:07 GMT
On a remote pc that is...

I have a VB6 data collection app running on a remote PC, and need to
determine if the main form is open when a client app is opened.

Somewhat like this:
Dim FormVisible as boolean
If form1.visible = true then
FormVisible = true
Else
FormVisible = false
End ifBut not sure how to do remote.
Any Ideas?
Jason Keats - 04 Jul 2009 02:39 GMT
> On a remote pc that is...
>
[quoted text clipped - 9 lines]
> End ifBut not sure how to do remote.
> Any Ideas?

If by "opened" you mean loaded and visible, then something like this may
do the job.

If FormIsLoaded(Form1.Name) And Form1.Visible Then
    'do whatever
End If

Public Function FormIsLoaded(ByVal sFormName As String) As Boolean
    Dim bLoaded As Boolean
    Dim f As Form

    bLoaded = False

    For Each f In Forms
        If f.Name = sFormName Then
            bLoaded = True
            Exit For
        End If
    Next f

    Set f = Nothing

    FormIsLoaded = bLoaded
End Function

I'll leave to you the problem of how your client app is going to talk to
your remote app which will have to run the above code and return the answer.
Kevin Provance - 04 Jul 2009 04:59 GMT
Checking if a form is open by accessing it's Visibiliy properly will load
the form if it is not already loaded, which I don't think is what this guy
wants to do.

The IsFormLoaded code suggested will to the trick.  Here is mine, where the
case of the form does not matter.:

Public Function IsFormLoaded(ByVal sFormName As String) As Boolean

Dim i As Long

For i = 0 To Forms.Count - 1
   If (LCase$(Forms(i).Name) = lcase$(sFormName)) Then
       IsFormLoaded = True
       Exit For
   End If
Next

End Function

Signature

2025
If you do not believe in time travel,
your beliefs are about to be tempered.

http://www.facebook.com/group.php?gid=43606237254

| > On a remote pc that is...
| >
[quoted text clipped - 37 lines]
| I'll leave to you the problem of how your client app is going to talk to
| your remote app which will have to run the above code and return the answer.
Jason Keats - 04 Jul 2009 06:47 GMT
> Checking if a form is open by accessing it's Visibiliy properly will load
> the form if it is not already loaded, which I don't think is what this guy
> wants to do.

That's a good point. That bit was air code (ie, untested).

Maybe it should have been:

If FormIsLoaded(Form1.Name) Then
    If Form1.Visible Then
        'do something
    Else
    Form1.Visible = True
    'do something
    End If
Else
    Form1.Show
    'do something
End If
Kevin Provance - 04 Jul 2009 19:10 GMT
Your code assumes the form is already loaded and you are checking for
visibility.

If your form is not loaded, then If Form1.Visible = False will load it if
it's not already loaded, which makes the check a moot point, it will always
return loaded even if it wasn't.

The code I offered checks the Forms collection (which is a collection of
Forms that have already been loaded) and compares the form you want to check
against that collection, which will tell you if it's loaded or not.

- Kev

Signature

2025
If you do not believe in time travel,
your beliefs are about to be tempered.

http://www.facebook.com/group.php?gid=43606237254

| > Checking if a form is open by accessing it's Visibiliy properly will load
| > the form if it is not already loaded, which I don't think is what this guy
[quoted text clipped - 15 lines]
|     'do something
| End If
Jason Keats - 05 Jul 2009 07:41 GMT
> Your code assumes the form is already loaded and you are checking for
> visibility.
[quoted text clipped - 8 lines]
>
> - Kev

I guess you're saying
    If FormIsLoaded(Form1.Name) Then
should be
    If FormIsLoaded("Form1") Then
for my code to work.

I must admit I normally pass a string to that function.

I forgot that Form1.Name will load the form if it's not already loaded!
Thanks for pointing out my mistake.
Randem - 04 Jul 2009 04:15 GMT
You could do something simple like open a file (exclusive)when you load the
form and close it when you exit the form. Then all the client form needs to
do is to check to see if it can write to the file. If you get an error then
the form is open...

It probaly the simliest method. Of course if your app terminates abruptly
you might leave the file open and will need to forcibly close it on the next
run.

Signature

Randem Systems
Your Installation Specialist
The Top Inno Setup Script Generator
http://www.randem.com/innoscript.html
Disk Read Error Press Ctl+Alt+Del to Restart
http://www.randem.com/discus/messages/9402/9406.html?1236319938

> On a remote pc that is...
>
[quoted text clipped - 9 lines]
> End ifBut not sure how to do remote.
> Any Ideas?
Kevin Provance - 04 Jul 2009 05:01 GMT
| You could do something simple like open a file (exclusive)when you load the
| form and close it when you exit the form. Then all the client form needs to
[quoted text clipped - 4 lines]
| you might leave the file open and will need to forcibly close it on the next
| run.

Sorry, but that is rather sloppy and unreliable.  It makes more check work
than really needed.

And sure does explain a lot.  <g>
Randem - 04 Jul 2009 05:07 GMT
Yes, it sure does. He is creating a client/server type app and you have no
clue...

Signature

Randem Systems
Your Installation Specialist
The Top Inno Setup Script Generator
http://www.randem.com/innoscript.html
Disk Read Error Press Ctl+Alt+Del to Restart
http://www.randem.com/discus/messages/9402/9406.html?1236319938

> | You could do something simple like open a file (exclusive)when you load
> the
[quoted text clipped - 14 lines]
>
> And sure does explain a lot.  <g>
Nobody - 04 Jul 2009 18:04 GMT
I would use Winsock for this. The main or server app listens to incoming
connections, and the clients connect to it using TCP. Below is a sample
code. To try it, add 1 command button with name "btnSend", and 2 Winsock
controls to Form1. Set the name for the first one to "wsckServer", and Index
property to 0, and "wsckClient" for the second one while leave Index
property blank, then add the following code:

Option Explicit

Private Sub Form_Load()
   ' Server
   wsckServer(0).LocalPort = 5555
   wsckServer(0).Listen

   ' Client
   wsckClient.RemotePort = 5555
   wsckClient.RemoteHost = "127.0.0.1" ' Local host
End Sub

' Server side =======================================

Private Sub wsckServer_ConnectionRequest(Index As Integer, _
   ByVal requestID As Long)
   wsckServer(GetNextAvailableSocket()).Accept requestID
End Sub

Private Sub wsckServer_DataArrival(Index As Integer, ByVal bytesTotal As
Long)
   Dim s As String

   wsckServer(Index).GetData s, vbString
   Debug.Print "wsckServer_DataArrival: s = '" & s & "'"

   Select Case Left(s, 3)
       Case "001":
           ' Send response
           wsckServer(Index).SendData "Ack"
   End Select
End Sub

' Get the next available socket, or load a new one if all are used
Private Function GetNextAvailableSocket() As Long
   Dim iNext As Long
   Dim iWinsockControlsCount As Long
   Dim ctl As Control

   iNext = 0 ' Assume no unused socket found
   For Each ctl In Me.Controls
       If TypeOf ctl Is Winsock Then
           If ctl.Name = "wsckServer" Then
               If ctl.Index > 0 Then ' Avoid the first socket
                   If ctl.State = sckClosed Then
                       ' Found unused socket
                       iNext = ctl.Index
                       Exit For
                   End If
                   ' Keep track of the maximum Index number
                   If iWinsockControlsCount < ctl.Index Then
                       iWinsockControlsCount = ctl.Index
                   End If
               End If
           End If
       End If
   Next

   If iNext = 0 Then
       iNext = iWinsockControlsCount + 1
       Load wsckServer(iNext)
   End If

   Debug.Print "GetNextAvailableSocket: returning " & iNext
   GetNextAvailableSocket = iNext
End Function

' Client side  =======================================

Private Sub btnSend_Click()
   Dim t As Single

   wsckClient.LocalPort = 0 ' Use dynamic port
   wsckClient.Connect

   ' Wait for up to 5 seconds to connect, or use the Connect event
   t = Timer
   Do While Timer - t < 5 And wsckClient.State <> sckConnected
       DoEvents
   Loop

   If wsckClient.State = sckConnected Then
       wsckClient.SendData "001"
   Else
       Debug.Print "Timeout waiting to connect"
   End If
End Sub

Private Sub wsckClient_DataArrival(ByVal bytesTotal As Long)
   Dim s As String

   wsckClient.GetData s, vbString
   Debug.Print "wsckClient_DataArrival: s = '" & s & "'"

   Select Case Left(s, 3)
       Case "Ack":
           ' Add code here
   End Select
End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.