Nice example. Thanks for sharing!
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).
[code=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[/code]
edit: have modified this example and deleted two (not useful) lines.
best regards, frank
you can't always get what you want, but if you try sometimes you might find, you get what you need
Nice example. Thanks for sharing!
Hi Frank,
thanks!
Little tip for you - instead of:
[code=thinbasic]
Begin Const, %WM_USER
%listy
%labely
%texty
End Const
[/code]
you can use more self documenting:
[code=thinbasic]
Begin ControlID
%listy
%labely
%texty
End ControlID
[/code]
As you say, this is based on PowerBasic example. Maybe it would be polite to cite the original author.
Thanks!,
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
you are welcome mike, petr
1)see my first postAs you say, this is based on PowerBasic example. Maybe it would be polite to cite the original author.
2)I have had a look at the manual. And it's some kind of a new thing to use more "Begin ControlID" instead of "begin const" ("begin const, %WM_User") I have picked this one from manualBegin ControlID
%listy
%labely
%texty
End ControlID
servus, frank
you can't always get what you want, but if you try sometimes you might find, you get what you need
Hi Frank,
thanks for updating it!
Yes, Begin/End ControlID is quite new, that is why brought it up. It makes code a bit easier to read.
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
Bookmarks