Page 1 of 1

How to autoresize a ListViewGadget when resizing the window?

Posted: Sun Sep 18, 2022 7:24 pm
by 29emorenol
Hello,

I am quite new at purebasic, i am having some trouble to display a text file and make listviewgadget resize when i resize the window.

Some one can help? or tell me if there is another way to do what i am trying to acomplish ?

Thanks to all!

Code: Select all

FileName$ = OpenFileRequester("", "", "Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)

If ReadFile(0, FileName$)
  
  If OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar | #PB_Window_MaximizeGadget)
    ;If OpenWindow(0, 0, 0, 870, 140, "ListViewGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ; ListViewGadget(0, 10, 10, 250, 120)
    ListViewGadget(0, 10, 10, 850, 120)
    SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, 850,0)
    While Eof(0) = 0
      line$ = VarString$ + ReadString(0) + #CRLF$
      AddGadgetItem (0, -1, line$)
    Wend
    SetGadgetState(0, 9) ; set (beginning with 0) the tenth item as the active one
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
  CloseFile(0)
  
EndIf

Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Sun Sep 18, 2022 7:52 pm
by infratec

Code: Select all

EnableExplicit

Define FileName$, line$, Event.i

FileName$ = OpenFileRequester("", "", "Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)
If FileName$
  If ReadFile(0, FileName$)
    
    If OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar | #PB_Window_MaximizeGadget)
      ListViewGadget(0, 10, 10, 250, 120)
      SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, 250,0)
      While Eof(0) = 0
        line$ = ReadString(0) + #CRLF$
        AddGadgetItem (0, -1, line$)
      Wend
      SetGadgetState(0, 9) ; set (beginning with 0) the tenth item as the active one
      Repeat
        Event = WaitWindowEvent()
        Select Event
          Case #PB_Event_SizeWindow
            ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20, WindowHeight(0) - 10)
            SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, WindowWidth(0) - 20, 0)
        EndSelect
      Until Event = #PB_Event_CloseWindow
    EndIf
    CloseFile(0)
  EndIf
EndIf
But a ListViewGadget() is, maybe, not the right gadget for showing a text file.

Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Sun Sep 18, 2022 8:32 pm
by RASHAD
Welcome to PB

Code: Select all

EnableExplicit

Define FileName$, line$, Event.i

Procedure sizeCB()
  ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20, WindowHeight(0) - 25)
  SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, WindowWidth(0) - 20, 0)
EndProcedure

FileName$ = OpenFileRequester("", "", "Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)
If FileName$
  If ReadFile(0, FileName$)
    
    If OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered| #PB_Window_MaximizeGadget)
      ListViewGadget(0, 10, 10, 250, 120)
      SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, 250,0)
      While Eof(0) = 0
        line$ = ReadString(0) + #CRLF$
        AddGadgetItem (0, -1, line$)
      Wend
      SetGadgetState(0, 9) ; set (beginning with 0) the tenth item as the active one
      BindEvent(#PB_Event_SizeWindow,@sizeCB())
      Repeat
        Event = WaitWindowEvent()

      Until Event = #PB_Event_CloseWindow
    EndIf
    CloseFile(0)
  EndIf
EndIf


Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Sun Sep 18, 2022 8:34 pm
by 29emorenol
Infratec i want to thank you for the help!!

I understand that the way im using the code will only open 65xxx lines.

In your experience what would be the best way to do the same without using ListViewGadget?

Thanks again!!

Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Sun Sep 18, 2022 8:40 pm
by 29emorenol
Thanks Rashad!! another way to understand the solution!!!

What an excellent community!!!

Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Mon Sep 19, 2022 6:43 am
by infratec
There is an EditorGadget() or an ScintillaGadget().
If you want only to view, you can set them to readonly.

Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Mon Sep 19, 2022 4:50 pm
by Axolotl
I recommend a program structure a little more like this

Code: Select all

EnableExplicit

;Define FileName$, line$, Event.i

Procedure sizeCB()
  ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20, WindowHeight(0) - 25)
  SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, WindowWidth(0) - 20, 0)
