Beginner Program help

• Question

• I'm trying to read the status of a check box, and associate the status with a variable so I can recall the status later in my program.
Here is my code so far;

GraphicsWindow.Width = 500
GraphicsWindow.Height = 500

InStock = Controls.AddCheckBox("Item in Stock") 'Fremy extension
Controls.Move(InStock, 150, 80)
'Controls.SetChecked(InStock , "True"  )
Controls.SetChecked(InStock , InStockStatus  )
Controls.RegisterEvent (InStock, "CheckclickEvent", "ItemStock_Clickchange")

Sub ItemStock_Clickchange
InStockStatus  = Controls.GetChecked (InStock)
endsub
TextWindow.Write (InStockStatus )

I would like to have the InStockStatus variable be either "True" if checked or "False" not checked.
Any help is greatly appreciated.
Thanks,
Steve
Monday, March 8, 2010 9:43 PM

• Steve,

you're almost there again.  This time, to make it easier, we will register an event :)

I've added a button onto your form. It only saves the stock tickbox when you click the button. When you re-run the program a second time, the tickbox will either be ticked or unticked, depending on what it was like when you clicked the button.

Path = Program.Directory
GraphicsWindow.Width = 500
GraphicsWindow.Height = 500

' add a button to control what happens
Controls.Move(button, 120, 110)
Controls.RegisterMouseUpEvent(button, "saveStatus")

' read the last stock status from file
ItemInStock = File.ReadLine (Path + "\item1.txt", 1)

' display the stock checkbox
Controls.Move(InStock, 150, 80)

' set stock checkbox depending on textfile setting
Controls.SetChecked(InStock, ItemInStock )

' subroutine to save new stock status
Sub saveStatus
' read the current checkbox setting
stockStatus=Controls.GetChecked(InStock)

' store checkbox setting to text file
File.WriteLine (Path + "\Item1.txt", 1, stockStatus )

'quit the program
Program.End()
EndSub  

• Marked as answer by Wednesday, March 10, 2010 9:00 PM
Wednesday, March 10, 2010 8:15 PM

All replies

• I'll first note, for the benefit of those that may not be aware: Steve's code uses an addon to SmallBasic called 'Fremy's Extention'.  The code won't work with the standard version of SmallBasic.

Steve, you're pretty close there.

You don't need to register the CheckclickEvent to handle the ticking/unticking of the checkbox.  In the code below I've stripped it out, and just show you simply how to work with it's true/untrue status. Let me know if you need more explanation.

GraphicsWindow.Width = 500
GraphicsWindow.Height = 500

InStock = Controls.AddCheckBox("Item in Stock") 'Fremy extension
Controls.Move(InStock, 150, 80)

' pause program until InStock is ticked
While Controls.GetChecked(InStock)="False"
Program.Delay(100) ' do something
EndWhile

' get the status of the checkbox (always 'True' in this example)
InStockStatus=Controls.GetChecked(InStock)

' from this point on, the program will remember what InStockStatus is
TextWindow.Write (InStockStatus )

Monday, March 8, 2010 10:49 PM
• Thanks Davey,
I tried the program as you wrote it and it works just as expected, once the check box is clicked the program proceeds.

I'm not understanding something though, I'm trying to save the "InStockStatuse" to a text file using File.Writeline command then the "InStockStatus" is retrieved using the file.readline but the result keeps coming up false.

Path = Program.Directory
GraphicsWindow.Width = 500
GraphicsWindow.Height = 500

ItemInStock = File.ReadLine (Path + "\item1.txt", 1)

Controls.Move(InStock, 150, 80)

'Controls.SetChecked(InStock, ItemInStock )

if Controls.GetChecked(InStock)="True" then
ItemInStock = InStockStatus
EndIf
InStockStatus=Controls.GetChecked(InStock)

TextWindow.Write (ItemInStock  )

File.WriteLine (Path + "\Item1.txt", 1, ItemInStock )

Tuesday, March 9, 2010 1:45 AM
• Steve,

you're almost there again.  This time, to make it easier, we will register an event :)

I've added a button onto your form. It only saves the stock tickbox when you click the button. When you re-run the program a second time, the tickbox will either be ticked or unticked, depending on what it was like when you clicked the button.

Path = Program.Directory
GraphicsWindow.Width = 500
GraphicsWindow.Height = 500

' add a button to control what happens
Controls.Move(button, 120, 110)
Controls.RegisterMouseUpEvent(button, "saveStatus")

' read the last stock status from file
ItemInStock = File.ReadLine (Path + "\item1.txt", 1)

' display the stock checkbox
Controls.Move(InStock, 150, 80)

' set stock checkbox depending on textfile setting
Controls.SetChecked(InStock, ItemInStock )

' subroutine to save new stock status
Sub saveStatus
' read the current checkbox setting
stockStatus=Controls.GetChecked(InStock)

' store checkbox setting to text file
File.WriteLine (Path + "\Item1.txt", 1, stockStatus )

'quit the program
Program.End()
EndSub  

• Marked as answer by Wednesday, March 10, 2010 9:00 PM
Wednesday, March 10, 2010 8:15 PM
• thanks Davey,
That is exactly what I need!
Wednesday, March 10, 2010 9:00 PM