Older Version
Newer Version
RodBird
Nov 25, 2015
Managing Multiple Windows by harmonv -- June 2007 This article and demo programs will help you handle multiple windows flawlessly. There are only a few things to remember, but they are important. (1) Don't try to open a window that is already open. (2) Don't try to close a window that is already closed. (3) As your program is quitting, close all open windows. We are going to show two methods. The first one uses a dedicated variable to remember the condition of each window. Think of it as a flag. When the flag is up, the window is open, and when it is down the window is closed. This method is good when dealing with a limited number of windows. [[code format="vbnet"]] ' Managing Multiple Windows ' using dedicated variables nomainwin win1Open = 0 ' windows closed win2Open = 0 ' Let's open our first window UpperLeftX = 100 UpperLeftY = 50 Open "Window One" For graphics As #win1 #win1, "trapclose [exitprogram]" win1Open = 1 ' Let's open our second window UpperLeftX = 450 Open "Window Two" For graphics As #win2 #win2, "trapclose [exitprogram]" win2Open = 1 #win2, "place 30 60 ; down" #win2, "\Click Red Close button to exit." #win2, "flush" Wait [exitprogram] If win1Open=1 Then Close #win1 If win2Open=1 Then Close #win2 END [[code]] The second method we will look at is very similar to the previous one but uses an array rather than separate variables. This is better for keeping track of a larger number of windows. [[code format="vbnet"]] ' Managing Multiple Windows ' using a array ' Avoid using REDIM on this array because it ' resets all array elements to 0. Dim winOpen(10) ' our array of flags nomainwin ' Let's open our first window UpperLeftX = 100 UpperLeftY = 50 Open "Window One" For graphics As #win1 #win1, "trapclose [exitprogram]" winOpen(1) = 1 ' Let's open our second window UpperLeftX = 450 Open "Window Two" For graphics As #win2 #win2, "trapclose [exitprogram]" winOpen(2) = 1 #win2, "place 30 60 ; down" #win2, "\Click Red Close button to exit." #win2, "flush" Wait [exitprogram] If winOpen(1)=1 Then Close #win1 If winOpen(2)=1 Then Close #win2 END [[code]] Events can be handled by subs as well as by [branchlabels]. In this final example, we have a window that contains a button. This button branches to the Sub sendTo rather than a branch label [sendTo]. When the button is clicked we open a window that has us choose where our program's output is to be sent or stored. If the window is already open we bring it to the top with an API call. (Thanks Alyce) [[code format="vbnet"]] ' Managing Multiple Windows ' a window demo with a button. nomainwin ' win2 handle is used in Subs, so needs to be global. Global reporTo$, win2 ' Avoid using REDIM on this array because it ' resets all array elements to 0. Dim winOpen(10) ' our array of flags ' Let's open our first window UpperLeftX = 100 UpperLeftY = 50 Button #win1.btn, "Send Report To", sendTo, UL, 100, 250, 120, 25 Open "Main Window" For window As #win1 #win1, "trapclose [exitprogram]" winOpen(1) = 1 Wait [exitprogram] If winOpen(1)=1 Then Close #win1 If winOpen(2)=1 Then Close #win2 END ' If window 2 closed, open it else bring it to front. Sub sendTo handle$ If winOpen(2)=0 Then UpperLeftX = 450 UpperLeftY = 100 wide = 70 : high = 25 Groupbox #win2.gb, "Select:", 20, 20, 100, 130 Radiobutton #win2.rb1, "File", set, nil, 30, 40, wide, high Radiobutton #win2.rb2, "Printer", set, nil, 30, 70, wide, high Radiobutton #win2.rb3, "Screen", set, nil, 30,100, wide, high statictext #win2.st, " ", 20, 160, 250, 25 Open "Send Report to:" For window As #win2 #win2, "trapclose win2close" winOpen(2) = 1 #win2, "font Arial 10" Else hWindow = hwnd(#win2) CallDLL #user32, "BringWindowToTop",_ hWindow as uLong, _ result AsBooleanlong End if End Sub ' Reset win2 radiobutton Sub Sub nil handle$ Wait End Sub ' Win2 radiobutton handler Sub Sub set btnfrom$ If btnfrom$="#win2.rb1" Then reporTo$="File" If btnfrom$="#win2.rb2" Then reporTo$="Printer" If btnfrom$="#win2.rb3" Then reporTo$="Screen" #win2.st, "Report will be sent to "; reporTo$ End Sub ' close win2 Sub Sub win2close handle$ If winOpen(2)=1 Then Close #win2 winOpen(2) = 0 End If End Sub [[code]] Also, Carl has shared his own method of managing multiple windows. It's called multiwin.bas and comes with LB.