locked
How to Close (Stop,Quit,KillProcess) a Graphics window ? RRS feed

  • Question

  • Hello & Thanks ;

    In code below (an extremely stripped version of Nonki's code) ,

    I want to Close out (Exit from) a 'browser' window . I tried 'Controls.Remove(browser' , but it doesn't stop a video from playing . How can I do that ?

    To test it out 

    1) code

    2) click on google link

    3 on google page click on YouTube link

    4) click on any video with sound 

    5) click on browser [Go >>] button

    6) you can still hear audio playing 

    Thanks for your Help...

    ' LDControls-Btn-TxtBox-Browser-VM   One Window One Button One TextBox
    
    browserWidth = 1000
    browserHeight = 1000
    buttonWidth = 100
    buttonHeight = 30
    textBoxWidth = 300
    textBoxHeight = 300
    addressBarText = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    
    Initialise()
    While ("True")
      Program.Delay(20)
    EndWhile
    
    Sub Initialise
      button1 = Controls.AddButton("GO >>", 50, 4)
      Controls.SetSize(button1,buttonWidth , buttonHeight)
      Controls.ButtonClicked = OnButtonClicked
      
      addressBar = Controls.AddTextBox(200,4)
      Controls.SetTextBoxText(addressBar , addressBarText)
      Controls.SetSize(addressBar , 400 ,30)  
      
      url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
      browser = LDControls.AddBrowser(browserWidth , browserWidth, url)
      Controls.Move(browser, 0, 50)
      Controls.SetSize(browser , 1000 ,500)  
      GraphicsWindow.Title="Safe Browser"
      GraphicsWindow.CanResize="True"
      GraphicsWindow.Width = 1000
      GraphicsWindow.Height = 520
    EndSub ' Initialize
    
    Sub OnButtonClicked
      '  GraphicsWindow.ShowMessage("Sub OnButtonClicked", "VM")
      Controls.Remove(browser)
      url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
      browser = LDControls.AddBrowser(browserWidth , browserWidth, url)
      
    EndSub ' OnButtonClicked
      



    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 9, 2020 2:03 PM

Answers

  • That is logical. The line   browser = LDControls.AddBrowser(800,600, url) 

    defines a variable called "browser". If you delete the line , the following command contains a non allocated variable:

    LDControls.BrowserSetURL(browser, url)

    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Monday, August 10, 2020 12:05 PM typo
    • Marked as answer by vmars316 Monday, August 10, 2020 2:31 PM
    Monday, August 10, 2020 11:20 AM
    Answerer

