Page 1 of 2
Newbie stumper
Posted: Tue Nov 29, 2011 1:19 am
by kpeters58
Hi all,
this one stumps me (my second day with PB):
When I have the window below open and open another window from the same application, the container gadget with id #20 disappears into nirwana, even if the two windows do not overlap (Windows 7, if that's of any interest)
As always,
thanks for any pointers
Kai
Code: Select all
Procedure OpenWindow_Window_1()
If OpenWindow(#Form_About, 488, 274, 450, 300, "About", #PB_Window_ScreenCentered|#PB_Window_TitleBar, WindowID(#Form_Main))
Frame3DGadget(#PB_Any, 10, 10, 430, 250, " Version Information ")
SetGadgetFont(TextGadget(#PB_Any, 60, 50, 200, 24, VersionInfo\ProductName), LoadFont(1, "Arial", 14, #PB_Font_Bold))
TextGadget (#PB_Any, 60, 80, 200, 24, VersionInfo\ProductInfo)
; >>> shadow first
ContainerGadget(10, 50, 120, 370, 120)
CloseGadgetList()
SetGadgetColor(10, #PB_Gadget_BackColor, RGB(60, 60, 60))
; >>> white rect next overlapping the shadow rectangle
ContainerGadget(20, 45, 115, 370, 120, #PB_Container_Flat)
TextGadget (22, 45, 20, 200, 24, VersionInfo\CompanyName)
TextGadget (24, 45, 50, 200, 24, VersionInfo\CopyRight)
TextGadget (26, 45, 80, 200, 24, VersionInfo\CompanyURL)
CloseGadgetList()
For i = 20 To 26 Step 2
SetGadgetColor(i, #PB_Gadget_BackColor, RGB(255, 255, 255))
Next
ButtonGadget(#Form_About_Button, 390, 270, 50, 25, "Close")
EndIf
EndProcedure
Re: Newbie stumper
Posted: Tue Nov 29, 2011 1:26 am
by citystate
at a guess, #Form_About_Button is equal to 20
it's good practice to either use all hard coded values, or all global constants when coding gadgets
Re: Newbie stumper
Posted: Tue Nov 29, 2011 1:31 am
by kpeters58
Hmm, looks like I have to keep my IDs unique - both windows had a container gadget with the same ID....
Kai
Re: Newbie stumper
Posted: Tue Nov 29, 2011 1:41 am
by em_uk
I would like to see a little more code, or a working example with the problem.
I did a little bit of code changing and easily got 3 windows to open, but without seeing the other parts of the code I'm not sure what will be going on.
Re: Newbie stumper
Posted: Tue Nov 29, 2011 1:45 am
by netmaestro
It's best to pick one approach to numbering your gadgets and stick to it. Here you're using constants for some gadgets and hardcoded numbers for others, setting yourself up for a conflict. For projects that aren't just hacking around, ones that will need maintained in future, I like to make a gadget enumeration block and put a constant for every gadget in the project in the block. With this approach there is no possibility of a conflict. Other approaches are fine too, such as #PB_Any or hardcoded numbers, but it's important not to mix them if you want a smooth, trouble-free development process without unnecessary headaches.
Re: Newbie stumper
Posted: Tue Nov 29, 2011 3:04 am
by IdeasVacuum
I like to make a gadget enumeration block and put a constant for every gadget in the project in the block. With this approach there is no possibility of a conflict.
This is always the safest way to do it, for everything except an app with a huge amount of gadgets (in which case it is a matter of hard coding them/testing them via loops).
Re: Newbie stumper
Posted: Tue Nov 29, 2011 3:10 am
by netmaestro
Yes, and I also said #PB_Any is fine too. The point is just pick one approach and stick to it.
Re: Newbie stumper
Posted: Tue Nov 29, 2011 3:50 am
by IdeasVacuum
...I know #PB_Any is very convenient and works well, but in anything other than simple source, it's easy to get confused about what happens where and to what.
Re: Newbie stumper
Posted: Tue Nov 29, 2011 4:00 am
by MachineCode
IdeasVacuum wrote:...I know #PB_Any is very convenient and works well, but in anything other than simple source, it's easy to get confused about what happens where and to what.
Say what? I disagree. #PB_Any makes large sources easier to follow. It depends on how you code, I guess.
Re: Newbie stumper
Posted: Tue Nov 29, 2011 4:11 am
by IdeasVacuum
...well, if you have 25 "#PB_Any String Gadgets" I don't see how that is easy to understand in your code in terms of each gadgets purpose? If you use enumerations on the other hand, you will have a much better idea of purpose with labels like #GivenName #FamilyName #DateOfBirth etc. I try to get the best of both worlds by having more than one enumeration block and defining the start number and step. That way, the code is easy to read overall, but I can still process gadgets sequentially in a loop - for example, grabbing the text from those String Gadgets and saving to file, where the loop simply increments the gadget id number.
Re: Newbie stumper
Posted: Tue Nov 29, 2011 4:18 am
by MachineCode
IdeasVacuum wrote:if you have 25 "#PB_Any String Gadgets" I don't see how that is easy to understand in your code in terms of each gadgets purpose?
It's easy! Do what I do. No enumerations required!
Code: Select all
OpenWindow(0,100,100,640,480,"test")
Global username=ButtonGadget(#PB_Any,10,10,200,25,"Username")
Global password=ButtonGadget(#PB_Any,10,40,200,25,"Password")
Global website=ButtonGadget(#PB_Any,10,70,200,25,"Website")
Repeat
event=WaitWindowEvent()
If event=#PB_Event_Gadget
Select EventGadget()
Case username : Debug "username"
Case password : Debug "password"
Case website : Debug "website"
EndSelect
EndIf
Until event=#PB_Event_CloseWindow
IdeasVacuum wrote:I can still process gadgets sequentially in a loop - for example, grabbing the text from those String Gadgets and saving to file, where the loop simply increments the gadget id number.
You can do this with my example like so:
Code: Select all
For g=username To website
text$=GetGadgetText(g)
Next
Re: Newbie stumper
Posted: Tue Nov 29, 2011 6:07 am
by citystate
kpeters58 wrote:Hmm, looks like I have to keep my IDs unique - both windows had a container gadget with the same ID....
Kai
yup, that would do it...
for methods on how to minimise this happening again, I'd suggest one of them posted above - but as netmaestro says, be consistent - it makes your job much easier in the long run
Re: Newbie stumper
Posted: Tue Nov 29, 2011 11:15 am
by Trond
MachineCode wrote:
You can do this with my example like so:
Code: Select all
For g=username To website
text$=GetGadgetText(g)
Next
No, that's wrong. It works by chance only. The gadget numbers allocated by #PB_Any are never one number apart and not guaranteed to be sequential. You have to put the numbers in an array and loop over it.
When running your example:
username = 31905408
password = 31905504
website = 31905600
Re: Newbie stumper
Posted: Tue Nov 29, 2011 11:17 am
by Trond
Hmm, looks like I have to keep my IDs unique
ID's must definetely be unique, that's the purpose of ID's: To identify an object uniquely.
Edit: We are actually talking about gadget numbers here (sometimes referred to as #Gadget), not ID's. An ID is what you get when passing a gadget number to the function GadgetID(). The ID is the number used by the operating system to identify the gadget.
Re: Newbie stumper
Posted: Tue Nov 29, 2011 12:16 pm
by MachineCode
Trond wrote:It works by chance only
You're right. I only tested the output of the first gadget, saw that it worked
for that single test, so I posted a For/Next loop that I assumed would work for all. Sorry peeps!