Preventing a form from opening a second time

This is a discussion on Preventing a form from opening a second time within the Paradox Database forums in Other Databases category; I have a form that assists me with programming tasks, and I would like to make sure it does not open a second time. Using a script, I am familiar with doing something like the following ( I am typing this off the top of head, so it is not syntax checked ) stFormname= :ztools:\\coding\\pickfields.fsl stTitle = Pick Field Utility if not f.attach(stTitleOfForm) then f.open(stFormname) endif And that would be fine, except, as part of my issue, I would like the code to be *inside* the form itself, not in an external script. I ended up ...

Go Back   Database Forum > Other Databases > Paradox Database

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-26-2008, 01:51 AM
Default Preventing a form from opening a second time


I have a form that assists me with programming tasks,
and I would like to make sure it does not open a second time.

Using a script,
I am familiar with doing something like the following
( I am typing this off the top of head, so it is not syntax checked )

stFormname=":ztools:\\coding\\pickfields.fsl"
stTitle = "Pick Field Utility"
if not f.attach(stTitleOfForm) then
f.open(stFormname)
endif

And that would be fine,
except, as part of my issue,
I would like the code to be *inside* the form itself,
not in an external script.

I ended up figuring out 2 ways to do this,
and I wanted to share this experience,
but I welcome any input from others on how to do this.
This post gets a little long, so proceed with caution. =)

Remember, I want the prevention code to be inside the form,
not in an external script, form, or library.


Solution 1 :

I decided to start in the init section of the form,
and I am using either pdox 10 sp3 or pdox X3.

Inside the form, I found the following useful :

f.attach()
stTitleThisForm = f.getTitle()

This allows me to fetch the title on the fly, as the form that is opening,
and eliminates the need to hard code the form title.
( which I think is a huge plus )

I then used enumFormnames(arForms) to list all open forms to an array,
and I can step through that array to see that the form is in there twice.
If I find that arForms contains stTitleThisForm more than once,
I simply exited the form that was loading as follows :

eventInfo.setErrorCode(canNotArrive)
close()
return

This closes the form, and only leaves one instance of the form open on the
desktop.
But, that form is not guaranteed to be on top,
and even though I did not want this form open 2 times,
I did want the open copy of this form to come to the top of the pile.

So as many programming tasks happen,
the main idea was fairly easy, but the last touch was a pita.


If I had a form handle, I could use f.bringToTop(),
but.........I dont see any way to get a handle to the first instance of the
form,
from the code in that form as it is opening the second time.

The problem is, the title of the 1st instance is the same title as the 2nd
instance,
and we are now talking about the code that is running in the 2nd instance of
the form.

If you call enumFormnames(arForms)
and then call ( or somethign similar, depending on what forms open)
if arForms[1] = arForms[2] then
msgInfo("Note","2 titles are the same")
endif
So, this proves that pdox thinks both instances of the form have the same
name.
Obviously the above indexes to the array need to be correct,
to reflect where they are ni the array.
I simply used arForms.view() to look at array and determine necessary index.

Trying
f.attach(arForms[1]) and f.attach(arForms[2])
always returned a form handle to the 2nd instance of the form,
which makes sense , since it was the same exact title.

Without the form handle,
I was unable to call the pdox form method bringToTop.


I did find that there was a windows api call,
and the uses statement looks like

"USER32"
BringWindowToTop(hWnd CLONG) CLONG

This requires a windows handle for the form,
not a paradox form handle.
This ended up being fairly easy to get,
using the paradox procedure : enumDesktopWindowHandles,
which returns all paradox form handles in a dynarray,
with the index of the dynarray being the handle,
and the value being the window name.

enumDesktopWindowHandles(dyHandles)

We get the handle for the currently executing 2nd instance of the form
rather easily as follows :
;This gets handle of the current instance of the form
liHandleThisForm = f.windowHandle()

;init handle of the 1st instance we are looking for
liHandleOtherForm = 0

Now step thru dynarray, and find title
but remember title is in the dynarray 2 times
and we want the handle for the one that is different from the current form

foreach stHandle in dyHandles
stTitle = dyHandles[stHandle]

if stTitle = stTitleThisForm then
liHandle = longint(stHandle)
if liHandle <> liHandleThisForm then
liHandleOtherForm = liHandle
quitloop
endif
endif

