How to autoresize a ListViewGadget when resizing the window?

Just starting out? Need help? Post your questions and find answers here.
29emorenol
New User
New User
Posts: 5
Joined: Sun Sep 18, 2022 7:17 pm

How to autoresize a ListViewGadget when resizing the window?

Post 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
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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

Egypt my love
29emorenol
New User
New User
Posts: 5
Joined: Sun Sep 18, 2022 7:17 pm

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

Post 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!!
29emorenol
New User
New User
Posts: 5
Joined: Sun Sep 18, 2022 7:17 pm

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

Post by 29emorenol »

Thanks Rashad!! another way to understand the solution!!!

What an excellent community!!!
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

There is an EditorGadget() or an ScintillaGadget().
If you want only to view, you can set them to readonly.
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

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

Post 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 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

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

Post 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 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
29emorenol
New User
New User
Posts: 5
Joined: Sun Sep 18, 2022 7:17 pm

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

Post by 29emorenol »

Thanks Axolotl,

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