EndProcedure

Procedure.s OpenTextFile(FileName$)  
  Protected line$ 

  FileName$ = OpenFileRequester("Open Tectfile", FileName$, "Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)
  If FileName$ 
    ClearGadgetItems(0) 
    If ReadFile(0, FileName$)
      While Eof(0) = 0
        line$ = ReadString(0) + #CRLF$
        AddGadgetItem (0, -1, line$)
      Wend
      CloseFile(0)
    EndIf
  EndIf
  ProcedureReturn FileName$ 
EndProcedure

Procedure main() 
; Protected LocalVariables 
  Protected FileName$ 

  If OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered| #PB_Window_MaximizeGadget)
    ListViewGadget(0, 10, 10, 250, 120)
    SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, 250,0)

    BindEvent(#PB_Event_SizeWindow, @sizeCB()) 

    FileName$ = OpenTextFile(FileName$) 
    SetGadgetState(0, 9) ; set (beginning with 0) the tenth item as the active one

    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break 

        Case #PB_Event_Gadget 
          Select EventType() 
            Case #PB_EventType_RightClick 
              FileName$ = OpenTextFile(FileName$) 
              SetGadgetState(0, 9) ; set (beginning with 0) the tenth item as the active one
          EndSelect 
      EndSelect 
    ForEver 
  EndIf
  ProcedureReturn 0  ; return value to system // not necessary 
EndProcedure 

End main()  ;// main() ; only works as well 

Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Mon Sep 19, 2022 4:55 pm
by Axolotl
The ListViewGadget( ) is not the right choice for displaying text.
In Windows these are 'list boxes' .
A list box is a control window that contains a simple list of items from which the user can choose.
For more complex lists, use the List View instead. In PureBasic this is the ListIconGadget.

As infratec mentioned already, a better choice for Text stuff is EditorGadget().
I couldn't stop myself...

Code: Select all

EnableExplicit

;Define FileName$, line$, Event.i

Procedure sizeCB()
  ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20, WindowHeight(0) - 45)
; SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, WindowWidth(0) - 20, 0)
EndProcedure

Procedure.s OpenTextFile(FileName$)  
  Protected line$ 

  FileName$ = OpenFileRequester("Open Tectfile", FileName$, "Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)
  If FileName$ 
    ClearGadgetItems(0) 
    If ReadFile(0, FileName$) 
      line$ = ReadString(0, #PB_File_IgnoreEOL) 
      SetGadgetText(0, line$) 
;     While Eof(0) = 0
;       line$ = ReadString(0) + #CRLF$
;       AddGadgetItem (0, -1, line$)
;     Wend
      CloseFile(0)
    EndIf
  EndIf
  ProcedureReturn FileName$ 
EndProcedure

Procedure main() 
; Protected LocalVariables 
  Protected FileName$ = "test.txt" 

  If OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered| #PB_Window_MaximizeGadget)
;     ListViewGadget(0, 10, 10, 250, 120)
;     SendMessage_(GadgetID(0), #LB_SETHORIZONTALEXTENT, 250,0)
    ButtonGadget(1, 10,  5, 80, 20, "Open") 
    EditorGadget(0, 10, 30, 250, 100, #PB_Editor_ReadOnly) ; | #PB_Editor_WordWrap 

    BindEvent(#PB_Event_SizeWindow, @sizeCB()) 

    FileName$ = OpenTextFile(FileName$) 
    SetGadgetState(0, 9) ; set (beginning with 0) the tenth item as the active one

    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break 

        Case #PB_Event_Gadget 
          Select EventGadget() 
            Case 1 
              FileName$ = OpenTextFile(FileName$) 

          EndSelect 
      EndSelect 
    ForEver 
  EndIf
  ProcedureReturn 0  ; return value to system // not necessary 
EndProcedure 

End main()  ;// main() ; only works as well 

Re: How to autoresize a ListViewGadget when resizing the window?

Posted: Mon Sep 19, 2022 8:23 pm
by 29emorenol
Thanks Axolotl,

It totally make sense, using the right tool for the job!!