endforeach

;If for some reason, we didnt find it, do not call windows api function
if liHandleOtherForm = 0 then
return(false)
endif

; call api function, to bring 1st instance to the top of the pile
liRet = BringWindowToTop(liHandleOtherForm)

and then exit and close self as mentioned before



Second Solution

I had mentioned before,
the issue to get pdox form handle is difficult
because both instances have the same exact title.

I went and manually entered the form title to be
stTitle = "Pick Field Utilityx"

My idea was to modify the title of the open form,
so that it had a slightly differnt value than the form in the init stage.
This worked rather well.

So the init method looks like

method init(var eventInfo Event)

var
fThis,fOrig form
stTitleThisForm,stNewTitle string
endvar

; attach to current form, and get its title
fThis.attach()
stTitleThisForm = fThis.getTitle()

; for new title, removes last char from orig title
stNewTitle = stTitleThisForm.substr(1,stTitleThisForm.sizeEx()-1)

; if possible, attach to corrected title, bring to top, and close down
if fOrig.attach(stNewTitle) then
fOrig.bringtotop()
eventInfo.setErrorCode(canNotArrive)
close()
return
endif

; form was not open, so change this form title
fThis.setTitle(stNewTitle)

endMethod

Thats it.
It seems to work ok.


I added this to the close method of this form,

;fix up title to original
f.attach()
f.setTitle(f.getTitle() + "x")

I was thinking of putting a box object on the form,
and putting all relevant code inside it,
so that I could simply plop it on any form that needed this code,
but I did not get around to that yet.

Perhaps there are easier ways to do this.
Comments, suggestions, and discussion always welcome.


Robert Wiltshire
































Reply With Quote
  #2  
Old 07-26-2008, 08:49 AM
Default Re: Preventing a form from opening a second time

third possible solution.. when you open the form, create a companion text
file.. if the text file exists, don't open the form.. should be way less
code..

--
Steven Green - Myrtle Beach, South Carolina USA

Diamond Software Group
http://www.diamondsg.com/main.htm
Paradox Support & Sales

Diamond Sports Gems
http://www.diamondsg.com/gemsmain.htm
Sports Memorabilia and Trading Cards