All replies

  • Oops ,  '1) code'  should say  '1) Run code' .

    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 9, 2020 2:05 PM
  • Change url stops any playing sound, for example.

    LDControls.BrowserSetURL(browser,"https://duckduckgo.com/")

    Remember the previous post where I suggested you just change urls not recreate new browser controls.

    Sunday, August 9, 2020 4:05 PM
  • Hi litdev ;

    Yes , I remember the incident but couldn't find again where it was mentioned . It confused me .

    Because it doesn't make sense to use both of these each time ?

      browser = LDControls.AddBrowser(browserWidth , browserWidth , url)
      LDControls.BrowserSetURL(browser, url) 

    If just use this alone  LDControls.BrowserSetURL(browser, url) 

    compiler doesn't know who 'browser' is .

    Is there another way to identify 'browser' . 

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 9, 2020 5:31 PM
  • With this subroutine the music stops:

    Sub OnButtonClicked
      '  GraphicsWindow.ShowMessage("Sub OnButtonClicked", "VM")
      'Controls.Remove(browser)
      url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
      LDControls.BrowserSetURL(browser,url)
      'browser = LDControls.AddBrowser(browserWidth , browserWidth, url)
      
    EndSub 


    Sunday, August 9, 2020 7:43 PM
  • Thanks ;

    Sorry to say but this will not compile :

    Error: 39.28: The variable 'browser' is used but its value is not assigned. 

    If just use this alone  LDControls.BrowserSetURL(browser, url) 
    compiler doesn't know who 'browser' is .
    Is there another way to identify 'browser' .
    ' LDControls-Btn-TxtBox-Browser-FORUM   One Window One Button One TextBox
    
    browserWidth = 1000
    browserHeight = 1000
    buttonWidth = 100
    buttonHeight = 30
    textBoxWidth = 300
    textBoxHeight = 300
    addressBarText = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    
    Initialise()
    While ("True")
      Program.Delay(20)
    EndWhile
    
    Sub Initialise
      button1 = Controls.AddButton("GO >>", 50, 4)
      Controls.SetSize(button1,buttonWidth , buttonHeight)
      Controls.ButtonClicked = OnButtonClicked
      
      addressBar = Controls.AddTextBox(200,4)
      Controls.SetTextBoxText(addressBar , addressBarText)
      Controls.SetSize(addressBar , 400 ,30)  
      
    '  Controls.Move(browser, 0, 50)
    '  Controls.SetSize(browser , 1000 ,500)  
      GraphicsWindow.Title="Safe Browser"
      GraphicsWindow.CanResize="True"
      GraphicsWindow.Width = 1000
      GraphicsWindow.Height = 520
    EndSub ' Initialize
    
    Sub OnButtonClicked
      '  GraphicsWindow.ShowMessage("Sub OnButtonClicked", "FORUM")
      
      url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
      LDControls.BrowserSetURL(browser, url)
      
    EndSub ' OnButtonClicked

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 9, 2020 11:28 PM
  • Here's another example .

    Same deal , take away the 

        browser = LDControls.AddBrowser(800,600, url) 

    and it won't run . 

    Thanks for your Help...

    ' One Window One Button
    buttonWidth = 50
    buttonHeight = 30
    url = "http://vmars.us"
    
    Initialise()
    While ("True")
      Program.Delay(20)
    EndWhile
    
    
    Sub Initialise
    button1 = Controls.AddButton("BACK", 100, 2)
    Controls.SetSize(button1,buttonWidth , buttonHeight)
    'Controls.ButtonClicked = OnButtonClicked
    
        url = "https://social.msdn.microsoft.com/Forums/en-US/home?forum=smallbasic"
        browser = LDControls.AddBrowser(800,600, url)
        Controls.Move(browser, 0, 50)
        GraphicsWindow.ShowMessage("browser = LDControls.AddBrowser(800,600, url)" , "WAIT FOR IT: LDControls.AddBrowser" )
    
        GraphicsWindow.ShowMessage("LDControls.BrowserSetURL(browser, url)" , "NEXT STOP = LDControls.BrowserSetURL" )
        url = "http://vmars.us"
        LDControls.BrowserSetURL(browser, url)
      EndSub ' Initialize
    
      


    "All things in moderation, except for love and forgiveness."...vm

    Monday, August 10, 2020 1:01 AM
  • That is logical. The line   browser = LDControls.AddBrowser(800,600, url) 

    defines a variable called "browser". If you delete the line , the following command contains a non allocated variable:

    LDControls.BrowserSetURL(browser, url)

    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Monday, August 10, 2020 12:05 PM typo
    • Marked as answer by vmars316 Monday, August 10, 2020 2:31 PM
    Monday, August 10, 2020 11:20 AM
    Answerer
  • Thanks WhTurner33

    Guess I misinterpreted what LitDev said , I understood his comments to mean ,

    Don't use 'browser = LDControls.AddBrowser(800,600, url) ' ,

    Use 'LDControls.BrowserSetURL(browser, url)' insread . 

    So I was asking , If I don't use 

    'browser = LDControls.AddBrowser(800,600, url) ' ,

    how else can I identify 'browser' to the compiler .

    Thanks for clarifying things .


    "All things in moderation, except for love and forgiveness."...vm

    Monday, August 10, 2020 2:30 PM
  • If I put the line from litdev in your program   

    ' LDControls-Btn-TxtBox-Browser-FORUM One Window One Button One TextBox

    ''And change the sub with litdev's line to:

        

    Sub OnButtonClicked
      '  GraphicsWindow.ShowMessage("Sub OnButtonClicked", "VM")
      LDControls.BrowserSetURL(browser,"https://duckduckgo.com/")
      ''Controls.Remove(browser)
      ''url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
      ''browser = LDControls.AddBrowser(browserWidth , browserWidth, url)

    EndSub ' OnButtonClicked

    ''It works as you wish (kill the music)


    Jan [ WhTurner ] The Netherlands


    Monday, August 10, 2020 3:30 PM
    Answerer
  • On the issue of not creating a new browser control when you want to change url.

    I recommend:

    1] Create all the controls (and shapes) you want in the program once at the start, before the game loop.

    2] Use Shapes.HideShape and Shapes.ShowShape to display them or not during the logic of the program (works with controls as well as shapes).

    3] Interact with various control commands to modify the properties, position, contents, event etc as required rather than ceating new controls.

    4] If you must (usually there is a better way as above) add a new shape or control, then make sure you first  delete the old one (Shapes.Remove)  If not you will have loads of orphaned shapes at best slowing you program and at worst causing all sorts of other issues.

    In the case of the browser, its a bit like creating a whole new browser each time you change the url.  But this applies to other shapes and controls, especial game sprites that appear and disappear for example.

    Monday, August 10, 2020 7:18 PM
  • Thanks ;

    I think what you are saying is that each 

    LDControls.BrowserSetURL(browser, url)

    creates a new window . Stacking up windows and eventually the system will run out of space .

    Is there a way to keep track of all GraphicWindows ,

    release these resources , maybe keep the original  ,  

    browser = LDControls.AddBrowser(browserWidth , browserHeight , url) 

    but release the rest ?

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Tuesday, August 11, 2020 10:03 PM
  • Here is the code I am using , no shapes , just 'browser' . 

    If I activate this code , then I get error:

    LDControls.BrowserSetURL : Shape not found : Control1

    Thanks for your Help...

    ' LDControls-Btn-TxtBox-Browser-VM.sb    One Window One Button One TextBox
    ' https://social.technet.microsoft.com/wiki/contents/articles/23583.small-basic-reference-documentation.aspx
    ' https://social.msdn.microsoft.com/Forums/en-US/home?forum=smallbasic
    ' to end program use  Program.End()
    
    firstTimeButtonClicked = 1
    htmlText = "Nuttin' in here yet !"
    browserWidth = 1000
    browserHeight = 500
    backButtonWidth = 75
    backButtonHeight = 30
    homeButtonWidth = 75
    homeButtonHeight = 30
    goButtonWidth = 75
    goButtonHeight = 30
    textBoxWidth = 300
    textBoxHeight = 300
    exitButtonWidth = 75
    exitButtonHeight = 30
    lastSiteVisited = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    addressBarText = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    browser = LDControls.AddBrowser(browserWidth , browserHeight , url) 
    Controls.SetSize(browser , 1000 ,500)  
    Controls.Move(browser, 0, 50)
    
    
    Initialise()
    While ("True")
      Program.Delay(20)
    EndWhile
    
    Sub Initialise
      backButton = Controls.AddButton("<<-Back", 95, 8)
      Controls.SetSize(backButton,backButtonWidth , backButtonHeight)
      
      homeButton = Controls.AddButton("Home", 200, 8)
      Controls.SetSize(homeButton,homeButtonWidth , homeButtonHeight)
      
      goButton = Controls.AddButton("GO->>", 305, 8)
      Controls.SetSize(goButton,goButtonWidth , goButtonHeight)
      
      addressBar = Controls.AddTextBox(405,8)
      Controls.SetTextBoxText(addressBar , addressBarText)
      Controls.SetSize(addressBar , 200 ,30)  '  remove after testing 
      
      htmlTextBox = Controls.AddMultiLineTextBox(630,4)
      Controls.ButtonClicked = OnButtonClicked
      
      exitButton = Controls.AddButton("Close", 855, 8)
      Controls.SetSize(exitButton,exitButtonWidth , exitButtonHeight)
      
      GraphicsWindow.Title="SafeBrowser"
      GraphicsWindow.CanResize="True"
      GraphicsWindow.Width = 1000
      GraphicsWindow.Height = 520
    EndSub ' Initialize
    
    
    Sub OnButtonClicked
      If firstTimeButtonClicked > 2 Then
             Shapes.Remove(browser)
      EndIf  
      
      If Controls.LastClickedButton = backButton Then
        url = lastSiteVisited
        lastSiteVisited = Controls.GetTextBoxText(addressBar)
        If url <> "" Then 
          LDControls.BrowserSetURL(browser, url)
        Else 
          GraphicsWindow.ShowMessage(url , "url = ")
        EndIf 
      EndIf '  goButton
    
      If Controls.LastClickedButton = homeButton Then
        url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
        If url <> "" Then 
          LDControls.BrowserSetURL(browser, url)
        Else 
          GraphicsWindow.ShowMessage(url , "url = ")
        EndIf 
      EndIf '  homeButton
    
    If Controls.LastClickedButton = goButton Then
        url = Controls.GetTextBoxText(addressBar)
        If url <> "" Then 
          LDControls.BrowserSetURL(browser, url)
        Else 
          GraphicsWindow.ShowMessage(url , "AddressBar url = ")
        EndIf 
        lastSiteVisited = url 
      EndIf ' goButton
      
      If Controls.LastClickedButton = exitButton Then
        Program.End()
       
    EndIf '  exitButton
    
    htmlText = Network.GetWebPageContents(url) 
      Controls.SetTextBoxText(htmlTextBox,htmlText)
      firstTimeButtonClicked = firstTimeButtonClicked  + 1
      
    EndSub ' OnButtonClicked
      


    "All things in moderation, except for love and forgiveness."...vm

    Wednesday, August 12, 2020 12:56 AM
  • OK, I think at this stage this is one for you to try to debug a bit more!

    This one is about program logic and its important to learn how to debug your code.

    Think of it like a logic puzzle and try to look for possible causes (get a hypothesis then think of a way to test)

    Some principals:

    1] Simplify till the bug goes away

    2] Add logging to TextWindow or a file

    3] Improve the structure logic (subroutines, variables and arrays, game loop etc)

    4] Use the debugging tool in SBPrime


    • Edited by litdev Wednesday, August 12, 2020 8:23 AM
    Wednesday, August 12, 2020 8:19 AM
  • Thanks ;

    Ok , I'm on it...

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Wednesday, August 12, 2020 1:42 PM
  • Why do you think its a bug ? 

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Wednesday, August 12, 2020 11:53 PM
  • Hello ;

    Unfortunately the Debug .pdf doesnt tell me how to get the redDot to show on the line I want o debug .

    ie. its not working for me . ?

    Help

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Thursday, August 13, 2020 12:42 AM
  • You can click on the left of the line you want, or use the button "Toggle Breakpoints"  (both in the image)

    ADDED: on the Debug tab is on the top at the right side a "Debug Help" of several pages to study


    Jan [ WhTurner ] The Netherlands


    Thursday, August 13, 2020 9:58 AM
    Answerer
  • Hello:

    Hmm...
    Wnen the Back Button i cicked , here is the program flow:

    60 Sub OnButtonClicked
    63 Shapes.Remove(browser)
    64 GraphicsWindow.ShowMessage(url , "After Shapes.Remove(browser) line")
    68   If Controls.LastClickedButton = backButton Then
    69     url = lastSiteVisited
    70     lastSiteVisited = Controls.GetTextBoxText(addressBar)
    71     If url <> "" Then 
    72       LDControls.BrowserSetURL(browser, url)
    Then up pops the console window error msg:
    LDControls.BrowserSetURL : Shape not found : Control1
    31 EndWhile
    30   Program.Delay(20)
    Then keeps looping between 31 & 30  ad nauseam ,
    until I close console window . 

    What have I learned :
    Don't use 'Shapes.Remove(browser)' command .

    So I revert to my question above: 
    "Is there a way to keep track of all GraphicWindows ,
    release these resources , maybe keep the original  ,  
    browser = LDControls.AddBrowser(browserWidth , browserHeight , url) 
    but release the rest ? "

    Thanks
    1  ' LDControls-Btn-TxtBox-Browser-VM.sb    One Window One Button One TextBox
    2  ' https://social.technet.microsoft.com/wiki/contents/articles/23583.small-basic-reference-documentation.aspx
    3  ' https://social.msdn.microsoft.com/Forums/en-US/home?forum=smallbasic
    4  ' to end program use  Program.End()
    5  
    6  firstTimeButtonClicked = 1
    7  htmlText = "Nuttin' in here yet !"
    8  browserWidth = 1000
    9  browserHeight = 500
    10 backButtonWidth = 75
    11 backButtonHeight = 30
    12 homeButtonWidth = 75
    13 homeButtonHeight = 30
    14 goButtonWidth = 75
    15 goButtonHeight = 30
    16 textBoxWidth = 300
    17 textBoxHeight = 300
    18 exitButtonWidth = 75
    19 exitButtonHeight = 30
    20 lastSiteVisited = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    21 addressBarText = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    22 url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    23 browser = LDControls.AddBrowser(browserWidth , browserHeight , url) 
    24 Controls.SetSize(browser , 1000 ,500)  
    25 Controls.Move(browser, 0, 50)
    26 
    27 
    28 Initialise()
    29 While ("True")
    30   Program.Delay(20)
    31 EndWhile
    32 
    33 Sub Initialise
    34   backButton = Controls.AddButton("<<-Back", 95, 8)
    35   Controls.SetSize(backButton,backButtonWidth , backButtonHeight)
    36   
    37   homeButton = Controls.AddButton("Home", 200, 8)
    38   Controls.SetSize(homeButton,homeButtonWidth , homeButtonHeight)
    39   
    40   goButton = Controls.AddButton("GO->>", 305, 8)
    41   Controls.SetSize(goButton,goButtonWidth , goButtonHeight)
    42   
    43   addressBar = Controls.AddTextBox(405,8)
    44   Controls.SetTextBoxText(addressBar , addressBarText)
    45   Controls.SetSize(addressBar , 200 ,30)  '  remove after testing 
    46   
    47   htmlTextBox = Controls.AddMultiLineTextBox(630,4)
    48   Controls.ButtonClicked = OnButtonClicked
    49   
    50   exitButton = Controls.AddButton("Close", 855, 8)
    51   Controls.SetSize(exitButton,exitButtonWidth , exitButtonHeight)
    52   
    53   GraphicsWindow.Title="SafeBrowser"
    54   GraphicsWindow.CanResize="True"
    55   GraphicsWindow.Width = 1000
    56   GraphicsWindow.Height = 520
    57 EndSub ' Initialize
    58 
    59 
    60 Sub OnButtonClicked
    61 '  If firstTimeButtonClicked > 2 Then
    62   
    63       Shapes.Remove(browser)
    64           GraphicsWindow.ShowMessage(url , "After Shapes.Remove(browser) line")
    65   
    66   '  EndIf  
    67   
    68   If Controls.LastClickedButton = backButton Then
    69     url = lastSiteVisited
    70     lastSiteVisited = Controls.GetTextBoxText(addressBar)
    71     If url <> "" Then 
    72       LDControls.BrowserSetURL(browser, url)
    73     Else 
    74       GraphicsWindow.ShowMessage(url , "url = ")
    75     EndIf 
    76   EndIf '  goButton
    77 
    78   If Controls.LastClickedButton = homeButton Then
    79     url = "http://vmars.us/SafeBrowser/SafeBrowserHome.html"
    80     If url <> "" Then 
    81       LDControls.BrowserSetURL(browser, url)
    82     Else 
    83       GraphicsWindow.ShowMessage(url , "url = ")
    84     EndIf 
    85   EndIf '  homeButton
    86 
    87 If Controls.LastClickedButton = goButton Then
    88     url = Controls.GetTextBoxText(addressBar)
    89     If url <> "" Then 
    90       LDControls.BrowserSetURL(browser, url)
    91     Else 
    92       GraphicsWindow.ShowMessage(url , "AddressBar url = ")
    93     EndIf 
    94     lastSiteVisited = url 
    95   EndIf ' goButton
    96   
    97   If Controls.LastClickedButton = exitButton Then
    98     Program.End()
    99    
    100EndIf '  exitButton
    101
    102htmlText = Network.GetWebPageContents(url) 
    103  Controls.SetTextBoxText(htmlTextBox,htmlText)
    104  firstTimeButtonClicked = firstTimeButtonClicked  + 1
    105  
    106EndSub ' OnButtonClicked
    107  

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Thursday, August 13, 2020 7:15 PM
  • First: If you copy your program here, do not include the linenumbers. Small Basic can't handle those.

    I am a little confused about your program. I think there is no need to remove the browserwindow. If I understand litdev you have to generate a browserwindow, and then you can change the contents of the window (overwriting the old contents) with the BrowserSetUrl command. In this way you have only one window. 

    Only if you create each time a new browser there will be multiple windows.


    Jan [ WhTurner ] The Netherlands

    Friday, August 14, 2020 2:51 PM
    Answerer
  • Thanks WhTurner33 

    " you can change the contents of the window (overwriting the old contents) with the BrowserSetUrl command. In this way you have only one window"

    That's what I needed to hear specifically . 

    Great news ! 


    "All things in moderation, except for love and forgiveness."...vm

    Friday, August 14, 2020 3:46 PM