locked
My program just keeps crashing when i click a textbox! HELP! RRS feed

  • Question

  • Hi to everybody. I'm new to programming and I want to test my knowledge by making my sister a simple math game. However, there's a problem that doesn't let me go further than the main screen.

    After the main screen, I want a screen to appear and ask for the user's name. If the name is "Diana" (my sister's name), it will go to easy difficulty of the game. If the name is not Diana (because it was typed by someone else in the house), then it will go to hard mode. I designed the background in Photoshop and i want the texbox to appear in top of it. this is the code I wrote for that part:

     

    GraphicsWindow.FontName = "ComicSans MS"
    GraphicsWindow.FontSize = 12
    namebox = Controls.AddMultiLineTextBox(20, 278)

     

    But when I click on the textbox so that i can type in it... It crashes! Why?

     

    EDIT: I have windows seven starter... if that helps.
    • Edited by norik434 Friday, May 20, 2011 11:27 PM
    Friday, May 20, 2011 10:56 PM

Answers

  • Ok, thanks for the code. 

    First I deleted the initial image loading (I don't have your images and they probably aren't the problem).

    I then deleted the welcome messages they look OK.

    I also got rid of the font setting, again we know this works by itself.

    We are left with:

    GraphicsWindow.MouseDown = OnMouseDown

    Sub OnMouseDown
      GraphicsWindow.MouseMove = name
    EndSub

    Sub name
      namebox = Controls.AddMultiLineTextBox(20, 278)
    EndSub

    This causes the crash!

    So what is the problem - the OnMouseDown subroutine is called on an event thread - a separate process started when the mouse is clicked - this process (or thread) is only active while this subroutine (OnMouseDown) is called - when this subroutine ends this thread is lost.  However during this short new thread you register a new event based on MouseMove.  When the OnMouseDown ends the MouseMove event (Sub name) is somehow left dangling (registered on a thread that no longer exists).  A bit complicated I know, but there its is.

    What can we do:

    1] Don't register one event within another - this is clear, but what can we do?

     The full code not starting a new event inside another.  Notice I didn't start a MouseMove event, just called the subroutine 'name' to create the textbox. 

    GraphicsWindow.Title = "Diana's Math! - By Diego Lamela"
    GraphicsWindow.Width = 640
    GraphicsWindow.Height = 480
    GraphicsWindow.DrawResizedImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\welcome-screen.jpg", 0, 0, 640, 480)
    GraphicsWindow.FontName = "Verdana"
    GraphicsWindow.FontBold = 1
    GraphicsWindow.FontSize = 30
    GraphicsWindow.DrawImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\boton-start.jpg", 200, 300)
    GraphicsWindow.MouseDown = OnMouseDown

    Sub OnMouseDown
     
      If(Clock.Hour < 12) Then
        GraphicsWindow.ShowMessage("Good morning!" , "Hola!")
      ElseIf (Clock.Hour < 20) Then
        GraphicsWindow.ShowMessage("Gafternoon!!", "Hola!")
      Else
        GraphicsWindow.ShowMessage("Good evening!", "Hola!")
      EndIf
      name() 'Just call this subroutine - an event cannot be started within another event
    EndSub

    Sub name
      GraphicsWindow.DrawResizedImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\name-screen.jpg", 0, 0, 640, 480)
      GraphicsWindow.FontName = "ComicSans MS"
      GraphicsWindow.FontSize = 12
      namebox = Controls.AddMultiLineTextBox(20, 278)
    EndSub

    Finally , notice this time I modified the welcome message to handle morning times (consider your original code with hour less than 12 - it does the Good Morning and the Good Evening!

      If(Clock.Hour < 12) Then
        GraphicsWindow.ShowMessage("Good morning!" , "Hola!")
      ElseIf (Clock.Hour < 20) Then
        GraphicsWindow.ShowMessage("Gafternoon!!", "Hola!")
      Else
        GraphicsWindow.ShowMessage("Good evening!", "Hola!")
      EndIf

    PS.

    The distinction between registering an event and calling a subroutine is that the event subroutine is called on a new temporary thread every time the event is triggered - in your case you wouldn't want to recreate the text box every time the move moved - perhaps 50 times per second as you move the mouse and for ever after when the mouse moves.

    • Marked as answer by norik434 Saturday, May 21, 2011 3:00 AM
    Saturday, May 21, 2011 12:12 AM

All replies

  • Hi, 

    When I try your 3 line code segment I can click into and type text into the textbox with no crash.

    Do you get the crash with just these three lines?

    If not please post a full sample that reproduces the crash for you.

    Try to simplify the code as much as possible removing unrelated code, while reproducing the crash in a segment that someone else can run - this will be easier to isolate/determine the problem and may help you find the problem yourself.  For example, is it dependent on the FontName or not - if not remove it from the test sample.

    This is a standard debugging technique (simplify and isolate). 

    The problem may be (1) a SmallBasic bug or (2) something not handled as a result of a coding error you made or (3) some kind of system specific issue (e.g font not found).

    Friday, May 20, 2011 11:26 PM
  • Interesting... When I type the same exact code by itself it works perfectly. But when n I use it in the program it just keeps crashing... What I'll do is I'm posting the whole thing to see if something's wrong with the code that causes this...

     

     

    GraphicsWindow.Title = "Diana's Math! - By Diego Lamela"
    GraphicsWindow.Width = 640
    GraphicsWindow.Height = 480
    GraphicsWindow.DrawResizedImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\welcome-screen.jpg", 0, 0, 640, 480)
    GraphicsWindow.FontName = "Verdana"
    GraphicsWindow.FontBold = 1
    GraphicsWindow.FontSize = 30
    GraphicsWindow.DrawImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\boton-start.jpg", 200, 300)
    GraphicsWindow.MouseDown = OnMouseDown
    
    Sub OnMouseDown
     
     If(Clock.Hour < 12) Then 
     GraphicsWindow.ShowMessage("Good morning!" , "Hola!")
     EndIf 
     
     If(Clock.Hour > 12 And Clock.Hour < 20) Then 
     GraphicsWindow.ShowMessage("Gafternoon!!", "Hola!")
     Else 
     GraphicsWindow.ShowMessage("Good evening!", "Hola!")
     EndIf 
     GraphicsWindow.MouseMove = name
    EndSub
    Sub name
    GraphicsWindow.DrawResizedImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\name-screen.jpg", 0, 0, 640, 480)
    GraphicsWindow.FontName = "ComicSans MS"
    GraphicsWindow.FontSize = 12
    namebox = Controls.AddMultiLineTextBox(20, 278)
     
    EndSub
    
    

     

     

    EDIT: I Polished the piece of code with the textbox so that it worked until the begining of the game and it works Great! but then when I paste it in my program it just crashes! What the heck is going on!?



    Friday, May 20, 2011 11:33 PM
  • Ok, thanks for the code. 

    First I deleted the initial image loading (I don't have your images and they probably aren't the problem).

    I then deleted the welcome messages they look OK.

    I also got rid of the font setting, again we know this works by itself.

    We are left with:

    GraphicsWindow.MouseDown = OnMouseDown

    Sub OnMouseDown
      GraphicsWindow.MouseMove = name
    EndSub

    Sub name
      namebox = Controls.AddMultiLineTextBox(20, 278)
    EndSub

    This causes the crash!

    So what is the problem - the OnMouseDown subroutine is called on an event thread - a separate process started when the mouse is clicked - this process (or thread) is only active while this subroutine (OnMouseDown) is called - when this subroutine ends this thread is lost.  However during this short new thread you register a new event based on MouseMove.  When the OnMouseDown ends the MouseMove event (Sub name) is somehow left dangling (registered on a thread that no longer exists).  A bit complicated I know, but there its is.

    What can we do:

    1] Don't register one event within another - this is clear, but what can we do?

     The full code not starting a new event inside another.  Notice I didn't start a MouseMove event, just called the subroutine 'name' to create the textbox. 

    GraphicsWindow.Title = "Diana's Math! - By Diego Lamela"
    GraphicsWindow.Width = 640
    GraphicsWindow.Height = 480
    GraphicsWindow.DrawResizedImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\welcome-screen.jpg", 0, 0, 640, 480)
    GraphicsWindow.FontName = "Verdana"
    GraphicsWindow.FontBold = 1
    GraphicsWindow.FontSize = 30
    GraphicsWindow.DrawImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\boton-start.jpg", 200, 300)
    GraphicsWindow.MouseDown = OnMouseDown

    Sub OnMouseDown
     
      If(Clock.Hour < 12) Then
        GraphicsWindow.ShowMessage("Good morning!" , "Hola!")
      ElseIf (Clock.Hour < 20) Then
        GraphicsWindow.ShowMessage("Gafternoon!!", "Hola!")
      Else
        GraphicsWindow.ShowMessage("Good evening!", "Hola!")
      EndIf
      name() 'Just call this subroutine - an event cannot be started within another event
    EndSub

    Sub name
      GraphicsWindow.DrawResizedImage("C:\Users\owner\Documents\Diego\Programacion!\Diana's_Math\name-screen.jpg", 0, 0, 640, 480)
      GraphicsWindow.FontName = "ComicSans MS"
      GraphicsWindow.FontSize = 12
      namebox = Controls.AddMultiLineTextBox(20, 278)
    EndSub

    Finally , notice this time I modified the welcome message to handle morning times (consider your original code with hour less than 12 - it does the Good Morning and the Good Evening!

      If(Clock.Hour < 12) Then
        GraphicsWindow.ShowMessage("Good morning!" , "Hola!")
      ElseIf (Clock.Hour < 20) Then
        GraphicsWindow.ShowMessage("Gafternoon!!", "Hola!")
      Else
        GraphicsWindow.ShowMessage("Good evening!", "Hola!")
      EndIf

    PS.

    The distinction between registering an event and calling a subroutine is that the event subroutine is called on a new temporary thread every time the event is triggered - in your case you wouldn't want to recreate the text box every time the move moved - perhaps 50 times per second as you move the mouse and for ever after when the mouse moves.

    • Marked as answer by norik434 Saturday, May 21, 2011 3:00 AM
    Saturday, May 21, 2011 12:12 AM
  • OMG! i can't believe I didn't see that... -_-

     

    Thanks a lot!

    Saturday, May 21, 2011 1:19 AM