"Robert Wiltshire" wrote in message
news:488aad74$1-at-pnews.thedbcommunity.com...
>
> I have a form that assists me with programming tasks,
> and I would like to make sure it does not open a second time.
>
> Using a script,
> I am familiar with doing something like the following
> ( I am typing this off the top of head, so it is not syntax checked )
>
> stFormname=":ztools:\\coding\\pickfields.fsl"
> stTitle = "Pick Field Utility"
> if not f.attach(stTitleOfForm) then
> f.open(stFormname)
> endif
>
> And that would be fine,
> except, as part of my issue,
> I would like the code to be *inside* the form itself,
> not in an external script.
>
> I ended up figuring out 2 ways to do this,
> and I wanted to share this experience,
> but I welcome any input from others on how to do this.
> This post gets a little long, so proceed with caution. =)
>
> Remember, I want the prevention code to be inside the form,
> not in an external script, form, or library.
>
>
> Solution 1 :
>
> I decided to start in the init section of the form,
> and I am using either pdox 10 sp3 or pdox X3.
>
> Inside the form, I found the following useful :
>
> f.attach()
> stTitleThisForm = f.getTitle()
>
> This allows me to fetch the title on the fly, as the form that is opening,
> and eliminates the need to hard code the form title.
> ( which I think is a huge plus )
>
> I then used enumFormnames(arForms) to list all open forms to an array,
> and I can step through that array to see that the form is in there twice.
> If I find that arForms contains stTitleThisForm more than once,
> I simply exited the form that was loading as follows :
>
> eventInfo.setErrorCode(canNotArrive)
> close()
> return
>
> This closes the form, and only leaves one instance of the form open on the
> desktop.
> But, that form is not guaranteed to be on top,
> and even though I did not want this form open 2 times,
> I did want the open copy of this form to come to the top of the pile.
>
> So as many programming tasks happen,
> the main idea was fairly easy, but the last touch was a pita.
>
>
> If I had a form handle, I could use f.bringToTop(),
> but.........I dont see any way to get a handle to the first instance of
> the form,
> from the code in that form as it is opening the second time.
>
> The problem is, the title of the 1st instance is the same title as the 2nd
> instance,
> and we are now talking about the code that is running in the 2nd instance
> of the form.
>
> If you call enumFormnames(arForms)
> and then call ( or somethign similar, depending on what forms open)
> if arForms[1] = arForms[2] then
> msgInfo("Note","2 titles are the same")
> endif
> So, this proves that pdox thinks both instances of the form have the same
> name.
> Obviously the above indexes to the array need to be correct,
> to reflect where they are ni the array.
> I simply used arForms.view() to look at array and determine necessary
> index.
>
> Trying
> f.attach(arForms[1]) and f.attach(arForms[2])
> always returned a form handle to the 2nd instance of the form,
> which makes sense , since it was the same exact title.
>
> Without the form handle,
> I was unable to call the pdox form method bringToTop.
>
>
> I did find that there was a windows api call,
> and the uses statement looks like
>
> "USER32"
> BringWindowToTop(hWnd CLONG) CLONG
>
> This requires a windows handle for the form,
> not a paradox form handle.
> This ended up being fairly easy to get,
> using the paradox procedure : enumDesktopWindowHandles,
> which returns all paradox form handles in a dynarray,
> with the index of the dynarray being the handle,
> and the value being the window name.
>
> enumDesktopWindowHandles(dyHandles)
>
> We get the handle for the currently executing 2nd instance of the form
> rather easily as follows :
> ;This gets handle of the current instance of the form
> liHandleThisForm = f.windowHandle()
>
> ;init handle of the 1st instance we are looking for
> liHandleOtherForm = 0
>
> Now step thru dynarray, and find title
> but remember title is in the dynarray 2 times
> and we want the handle for the one that is different from the current form
>
> foreach stHandle in dyHandles
> stTitle = dyHandles[stHandle]
>
> if stTitle = stTitleThisForm then
> liHandle = longint(stHandle)
> if liHandle <> liHandleThisForm then
> liHandleOtherForm = liHandle
> quitloop
> endif
> endif
>
> endforeach
>
> ;If for some reason, we didnt find it, do not call windows api function
> if liHandleOtherForm = 0 then
> return(false)
> endif
>
> ; call api function, to bring 1st instance to the top of the pile
> liRet = BringWindowToTop(liHandleOtherForm)
>
> and then exit and close self as mentioned before
>
>
>
> Second Solution
>
> I had mentioned before,
> the issue to get pdox form handle is difficult
> because both instances have the same exact title.
>
> I went and manually entered the form title to be
> stTitle = "Pick Field Utilityx"
>
> My idea was to modify the title of the open form,
> so that it had a slightly differnt value than the form in the init stage.
> This worked rather well.
>
> So the init method looks like
>
> method init(var eventInfo Event)
>
> var
> fThis,fOrig form
> stTitleThisForm,stNewTitle string
> endvar
>
> ; attach to current form, and get its title
> fThis.attach()
> stTitleThisForm = fThis.getTitle()
>
> ; for new title, removes last char from orig title
> stNewTitle = stTitleThisForm.substr(1,stTitleThisForm.sizeEx()-1)
>
> ; if possible, attach to corrected title, bring to top, and close down
> if fOrig.attach(stNewTitle) then
> fOrig.bringtotop()
> eventInfo.setErrorCode(canNotArrive)
> close()
> return
> endif
>
> ; form was not open, so change this form title
> fThis.setTitle(stNewTitle)
>
> endMethod
>
> Thats it.
> It seems to work ok.
>
>
> I added this to the close method of this form,
>
> ;fix up title to original
> f.attach()
> f.setTitle(f.getTitle() + "x")
>
> I was thinking of putting a box object on the form,
> and putting all relevant code inside it,
> so that I could simply plop it on any form that needed this code,
> but I did not get around to that yet.
>
> Perhaps there are easier ways to do this.
> Comments, suggestions, and discussion always welcome.
>
>
> Robert Wiltshire
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



Reply With Quote
  #3  
Old 07-26-2008, 01:52 PM
Default Re: Preventing a form from opening a second time


> third possible solution.. when you open the form, create a companion text
> file.. if the text file exists, don't open the form.. should be way less
> code..


