ListViewGadget to text file

Just starting out? Need help? Post your questions and find answers here.
David
User
User
Posts: 14
Joined: Sat Mar 05, 2005 11:23 am

ListViewGadget to text file

Post by David »

Hi.

Is there any way to copy all items in a ListViewGadget to a text file on separate lines? Doing it the other way around is no problem but I don't see a way of doing this. The documentation gives these commands:

- AddGadgetItem(): Add an item.
- RemoveGadgetItem(): Remove an item.
- ClearGadgetItemList(): Remove all the items
- CountGadgetItems(): Returns the number of items currently in the #Gadget.

- GetGadgetState(): Get the index of the selected item or -1 if there is no selected item.
- GetGadgetItemText(): Get the content of the given item.
- GetGadgetText(): Get the content of the selected item.
- SetGadgetState(): Change the selected item. If -1 is specified, the selection will be removed.

All of them appear to require an item to be selected, but I am trying to copy the entire contents without any selections. Any help would be appreciated. Thanks.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

David, what about this?...


Code: Select all

;/
Procedure.s GatherListViewItems(GadgetID.l, AddCRLF=#True)
  Define.l lItems, n
  Define.s sTempStr
  
  lItems = CountGadgetItems(GadgetID)
  If AddCRLF = #True
    For n=0 To lItems
      sTempStr + GetGadgetItemText(GadgetID,n,0) + #crlf$
    Next
  Else
    For n=0 To lItems
      sTempStr + GetGadgetItemText(GadgetID,n,0)
    Next
  EndIf
  
  ProcedureReturn sTempStr
  
EndProcedure
;/

Procedure.s CreateJunk(HowLong)
  Define.s sTempJunk
  Define.l n
  
  For n=0 To HowLong-1
    sTempJunk + Chr( 32+Random( 94 ) )
  Next
  ProcedureReturn sTempJunk
  
EndProcedure

;/

OpenWindow(0, 0, 0, 320, 240, "" )
CreateGadgetList( WindowID( 0 ) )
ListViewGadget( 0, 5, 5, 310, 230 )

;/ Lets fill the gadget with some junk...
For n=0 To 99
  AddGadgetItem( 0, -1,  Str(n) + "    " + CreateJunk(40))
Next

;/ Lets just debug all contents, no need for saving...
Debug GatherListViewItems(0)

;/
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
You Can't expect the language to have a function for everyones needs...
Last edited by dagcrack on Sat Aug 12, 2006 7:11 pm, edited 1 time in total.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Look more closely at GetGadgetItemText().
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

He wanted a helper routine I think :)
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
David
User
User
Posts: 14
Joined: Sat Mar 05, 2005 11:23 am

Post by David »

Hi.

I have been looking at GetGadgetItemText() for most of the day but I've not been able to find a way, so you are saying I am missing something Trond? I'll look again, thanks.

Thanks dagcrack, I'll give that a try. No, I certainly wouldn't expect PB to have a function for everyones needs. I just couldn't find a way of doing something so I thought ask others who are more experienced than myself. I appreciate your help, thanks.

BTW, what is a helper routine?
David
User
User
Posts: 14
Joined: Sat Mar 05, 2005 11:23 am

Post by David »

Your code worked dagcrack. Thank you very much!
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

David, no problem!.

About your question: A helper routine is simply a routine that serves a "helping" purpose, like this one does.

It helps you from writing lots of code, providing you an instant output of what you needed.
It could of been a macro though, but as you wish...

With time, you'll write lots of useful helper routines which you might include on your programs all the time.

A stupid example:

Code: Select all

Procedure.s hFixSentence(sInputStr.s) ; our helper routine.
  ;
  While FindString(sInputStr,"  ",0)
    sInputStr = ReplaceString(sInputStr,"  ", " ", 1)
  Wend
  sInputStr = ReplaceString(sInputStr, " ? ", "?",1)
  
  ProcedureReturn Trim(sInputStr)
  ;
EndProcedure

;/
Define.s sIn

sIn = " Hey     hows going ?   "
sIn = hFixSentence(sIn)
Debug sIn
In a program where you'd need to perform X actions all the time, on different parts of the program, you might want a helper routine (either as proc or macro, depending on the case). Instead of having to write the same thing all the time, I guess this is obvious even for the beginner...

As helpers, Macro do a better job, but we cant globalize anything when we talk about programming, since every single case is special and needs to be treated in a different way most of the times.

By the way, I assumed you were on Windows, thats why I used CRLF, else, you'll have to change to suit your needs. Since *nix uses LF !.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

dagcrack seems to be in a chippery mood today :lol:
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Post Reply