Lionheart008
05-07-2010, 16:38
showing simple prime numbers to 1000 :) I've have translated this example from powerbasic (credit to author: borje hagsten) side (very old one and adapted for thinbasic).
' Empty GUI script created on 07-05-2010 16:23:08 by frank (ThinAIR)
Uses "ui"
Begin Const, %WM_USER
%listy
%labely
%texty
End Const
Declare Function GetDlgItem Lib "USER32.DLL" Alias "GetDlgItem" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Function TBMain() As Long
Local hDlg As Long
Dialog New 0, "thinbasic: get prime numbers", -1, -1, 180, 160, %WS_SYSMENU To hDlg
Control Add LISTBOX, hDlg, %listy, , 4, 20, 100, 120, _
%WS_TABSTOP Or %WS_VSCROLL, %WS_EX_CLIENTEDGE
Control Add Button, hDlg, %IDOK, "Get", 112, 4, 50, 14,
Control Add Button, hDlg, %IDCANCEL, "Exit", 112, 20, 50, 14,
Control Add Label, hDlg, %labely, "Up to:", 4, 4, 40, 10,
Control Add Textbox, hDlg, %texty, "1000", 44, 4, 60, 12, %ES_NUMBER, %WS_EX_CLIENTEDGE
Dialog Show Modal hDlg Call DlgCallback
End Function
CallBack Function DlgCallback()
Select Case CBMSG
Case %WM_INITDIALOG
Control Set Focus CBHNDL, %IDOK
Case %WM_COMMAND
Select Case CBCTL
Case %IDOK
Local Buf As String
Local Num As Long
Local t As Single
Control Get Text CBHNDL, %texty To Buf
Num = Val(buf)
If Num Then
LISTBOX Reset CBHNDL, %listy
t = Timer
GetPrimes GetDlgItem(CBHNDL, %listy), Num
t = Timer - t
Control Send CBHNDL, %listy, %LB_GETCOUNT, 0, 0 To Num
MsgBox 0, Format$(Num) & " prime numbers listed in " & Format$(t, "0.000") & " seconds."
End If
Case %IDCANCEL
Dialog End CBHNDL
End Select
End Select
End Function
Sub GetPrimes (ByVal Lst As Long, ByVal MaxNum As Long)
Local Buf As String
Local i As Long
Local Num As Long
Local Flag As Byte
Flag = 1
For Num = 1 To MaxNum
For i = 2 To Sqr(Num)
If num And Mod(num,i) = 0 Then
Flag = 0
Exit For
End If
Next I
If Flag Then
Buf = Str$(Num)
SendMessage Lst, %LB_ADDSTRING, 0, StrPtr(Buf)
Else
Flag = 1
End If
Next Num
End Sub
edit: have modified this example and deleted two (not useful) lines.
best regards, frank
' Empty GUI script created on 07-05-2010 16:23:08 by frank (ThinAIR)
Uses "ui"
Begin Const, %WM_USER
%listy
%labely
%texty
End Const
Declare Function GetDlgItem Lib "USER32.DLL" Alias "GetDlgItem" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Function TBMain() As Long
Local hDlg As Long
Dialog New 0, "thinbasic: get prime numbers", -1, -1, 180, 160, %WS_SYSMENU To hDlg
Control Add LISTBOX, hDlg, %listy, , 4, 20, 100, 120, _
%WS_TABSTOP Or %WS_VSCROLL, %WS_EX_CLIENTEDGE
Control Add Button, hDlg, %IDOK, "Get", 112, 4, 50, 14,
Control Add Button, hDlg, %IDCANCEL, "Exit", 112, 20, 50, 14,
Control Add Label, hDlg, %labely, "Up to:", 4, 4, 40, 10,
Control Add Textbox, hDlg, %texty, "1000", 44, 4, 60, 12, %ES_NUMBER, %WS_EX_CLIENTEDGE
Dialog Show Modal hDlg Call DlgCallback
End Function
CallBack Function DlgCallback()
Select Case CBMSG
Case %WM_INITDIALOG
Control Set Focus CBHNDL, %IDOK
Case %WM_COMMAND
Select Case CBCTL
Case %IDOK
Local Buf As String
Local Num As Long
Local t As Single
Control Get Text CBHNDL, %texty To Buf
Num = Val(buf)
If Num Then
LISTBOX Reset CBHNDL, %listy
t = Timer
GetPrimes GetDlgItem(CBHNDL, %listy), Num
t = Timer - t
Control Send CBHNDL, %listy, %LB_GETCOUNT, 0, 0 To Num
MsgBox 0, Format$(Num) & " prime numbers listed in " & Format$(t, "0.000") & " seconds."
End If
Case %IDCANCEL
Dialog End CBHNDL
End Select
End Select
End Function
Sub GetPrimes (ByVal Lst As Long, ByVal MaxNum As Long)
Local Buf As String
Local i As Long
Local Num As Long
Local Flag As Byte
Flag = 1
For Num = 1 To MaxNum
For i = 2 To Sqr(Num)
If num And Mod(num,i) = 0 Then
Flag = 0
Exit For
End If
Next I
If Flag Then
Buf = Str$(Num)
SendMessage Lst, %LB_ADDSTRING, 0, StrPtr(Buf)
Else
Flag = 1
End If
Next Num
End Sub
edit: have modified this example and deleted two (not useful) lines.
best regards, frank