Interesting idea, thank you.
This would certainly need something in the close event to whack the file.

I suppose games could be played with environment strings too.

The only thing that bothers at me at first
with either the file technique ( or environment string) ,
is ..........what if form does not clean up after itself for some unusual
reason.

I had the same concerns with solution #2
where I modified the form title after opening.

Issue might be similar to a ".lck" file being left stray.
It is not usual that it happens, but if it did,
figuring out why the form is not opening,
and then finding the file to delete might prove to be difficult.

Thank you for suggestion.






Reply With Quote
  #4  
Old 07-26-2008, 02:23 PM
Default Re: Preventing a form from opening a second time


> I suppose games could be played with environment strings too.


This was my first thought.

Second was a registry entry.

> is ..........what if form does not clean up after itself for some
> unusual reason.


Which is a plus for environment string. It 'cleans up' when Paradox
closes at the very worst.

Particularly is the one-line aspect of either of these two. if not
.... then form.open() - rather than declaring file type before being
able to work with it. Also, they are universal to the instance of
Paradox.

Moreover, with environmentstrings you can enumenvironmentstrings() and
have instant access to them all to know what is open. (if you use a
standard method of naming the environmentstrings you use for flagging
this)


------------------------------
Tony McGuire


Reply With Quote
  #5  
Old 07-26-2008, 05:48 PM
Default Re: Preventing a form from opening a second time


Originally I had looked at this task as

if ifFormAlreadyOpen
then
do not allow 2nd instance of form to open
else
allow this form to open
endif


But really it is more like this :

if isFormAlreadyOpen
then
do not allow 2nd instance of form to open
surface 1st instance of form
else
allow this form to open
endif

And while using a semaphore,
such as a file or environment variable helps to determine
the "isFormAlreadyOpen" part of the task,
they do not address the total solution necessary,
specifically the 'surface 1st instance of form'

Thank you for comments and suggstions.
















Reply With Quote
  #6  
Old 07-26-2008, 05:52 PM
Default Re: Preventing a form from opening a second time

Robert

I think I would go with your first thought with the following additions.

1. If your form doesn't exist as an open form, then open it and give it a
different name.
2. Next, since your open form has a different name, change the check of the
enumerated list and for the new form name and if it exists, then the form is
open.
3. Since the open form has a different name than the form you are trying to
open, you can do a bring to top the form you want on top and close the form
that is opening.

Just a WAG, but hope it helps. - Rick

