Please help diagnose my procedure

Just starting out? Need help? Post your questions and find answers here.
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Please help diagnose my procedure

Post by wmorton »

I have an interface which consists of an editor gadget (#Editor) and three buttons - a 'process' button (which triggers this procedure), and 'yes' and 'no' buttons (#Yes and #No)

I want the program to work as follows:
I paste text into the editor and click 'process'. It then shows me each line of text one at a time and should wait for me to click 'yes' or 'no' - at this point the current string will be written to one of two files. It should continue stepping through the text in the editor one line at a time until the end is reached.

The procedure below doesn't seem to work, and I have no idea why. It gets in a loop where it steps through the strings one at a time, but it doesn't wait for my yes/no input. What am I doing wrong?

Thanks!

Code: Select all

Procedure Process()
  
  fulltext$ = GetGadgetText(#Editor)
  string_length = 1+CountString(fulltext$,Chr(13)+Chr(10))
  
  OpenFile(0,GetGadgetText(#YesFilename))
  OpenFile(1,GetGadgetText(#YesFilename))
  
  For position = 1 To string_length
  
    currentstring$ = StringField(fulltext$,position,Chr(13)+Chr(10))
    cleanstring1$ = Trim(currentstring$,Chr(13))
    cleanstring2$ = Trim(cleanstring1$,Chr(10))
    cleanstring3$ = Trim(cleanstring2$)
    cleanstring$ = cleanstring3$
    
    SetGadgetText(#TextCurrentProc,cleanstring$)
    Delay(1000)
    
    Repeat
      
        If position = string_length
          Break
        EndIf
        
        Event = WaitWindowEvent()
         
        Select Event
         
          Case #Yes
            WriteStringN(0,cleanstring$)
      
          Case #No
            WriteStringN(0,cleanstring$)
             
        EndSelect
              
     Until Event = #PB_Event_Gadget
       
  Next
  
  CloseFile(0)
  CloseFile(1)
   
EndProcedure
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Please help diagnose my procedure

Post by luis »

What am I doing wrong?
Almost everything. From what I can see probably your program message loop is structured in a bad way and/or you are trying to use the buttons' PB numbers (#Yes, #No ?) as events (see EventGadget() instead).

In any case, you can't expect the code you just posted to work.
(Wait)WindowEvent return events of type #PB_Event_*.*, certainly not #Yes / #No unless you defined these constant as aliases for some #PB_Event_*.*.
Look at the WindowEvent(), EventGadget(), EventType() help pages, and if still stucked post some runnable code where we can see what you defined, where, and how. Not a snippet.
"Have you tried turning it off and on again ?"
A little PureBasic review
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Please help diagnose my procedure

Post by buddymatkona »

@wmorton
1) File 0,1 are opened with the same filename.
2) Case #yes,#no both write to file 0. Debug changes?
3) Windows events are frequent and asynchronous so there is no waiting for your buttons.
To avoid problems and process all events, you should have a separate event loop without long delays.
4) "While position < string_length" is simpler than FOR when you alter position inside a loop.

Good luck.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Please help diagnose my procedure

Post by RASHAD »

Hi wmorton

Take the next as guide and adapt it for your need

Code: Select all

Global Answer,Row

Procedure Process()
 Result = CountGadgetItems(0)
    Result$ = GetGadgetItemText(0,row)
    If Row < Result
       If Answer = 1
          WriteStringN(0,Result$)
       ElseIf Answer = 2
          WriteStringN(1,Result$)
       EndIf
       SetGadgetItemText(3, Row, "   ")
       Row + 1
       SetGadgetItemText(3, Row, "-->")
    EndIf
 If Row > Result
    CloseFile(0)
    CloseFile(1)
 EndIf
EndProcedure

OpenWindow(0,0,0,400,300,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
EditorGadget(0,30,10,360,240)
AddGadgetItem(0,1,"1- uyiy y iy yi ii iuy ")
AddGadgetItem(0,2,"2- 34546 67575 6 75757 675 ")
AddGadgetItem(0,3,"3- ghfhgfhf  hgfhgfhfgh  fgh")
AddGadgetItem(0,4,"4- uyiy y iy yi ii iuy ")
AddGadgetItem(0,5,"5- uyiy y iy yi ii iuy ")
ButtonGadget(1,10,270,60,22,"File #1")
ButtonGadget(2,80,270,60,22,"File #2")
EditorGadget(3,10,10,18,240)
For r = 0 To 5
    AddGadgetItem(3,r,"   ")
Next
SetGadgetItemText(3, 0, "-->")

OpenFile(0,"e:\test1.txt")
OpenFile(1,"e:\test2.txt")

Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            If IsFile(0)
               CloseFile(0)
            EndIf
            If IsFile(1)
               CloseFile(1)
            EndIf
            Quit = 1
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 1
               Answer = 1
               Process()
           
           Case 2 
               Answer = 2 
               Process()  
          EndSelect          
             
  EndSelect 

Until Quit = 1
End

Egypt my love
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Please help diagnose my procedure

Post by TI-994A »

wmorton wrote:I want the program to work as follows: (requirements)
I paste text into the editor and click 'process'. It then shows me each line of text one at a time and should wait for me to click 'yes' or 'no' - at this point the current string will be written to one of two files. It should continue stepping through the text in the editor one line at a time until the end is reached.
Hello wmorton. This example is based on your snippet and requirements:

Code: Select all

EnableExplicit

Enumeration
  #MainWindow
  #Editor
  #Process_button
  #Yes_button
  #No_button
EndEnumeration

#YesFilename = "AcceptedStrings.txt"
#NoFilename = "RejectedStrings.txt"

Global appQuit
Define wFlags

Procedure Process()
  Define.s currentString, fullText, crIndex.i = 1
  
  OpenFile(0, GetCurrentDirectory() + #YesFilename)
  OpenFile(1, GetCurrentDirectory() + #NoFilename)
  
  fullText = GetGadgetText(#Editor)
  
  While StringField(fullText, crIndex, #CR$) <> #NULL$
    currentString = Trim(StringField(fullText, crIndex, #CR$), #LF$)
    SetGadgetText(#Editor, currentString)
    
    Repeat  
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          appQuit = 1
          ProcedureReturn
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #Yes_button
              WriteStringN(0, currentString)
              Break
            Case #No_button
              WriteStringN(1, currentString)
              Break
          EndSelect
      EndSelect
    ForEver
    crIndex + 1
  Wend
  
  SetGadgetText(#Editor, "")
  HideGadget(#Process_Button, 0)
  DisableGadget(#Process_button, 1)
  HideGadget(#Yes_Button, 1)
  HideGadget(#No_Button, 1)
  
  CloseFile(0)
  CloseFile(1)
  
  ProcedureReturn  
   
EndProcedure
  
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, 0, 0, 260, 320, "Text Parser", wFlags)
ButtonGadget(#Process_Button, 10, 280, 240, 30, "P R O C E S S")
EditorGadget(#Editor, 10, 10, 240, 260, #PB_Editor_WordWrap)
ButtonGadget(#Yes_Button, 50, 280, 60, 30, "YES")
ButtonGadget(#No_Button, 150, 280, 60, 30, "NO")
DisableGadget(#Process_button, 1)
HideGadget(#Yes_Button, 1)
HideGadget(#No_Button, 1)

While Not appQuit
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Editor  
          If EventType() = #PB_EventType_Change
            If Trim(GetGadgetText(#Editor)) <> #NULL$
              DisableGadget(#Process_button, 0)
            Else
              DisableGadget(#Process_button, 1)
            EndIf
          EndIf
        Case #Process_Button
          HideGadget(#Process_Button, 1)
          HideGadget(#Yes_Button, 0)
          HideGadget(#No_Button, 0)
          Process()
      EndSelect
  EndSelect
Wend
Hope it helps.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Re: Please help diagnose my procedure

Post by wmorton »

Thank you all for your replies, hey are a big help. I have a lot to learn!
Post Reply