• Welcome to Theos PowerBasic Museum 2017.

Problem with "Control set text"...

Started by Christian Damhus, May 10, 2014, 08:16:02 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Christian Damhus

Another beginner's question. I found an old program, whichs draws six different numbers. It's a lottery programm and an old DOS-Programm. Using PB3.5 it works fine and does what I want it to do. I tried to re-write it in PB/Win 10. When I use the msgbox-Command, it shows the drawn numbers. But I want these numbers do be displayed in the Textboxes. This does not work. It always shows the same number. I can't find the solution. Can somebody help me? Thank you!

#COMPILE EXE
#DIM ALL

GLOBAL hDlg         AS DWORD
GLOBAL Result       AS DWORD

GLOBAL i            AS INTEGER
GLOBAL number()     AS INTEGER
GLOBAL counter      AS INTEGER

%GetNumbers      = 1000



FUNCTION PBMAIN () AS LONG
    DIM number(1 TO 49) AS INTEGER


    DIALOG NEW PIXELS, %HWND_DESKTOP, "Lottozahlen-Generator",,,300,250, %WS_SYSMENU TO hDlg
    CONTROL ADD LABEL, hDlg, 100, "Zahl #1:", 30, 20, 40, 15
    CONTROL ADD LABEL, hDlg, 101, "Zahl #2:", 30, 50, 40, 15
    CONTROL ADD LABEL, hdlg, 102, "Zahl #3:", 30, 80, 40, 15
    CONTROL ADD LABEL, hDlg, 103, "Zahl #4:", 30, 110, 40, 15
    CONTROL ADD LABEL, hDlg, 104, "Zahl #5:", 30, 140, 40, 15
    CONTROL ADD LABEL, hDlg, 105, "Zahl #6:", 30, 170, 40, 15

    CONTROL ADD TEXTBOX,hDlg, 106, "", 80, 20, 40, 20, %ES_READONLY OR %WS_BORDER
    CONTROL ADD TEXTBOX, hDlg, 107, "", 80, 50, 40, 20, %ES_READONLY OR %WS_BORDER
    CONTROL ADD TEXTBOX, hDlg, 108, "", 80, 80, 40, 20, %ES_READONLY OR %WS_BORDER
    CONTROL ADD TEXTBOX, hDlg, 109, "", 80, 110, 40, 20, %ES_READONLY OR %WS_BORDER
    CONTROL ADD TEXTBOX, hDlg, 110, "", 80, 140, 40, 20, %ES_READONLY OR %WS_BORDER
    CONTROL ADD TEXTBOX, hDlg, 111, "", 80, 170, 40, 20, %ES_READONLY OR %WS_BORDER

    CONTROL ADD BUTTON, hDlg, %GetNumbers, "Get the Numbers!", 30, 220, 240, 20
    DIALOG SHOW MODAL hDlg CALL DlgProc TO Result

END FUNCTION

CALLBACK FUNCTION DlgProc
    LOCAL a     AS INTEGER
    SELECT CASE CB.MSG
        CASE %WM_COMMAND
            IF CB.CTLMSG = %BN_CLICKED THEN
                IF CB.CTL = %GetNumbers THEN
                    RANDOMIZE TIMER
                    FOR i = 1 TO 6
                        DO
                            counter = INT(RND*49)+1
                            IF (number(counter) = 0) THEN
                                number(counter) = 1
                                EXIT DO
                            END IF
                        LOOP
                NEXT i
                FOR i = 1 TO 49
                    IF (number(i) = 1) THEN
                        FOR a = 106 TO 111
                        CONTROL SET TEXT hDlg, a, STR$(i)
                        'control set text hDlg, 107, Str$(i)
                        'msgbox str$(i)

                        NEXT a
                    END IF
                NEXT i

                    FUNCTION = 1
                END IF
            END IF
    END SELECT


END FUNCTION

Edwin Knoppert

Almost everything in Windows requires manual updates when the app is not getting into idle mode (due to calculations etc)..
Is there a control update or refresh in pb?
Otherwise UpdateWindow(hWndOfTextBox) is needed.

Christian Damhus

Hello, Mr. Knoppert.

Thanks for you reply. Well, to be honest! I am an absolut beginner and have never heard of "UpdateWindow". I tried to get some information from VB-Programs, but it also doesn't work. As an old Basic/DOS-Hobbyprogrammer I thought it would be something like:

FOR i = 1 to 49
PRINT i
NEXT

But now I see that Windows isn't that easy, at least for a beginner!

Edwin Knoppert

#3
I have looked it up, try:
CONTROL REDRAW hDlg, id&
after each text you set.

(where id& is your a variable like: CONTROL REDRAW hDlg, a )

Christian Damhus

Thank you for your help. But somehow it still displays the same number in all Textbox-Controls.

CALLBACK FUNCTION DlgProc

    'DIM number(1 TO 49) AS INTEGER
    SELECT CASE CB.MSG
        CASE %WM_COMMAND
            IF CB.CTLMSG = %BN_CLICKED THEN
                IF CB.CTL = %GetNumbers THEN
                    RANDOMIZE TIMER
                    FOR i = 1 TO 6
                        DO
                            counter = INT(RND*49)+1
                            IF (number(counter) = 0) THEN
                                number(counter) = 1
                                EXIT DO
                            END IF
                        LOOP
                NEXT i
                FOR i = 1 TO 49
                    IF (number(i) = 1) THEN
                        FOR a = 106 TO 111
                        CONTROL SET TEXT hDlg, a, STR$(i)
                        [i][b]CONTROL REDRAW hDlg, a[/b][/i]


                        'control set text hDlg, 107, Str$(i)
                        'msgbox str$(i)

                        NEXT a
                    END IF
                NEXT i

                    FUNCTION = 1
                END IF
            END IF
    END SELECT



Hmmm... I don't understand it. The logic is pretty simple, I think. Each Textbox (controls 106 to 111) is meant to display one of the six numbers that have been drawn before. But they still display the same number. Okay, I know that Windows-Programming is a bit difficult, but now I am totally stucked.

Is there something like a "Beginners Guide to PowerBASIC/Win"? Has anybody the intention to write such a book?

José Roca

But you're telling it to print the same number in all the controls...

Instead of


                FOR i = 1 TO 49
                    IF (number(i) = 1) THEN
                        FOR a = 106 TO 111
                        CONTROL SET TEXT hDlg, a, STR$(i)
                        [i][b]CONTROL REDRAW hDlg, a[/b][/i]


                        'control set text hDlg, 107, Str$(i)
                        'msgbox str$(i)

                        NEXT a
                    END IF
                NEXT i


use


                LOCAL idx AS LONG
                idx = 106
                FOR i = 1 TO 49
                    IF (number(i) = 1) THEN
                        CONTROL SET TEXT hDlg, idx, STR$(i)
                        INCR idx
                    END IF
                NEXT i


Christian Damhus

Thank you, Mr. Roca. Indeed, it works now. I admit I did not think of such a solution. This tells me, that I have to learn alot.