"Robert Wiltshire" wrote in message
news:488aad74$1-at-pnews.thedbcommunity.com...
>
> I have a form that assists me with programming tasks,
> and I would like to make sure it does not open a second time.
>
> Using a script,
> I am familiar with doing something like the following
> ( I am typing this off the top of head, so it is not syntax checked )
>
> stFormname=":ztools:\\coding\\pickfields.fsl"
> stTitle = "Pick Field Utility"
> if not f.attach(stTitleOfForm) then
> f.open(stFormname)
> endif
>
> And that would be fine,
> except, as part of my issue,
> I would like the code to be *inside* the form itself,
> not in an external script.
>
> I ended up figuring out 2 ways to do this,
> and I wanted to share this experience,
> but I welcome any input from others on how to do this.
> This post gets a little long, so proceed with caution. =)
>
> Remember, I want the prevention code to be inside the form,
> not in an external script, form, or library.
>
>
> Solution 1 :
>
> I decided to start in the init section of the form,
> and I am using either pdox 10 sp3 or pdox X3.
>
> Inside the form, I found the following useful :
>
> f.attach()
> stTitleThisForm = f.getTitle()
>
> This allows me to fetch the title on the fly, as the form that is opening,
> and eliminates the need to hard code the form title.
> ( which I think is a huge plus )
>
> I then used enumFormnames(arForms) to list all open forms to an array,
> and I can step through that array to see that the form is in there twice.
> If I find that arForms contains stTitleThisForm more than once,
> I simply exited the form that was loading as follows :
>
> eventInfo.setErrorCode(canNotArrive)
> close()
> return
>
> This closes the form, and only leaves one instance of the form open on the
> desktop.
> But, that form is not guaranteed to be on top,
> and even though I did not want this form open 2 times,
> I did want the open copy of this form to come to the top of the pile.
>
> So as many programming tasks happen,
> the main idea was fairly easy, but the last touch was a pita.
>
>
> If I had a form handle, I could use f.bringToTop(),
> but.........I dont see any way to get a handle to the first instance of
> the form,
> from the code in that form as it is opening the second time.
>
> The problem is, the title of the 1st instance is the same title as the 2nd
> instance,
> and we are now talking about the code that is running in the 2nd instance
> of the form.
>
> If you call enumFormnames(arForms)
> and then call ( or somethign similar, depending on what forms open)
> if arForms[1] = arForms[2] then
> msgInfo("Note","2 titles are the same")
> endif
> So, this proves that pdox thinks both instances of the form have the same
> name.
> Obviously the above indexes to the array need to be correct,
> to reflect where they are ni the array.
> I simply used arForms.view() to look at array and determine necessary
> index.
>
> Trying
> f.attach(arForms[1]) and f.attach(arForms[2])
> always returned a form handle to the 2nd instance of the form,
> which makes sense , since it was the same exact title.
>
> Without the form handle,
> I was unable to call the pdox form method bringToTop.
>
>
> I did find that there was a windows api call,
> and the uses statement looks like
>
> "USER32"
> BringWindowToTop(hWnd CLONG) CLONG
>
> This requires a windows handle for the form,
> not a paradox form handle.
> This ended up being fairly easy to get,
> using the paradox procedure : enumDesktopWindowHandles,
> which returns all paradox form handles in a dynarray,
> with the index of the dynarray being the handle,
> and the value being the window name.
>
> enumDesktopWindowHandles(dyHandles)
>
> We get the handle for the currently executing 2nd instance of the form
> rather easily as follows :
> ;This gets handle of the current instance of the form
> liHandleThisForm = f.windowHandle()
>
> ;init handle of the 1st instance we are looking for
> liHandleOtherForm = 0
>
> Now step thru dynarray, and find title
> but remember title is in the dynarray 2 times
> and we want the handle for the one that is different from the current form
>
> foreach stHandle in dyHandles
> stTitle = dyHandles[stHandle]
>
> if stTitle = stTitleThisForm then
> liHandle = longint(stHandle)
> if liHandle <> liHandleThisForm then
> liHandleOtherForm = liHandle
> quitloop
> endif
> endif
>
> endforeach
>
> ;If for some reason, we didnt find it, do not call windows api function
> if liHandleOtherForm = 0 then
> return(false)
> endif
>
> ; call api function, to bring 1st instance to the top of the pile
> liRet = BringWindowToTop(liHandleOtherForm)
>
> and then exit and close self as mentioned before
>
>
>
> Second Solution
>
> I had mentioned before,
> the issue to get pdox form handle is difficult
> because both instances have the same exact title.
>
> I went and manually entered the form title to be
> stTitle = "Pick Field Utilityx"
>
> My idea was to modify the title of the open form,
> so that it had a slightly differnt value than the form in the init stage.
> This worked rather well.
>
> So the init method looks like
>
> method init(var eventInfo Event)
>
> var
> fThis,fOrig form
> stTitleThisForm,stNewTitle string
> endvar
>
> ; attach to current form, and get its title
> fThis.attach()
> stTitleThisForm = fThis.getTitle()
>
> ; for new title, removes last char from orig title
> stNewTitle = stTitleThisForm.substr(1,stTitleThisForm.sizeEx()-1)
>
> ; if possible, attach to corrected title, bring to top, and close down
> if fOrig.attach(stNewTitle) then
> fOrig.bringtotop()
> eventInfo.setErrorCode(canNotArrive)
> close()
> return
> endif
>
> ; form was not open, so change this form title
> fThis.setTitle(stNewTitle)
>
> endMethod
>
> Thats it.
> It seems to work ok.
>
>
> I added this to the close method of this form,
>
> ;fix up title to original
> f.attach()
> f.setTitle(f.getTitle() + "x")
>
> I was thinking of putting a box object on the form,
> and putting all relevant code inside it,
> so that I could simply plop it on any form that needed this code,
> but I did not get around to that yet.
>
> Perhaps there are easier ways to do this.
> Comments, suggestions, and discussion always welcome.
>
>
> Robert Wiltshire
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



