Prevent adding form twice

jschuchert

Active Member
Licensed User
Longtime User
The title may be misleading but here is what I am trying to do in my app:

A menu item (from the menu editor) adds a form and other controls, then allows a variable to a set a value to be used throughout the application. However, I want the user to be able to change the value when needed without leaving the program. But if the user clicks the menu item again, it will try to add the form again and throws an error. I understand that and am hoping I can add code somewhere that will prevent it. I have tried to add a counter that would bypass the 'addform' command and simply show the form again containing the previous value but that isn't working. I can always add another menu item to only 'show the form' but that seems like poor programming. Just closing the form doesn't work. Here is the code:

B4X:
Sub mnuDistMult_Click
If intcounter > 0 Then frmdistmult.show [color=red] 'global variable set to 0 when app opens'[/color]
AddForm("frmDistMult","Distance Multiplier")
frmdistmult.color=cSilver
frmdistmult.show
AddTextBox("frmdistmult","txtdm",40,20,50,20,"1")
AddLabel("frmdistmult","lbldm",40,45,50,20,"Dist Mult")
lbldm.color=cSilver
AddButton("frmdistmult","btnDM",125,20,50,20,"OK")
AddButton("frmdistmult","btnDMMenu",125,50,50,20,"Menu")
AddLabel("frmdistmult","lbldminfo",40,80,150,40,"")
lbldminfo.color=cSilver
intcounter=intcounter+1 [color=red] 'increments counter' [/color]
End Sub

Sub btndm_click
sngdm=txtdm.text [color=red] sets a value that should be able to be changed [/color]
lbldminfo.text="All entered distances will now be multiplied by " & sngdm 
End Sub 

Sub btndmmenu_click
frmdistmult.close
frmmenu.Show [color=red] 'this is the main menu...not from the menu editor[/color]
End Sub

I can do it without having a menu item but I would need to add textboxes on the forms being affected and the space is limited. Any solution would also apply to other items I am comtemplating. I suspect the answer is pretty easy but I have a tendency to overlook those. Thanks for any help and Happy New Year to all.

Jim Schuchert
 

klaus

Expert
Licensed User
Longtime User
Try this code:
B4X:
Sub mnuDistMult_Click
If intcounter = 0 Then [COLOR=red]'global variable set to 0 when app opens'[/COLOR]
  AddForm("frmDistMult","Distance Multiplier")
  frmdistmult.color=cSilver
  frmdistmult.show
  AddTextBox("frmdistmult","txtdm",40,20,50,20,"1")
  AddLabel("frmdistmult","lbldm",40,45,50,20,"Dist Mult")
  lbldm.color=cSilver
  AddButton("frmdistmult","btnDM",125,20,50,20,"OK")
  AddButton("frmdistmult","btnDMMenu",125,50,50,20,"Menu")
  AddLabel("frmdistmult","lbldminfo",40,80,150,40,"")
  lbldminfo.color=cSilver
  intcounter = 1 [COLOR=red]'sets counter to 1' [/COLOR]
[COLOR=black]End If[/COLOR]
frmdistmult.show 
End Sub

At the beginning of the program intcounter=0 during the first run of Sub mnuDistMult_Click the form is initialized and intcounter is set to 1.
All future clicks, with intcounter=1, will not initialize the form again.

Best regards.
 

derez

Expert
Licensed User
Longtime User
try this code, if I understood what you aimed for:
B4X:
Sub mnudistmult_Click
If intcounter > 0 Then 
   frmdistmult.show  'global variable set to 0 when app opens'
Else
   AddForm("frmDistMult","Distance Multiplier")
   frmdistmult.color=cSilver
   frmdistmult.show
   AddTextBox("frmdistmult","txtdm",40,20,50,20,"1")
   AddLabel("frmdistmult","lbldm",40,45,50,20,"Dist Mult")
   lbldm.color=cSilver
   AddButton("frmdistmult","btnDM",125,20,50,20,"OK")
   AddButton("frmdistmult","btnDMMenu",125,50,50,20,"Menu")
   AddLabel("frmdistmult","lbldminfo",40,80,150,40,"")
   lbldminfo.color=cSilver
   intcounter=intcounter+1  'increments counter' 
End If
End Sub

intcounter is actually used as a flag, and can be assigned as 1 instead of intcounter=intcounter+1

Edit: Klaus answered (a better one) while I was playing with the code
 
Last edited:

jschuchert

Active Member
Licensed User
Longtime User
Klaus and Derez,

Thank you for your solutions but neither one works. I stlll get the message that I am adding the form again. Must be something else not apparent right now but probably simple.

Jim
 

klaus

Expert
Licensed User
Longtime User
If you get the message again that means that you have already added the form somewhere else.
Could you send your whole code, it would be much easier to help you instead reasoning just on a small part.
I am using this principle in most of my programs, not for adding a form but for adding controls, and it works. So I'm estonished that it doesn't work in your case.

You could also test it by setting a breakpoint in this line
B4X:
If intcounter = 0 Then
and execute the program step by step, with the F8 key, to see what happens.

Best regards.
 
Last edited:

jschuchert

Active Member
Licensed User
Longtime User
Klaus,

I found the problem. 'intcounter' was being set to 0 again during the only routine I used to test the results. So when I selected that menu item again, it saw '0' and tried to add the form again. Both of your solutions worked fine after I changed the variable name and I believe mine would have, too. That's one problem with globals if you are not careful. Sorry to have taken your time but thanks again.

JIm
 

klaus

Expert
Licensed User
Longtime User
Hi Jim,

Your original code wouldn't have worked because the line

B4X:
If intcounter > 0 Then frmdistmult.show

shows the form only if intcounter > 0 BUT the program goes always through the rest of the routine, also when intcounter > 0.

You need the structure
B4X:
[FONT=Courier New]If intcounter = 0 Then[/FONT]
[FONT=Courier New]' Initilize[/FONT]
[FONT=Courier New]End If[/FONT]
[FONT=Courier New]frmdistmult.show[/FONT]

In this case you initialize only when intcounter = 0, but the form is always shown.

Best regards.
 

jschuchert

Active Member
Licensed User
Longtime User
You are correct, as usual, Klaus. I saw that later and used another iteration (not posted) that sent the program sequence to the end of the code to show the form with a (ugh...Booo!...'goto' statement) but I like yours much better and have happily incorporated it into other menu stuff. This forum is so awsome.

Jim
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…