Opening multiple windows

Discussions relevant to the differences between LBB and LB4 which could affect code compatibility
flotul
Posts: 12
Joined: Fri Apr 06, 2018 7:06 am

Opening multiple windows

Post by flotul »

Hi All,

I have taken a "bad habit" by always using LBB to make my programs ;)

But when it comes to a problem I can't solve by myself and need help that might come from the LB forum, I have to go "back" to LB.

I have made a program that will use multiple windows, 3 in all but for now, let's do it with 2.

I can make it work in LBB but not in LB....and I don't know how to do it in LB :(

Here is my extremely simplified code working in LBB. For now, it doesn't do anything else but opening the FIRST or the SECOND window at will.


NOMAINWIN

[FIRST]
WindowWidth = 400
WindowHeight = 200
UpperLeftX = int((DisplayWidth-WindowWidth)/2)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)

MENU #FIRST,"Windows","First",[FIRST],"Second",[SECOND]
MENU #FIRST,"Exit","Exit",[QUIT.ALL]

STATICTEXT #FIRST, "FIRST WINDOW", 20, 10,200, 20

CLOSE #SECOND

OPEN "FIRST WINDOW" FOR window_nf AS #FIRST
#FIRST "TRAPCLOSE [QUIT.ALL]"
WAIT



[SECOND]
WindowWidth = 400
WindowHeight = 200
UpperLeftX = int((DisplayWidth-WindowWidth)/2)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)

MENU #SECOND,"Windows","First",[FIRST],"Second",[SECOND]
MENU #SECOND,"Exit","Exit",[QUIT.ALL]

STATICTEXT #SECOND, "SECOND WINDOW", 20, 10,200, 20

CLOSE #FIRST

OPEN "SECOND WINDOW" FOR window_nf AS #SECOND
#SECOND "TRAPCLOSE [QUIT.ALL]"
WAIT


[QUIT.ALL]
CLOSE #SECOND
CLOSE #FIRST
END


This is the error message I get when using the same code in LB4.5.1 editor.
LB_error_msg.png
LB_error_msg.png (2.31 KiB) Viewed 5605 times
Can I make this work in LB4?
guest
Site Admin
Posts: 192
Joined: Tue Apr 03, 2018 1:34 pm

Re: Opening multiple windows

Post by guest »

flotul wrote: Tue Mar 01, 2022 2:33 pm Can I make this work in LB4?
LB4 doesn't like you 'closing' a window that isn't open, so you will need to introduce a couple of Boolean variables (I have called them FirstOpen and SecondOpen) which keep track of which window is open. That way you can avoid trying to close a window that isn't open.

There may be a more elegant solution, but this seems to work:

Code: Select all

NOMAINWIN

[FIRST]
IF FirstOpen = 0 THEN

  WindowWidth = 400
  WindowHeight = 200
  UpperLeftX = int((DisplayWidth-WindowWidth)/2)
  UpperLeftY = int((DisplayHeight-WindowHeight)/2)

  MENU #FIRST,"Windows","First",[FIRST],"Second",[SECOND]
  MENU #FIRST,"Exit","Exit",[QUIT.ALL]

  STATICTEXT #FIRST, "FIRST WINDOW", 20, 10,200, 20

  IF SecondOpen THEN CLOSE #SECOND : SecondOpen = 0

  OPEN "FIRST WINDOW" FOR window_nf AS #FIRST
  FirstOpen = 1
  #FIRST "TRAPCLOSE [QUIT.ALL]"

END IF
WAIT



[SECOND]
IF SecondOpen=0 THEN

  WindowWidth = 400
  WindowHeight = 200
  UpperLeftX = int((DisplayWidth-WindowWidth)/2)
  UpperLeftY = int((DisplayHeight-WindowHeight)/2)

  MENU #SECOND,"Windows","First",[FIRST],"Second",[SECOND]
  MENU #SECOND,"Exit","Exit",[QUIT.ALL]

  STATICTEXT #SECOND, "SECOND WINDOW", 20, 10,200, 20

  IF FirstOpen THEN CLOSE #FIRST : FirstOpen = 0

  OPEN "SECOND WINDOW" FOR window_nf AS #SECOND
  SecondOpen = 1
  #SECOND "TRAPCLOSE [QUIT.ALL]"

END IF
WAIT


[QUIT.ALL]
IF SecondOpen THEN CLOSE #SECOND
IF FirstOpen THEN CLOSE #FIRST
END
flotul
Posts: 12
Joined: Fri Apr 06, 2018 7:06 am

Re: Opening multiple windows

Post by flotul »

Thanks.

I read an article from Gordon SWEET (http://www.libertybasicuniversity.com/l ... /multi.htm) and didn't understood why he used his "win1" "win2" etc "trackers". Now, you explaining it does make sense to me.

I'll give it a try asap (tomorrow morning).

Thanks again for the quick reply ;)