Reply With Quote
  #7  
Old 07-27-2008, 04:37 AM
Default Re: Preventing a form from opening a second time


Ok, solved good enough for me.

The pita issue on this ended up being that

>the issue to get pdox form handle is difficult
>because both instances have the same exact title.


Originall, my second solution from the first post,
was to change title after form was open,
so that when 2nd instance tried to open,
I had different titles to work with ,
and therefore could stay totally with pdox methods
of attach and bringToTop.


Using that approach,
plus reading suggestions leads me to
a slightly more efficient approach to that technique.
Instead of modifying the title after it opens,
I modify the form title of the second instance of the form,
which is about to be closed anyways.

The steps are like this

get handle of form opening for second time
fThis.attach()
gettitle for form
stTitle = fThis.getTitle()
get all form names into array
enumFormnames(arForms)
cycle through array and see how many times stTitle exists in arForms
used a for loop and test to see if arForms[x]=stTitle

if arForms contains stTitle 2 times, then change title of opening form
fThis.setTitle(stTitle+"2")
Since we will be closing this 2nd instance of the form,
it does not matter if we change the title.

That will leave us with only one form with stTitle as its title,
and it is simple matter to attach and then bringToTop.
fOrig.attach(stTitle)
fOrig.bringToTop()

Thank you all for taking time to read this and offer suggestions.
It was much appreciated and helped with the brainstorming process.

Robert Wiltshire



****Final code below ***************

Code in the init section now looks like this

method init(var eventInfo Event)

if isFormOpeningAlreadyOpen() then
eventInfo.setErrorCode(canNotArrive)
close()
return
endif

endMethod


and code in custom method looks like

method isFormOpeningAlreadyOpen() logical

var
fThis,fOrig form
stTitleThisForm string
arForms array[] string
liEach,liMatches longint
endvar

; attach to current form, and get its title
fThis.attach()
stTitleThisForm = fThis.getTitle()

;use opal enum method to populate array,
; and then count number of times current title is in array
enumFormnames(arForms)
liMatches = 0
for liEach from 1 to arForms.size()
if arForms[liEach] = stTitleThisForm then
liMatches = liMatches + 1
endif
endfor

;if found zero or one time, then not already open
if liMatches <= 1 then
return(false)
endif

; since we will be closing this form
; modify its title, so that attach gets other form
fThis.setTitle(stTitleThisForm+"2")

if fOrig.attach(stTitleThisForm)
then fOrig.bringToTop()
else msgStop("Error","Did not find 2nd instance of form")
endif

return(true)

endMethod














"Rick Rans" wrote in message
news:488b8e75$1-at-pnews.thedbcommunity.com...
> Robert
>
> I think I would go with your first thought with the following additions.
>
> 1. If your form doesn't exist as an open form, then open it and give it a
> different name.
> 2. Next, since your open form has a different name, change the check of
> the enumerated list and for the new form name and if it exists, then the
> form is open.
> 3. Since the open form has a different name than the form you are trying
> to open, you can do a bring to top the form you want on top and close the
> form that is opening.
>
> Just a WAG, but hope it helps. - Rick
>
> "Robert Wiltshire" wrote in message
> news:488aad74$1-at-pnews.thedbcommunity.com...
>>
>> I have a form that assists me with programming tasks,
>> and I would like to make sure it does not open a second time.
>>
>> Using a script,
>> I am familiar with doing something like the following
>> ( I am typing this off the top of head, so it is not syntax checked )
>>
>> stFormname=":ztools:\\coding\\pickfields.fsl"
>> stTitle = "Pick Field Utility"
>> if not f.attach(stTitleOfForm) then
>> f.open(stFormname)
>> endif
>>
>> And that would be fine,
>> except, as part of my issue,
>> I would like the code to be *inside* the form itself,
>> not in an external script.
>>
>> I ended up figuring out 2 ways to do this,
>> and I wanted to share this experience,
>> but I welcome any input from others on how to do this.
>> This post gets a little long, so proceed with caution. =)
>>
>> Remember, I want the prevention code to be inside the form,
>> not in an external script, form, or library.
>>
>>
>> Solution 1 :
>>
>> I decided to start in the init section of the form,
>> and I am using either pdox 10 sp3 or pdox X3.
>>
>> Inside the form, I found the following useful :
>>
>> f.attach()
>> stTitleThisForm = f.getTitle()
>>
>> This allows me to fetch the title on the fly, as the form that is
>> opening,
>> and eliminates the need to hard code the form title.
>> ( which I think is a huge plus )
>>
>> I then used enumFormnames(arForms) to list all open forms to an array,
>> and I can step through that array to see that the form is in there twice.
>> If I find that arForms contains stTitleThisForm more than once,
>> I simply exited the form that was loading as follows :
>>
>> eventInfo.setErrorCode(canNotArrive)
>> close()
>> return
>>
>> This closes the form, and only leaves one instance of the form open on
>> the desktop.
>> But, that form is not guaranteed to be on top,
>> and even though I did not want this form open 2 times,
>> I did want the open copy of this form to come to the top of the pile.
>>
>> So as many programming tasks happen,
>> the main idea was fairly easy, but the last touch was a pita.
>>
>>
>> If I had a form handle, I could use f.bringToTop(),
>> but.........I dont see any way to get a handle to the first instance of
>> the form,
>> from the code in that form as it is opening the second time.
>>
>> The problem is, the title of the 1st instance is the same title as the
>> 2nd instance,
>> and we are now talking about the code that is running in the 2nd instance
>> of the form.
>>
>> If you call enumFormnames(arForms)
>> and then call ( or somethign similar, depending on what forms open)
>> if arForms[1] = arForms[2] then
>> msgInfo("Note","2 titles are the same")
>> endif
>> So, this proves that pdox thinks both instances of the form have the same
>> name.
>> Obviously the above indexes to the array need to be correct,
>> to reflect where they are ni the array.
>> I simply used arForms.view() to look at array and determine necessary
>> index.
>>
>> Trying
>> f.attach(arForms[1]) and f.attach(arForms[2])
>> always returned a form handle to the 2nd instance of the form,
>> which makes sense , since it was the same exact title.
>>
>> Without the form handle,
>> I was unable to call the pdox form method bringToTop.
>>
>>
>> I did find that there was a windows api call,
>> and the uses statement looks like
>>
>> "USER32"
>> BringWindowToTop(hWnd CLONG) CLONG
>>
>> This requires a windows handle for the form,
>> not a paradox form handle.
>> This ended up being fairly easy to get,
>> using the paradox procedure : enumDesktopWindowHandles,
>> which returns all paradox form handles in a dynarray,
>> with the index of the dynarray being the handle,
>> and the value being the window name.
>>
>> enumDesktopWindowHandles(dyHandles)
>>
>> We get the handle for the currently executing 2nd instance of the form
>> rather easily as follows :
>> ;This gets handle of the current instance of the form
>> liHandleThisForm = f.windowHandle()
>>
>> ;init handle of the 1st instance we are looking for
>> liHandleOtherForm = 0
>>
>> Now step thru dynarray, and find title
>> but remember title is in the dynarray 2 times
>> and we want the handle for the one that is different from the current
>> form
>>
>> foreach stHandle in dyHandles
>> stTitle = dyHandles[stHandle]
>>
>> if stTitle = stTitleThisForm then
>> liHandle = longint(stHandle)
>> if liHandle <> liHandleThisForm then
>> liHandleOtherForm = liHandle
>> quitloop
>> endif
>> endif
>>
>> endforeach
>>
>> ;If for some reason, we didnt find it, do not call windows api function
>> if liHandleOtherForm = 0 then
>> return(false)
>> endif
>>
>> ; call api function, to bring 1st instance to the top of the pile
>> liRet = BringWindowToTop(liHandleOtherForm)
>>
>> and then exit and close self as mentioned before
>>
>>
>>
>> Second Solution
>>
>> I had mentioned before,
>> the issue to get pdox form handle is difficult
>> because both instances have the same exact title.
>>
>> I went and manually entered the form title to be
>> stTitle = "Pick Field Utilityx"
>>
>> My idea was to modify the title of the open form,
>> so that it had a slightly differnt value than the form in the init stage.
>> This worked rather well.
>>
>> So the init method looks like
>>
>> method init(var eventInfo Event)
>>
>> var
>> fThis,fOrig form
>> stTitleThisForm,stNewTitle string
>> endvar
>>
>> ; attach to current form, and get its title
>> fThis.attach()
>> stTitleThisForm = fThis.getTitle()
>>
>> ; for new title, removes last char from orig title
>> stNewTitle = stTitleThisForm.substr(1,stTitleThisForm.sizeEx()-1)
>>
>> ; if possible, attach to corrected title, bring to top, and close down
>> if fOrig.attach(stNewTitle) then
>> fOrig.bringtotop()
>> eventInfo.setErrorCode(canNotArrive)
>> close()
>> return
>> endif
>>
>> ; form was not open, so change this form title
>> fThis.setTitle(stNewTitle)
>>
>> endMethod
>>
>> Thats it.
>> It seems to work ok.
>>
>>
>> I added this to the close method of this form,
>>
>> ;fix up title to original
>> f.attach()
>> f.setTitle(f.getTitle() + "x")
>>
>> I was thinking of putting a box object on the form,
>> and putting all relevant code inside it,
>> so that I could simply plop it on any form that needed this code,
>> but I did not get around to that yet.
>>
>> Perhaps there are easier ways to do this.
>> Comments, suggestions, and discussion always welcome.
>>
>>
>> Robert Wiltshire
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

>
>



Reply With Quote
  #8  
Old 07-28-2008, 11:53 PM
Default Re: Preventing a form from opening a second time


Robert,

Sorry for jumping in late, but why not have a stub form that you open. All
it does is check to see if your main form is open. If it is, it uses it.
If not, it opens it. Either way, the stub form then immediately closes.
Sounds simple?

HTH,
Jim Moseley
Reply With Quote
  #9  
Old 07-29-2008, 12:24 AM
Default Re: Preventing a form from opening a second time

Jim

That would be good if you are starting out and always call the stub form. If
you have an application that is already in use, you may not remember where
all the calling locations are to change to call the stub form. Also the user
may already be opening the form by name and he/she would need to change the
opening process. I think Robert has a solution that he can now put into any
form that he wants to open or bring to the top.

Just my 2cents.

"Jim Moseley" wrote in message
news:488e862e$1-at-pnews.thedbcommunity.com...
>
> Robert,
>
> Sorry for jumping in late, but why not have a stub form that you open.
> All
> it does is check to see if your main form is open. If it is, it uses it.
> If not, it opens it. Either way, the stub form then immediately closes.
> Sounds simple?
>
> HTH,
> Jim Moseley



Reply With Quote
  #10  
Old 07-29-2008, 01:10 AM
Default Re: Preventing a form from opening a second time


> Sorry for jumping in late, but why not have a stub form that you open.
> All
> it does is check to see if your main form is open. If it is, it uses it.
> If not, it opens it. Either way, the stub form then immediately closes.
> Sounds simple?


Sure....and I do appreciate all suggestions,
but as Rick surmised I really wanted to have the code in the form itself,
due to various reasons.

In the opening paragraph of the initial posting,
I think you will find that I was aware of what you were mentioning,
but desired it to be internal to the form,
and not in an external script,library, or form.

Thank you for the suggestion.


I did post my final solution on 7/27/2008,
which ended up being about 30 lines of code,
which is small enough for what I was hoping to do.

I have since created a custom object,
which I do by placing a box object on a form,
and then I place custom code in it.
I name my object, with a "co" prefix (Custom Object),
and then I put the code for this method into the object.
Also, I modify property of an object to make it not visible at run time.

I then added the object to my object repository,
( a blank form located somewhere).

Inside the code of the method in the custom object,
some may find this an interesting technique.

I comment out sample calling code,
between the method declaration line and the opening var as follows:


method isFormOpeningAlreadyOpen() logical

; sample code to be cnp to form init method
{
if coOneInstance.isFormOpeningAlreadyOpen() then
eventInfo.setErrorCode(canNotArrive)
close()
return
endif
}

var

....
endmethod


Between the curly braces { and },
(which are handy to comment out multiple lines at once),
is the code that goes into init section of the form.

Now when I want to add this behavior to a new form,
I launch my form goodies form,
copy and paste the custom object to the desired form,
and then copy and paste code from this method in the object,
to the init section in a form.....total time now, less than a minute,
and the code is acceptably small enough for me


Robert Wiltshire



Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 01:14 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.