It was said numerous time that we can add .txt file in the Navigation pane and use it as a Variable.
In a small (very small) project, I had to add a list (actually 26 lines, but can grow).
I do not wanted to add lines of codes to copy the .txt file into Application Support, code to open it, etc.
Then I dropped the .txt file in the Navigation pane, and add that code:
API (1, sorry) Code:
Sub Open() Handles Open
Dim Loop_Idx As Integer
Dim Line_Cnt As Integer
Dim AnEntry As String
// How many entries in Character_List_CK ?
// 0-based & 1 EndOfLine at the end of the document = -2
Line_Cnt = CountFields(Character_List_CK, EndOfLine)-2
// Scan the whole Character_List_CK contents
For Loop_Idx = 0 To Line_Cnt
// Get a Character and URL
AnEntry = NthField(Character_List_CK, EndOfLine, Loop_Idx+1)
// Add a Row and put a Character and URL
Me.AddRow NthField(AnEntry, Chr(9), 1)
Me.RowTag(Loop_Idx) = NthField(AnEntry, Chr(9), 2)
// In case of… trouble
If UserCancelled Then Exit
PS: The original project (not that code) was in API2 (Xojo 21r2.1), once converted to 2015r1, the project was smaller than the original in API2 (do not know why). But not by far, this is a very small project.
For anyone curious about what is being discussed here, forgive me for mansplaining…
Emile has a text file called Character_List_CK.txt
It contains 26 rows of text
By dragging it into the IDE, it becomes part of the app.
(It will also appear in the resources folder of the compiled app as a file called Character_List_CK.txt)
But having dragged it into the project in this way, you can ALSO refer to the contents as if they are a string constant called
So it is possible to msgbox Character_List_CK,
and to parse the contents as Emile has shown above.
That’s it ! Thank you for this very good explanation.
I noticed that the code does not set a default entry (the PopupMenu is blank when the window appears).
So I add after
// Set a default Row
Me.SelectedRowIndex = 1 // You can set Row 0 if you want…
Well, since your explanation fills a gap in Emile’s post, I thank you for it.
As an alternative, would it not also be possible just to create a string constant in the application and place the text there during development? After that, it could be accessed in essentially the same way. And this would avoid having it end up in the resources folder.
As long as the text is not too long and without end of lines, yes, that works too.
I have string constants that are larger than 1.4MB – with, I think, 2.5MB being the biggest – in one of my projects, and EOLs aren’t a problem. I don’t usually do that, but there’s a specific reason this project requires it.
Ok, sorry, I was wrong. Looks like both the issues I thought are actually not a problem.
The drawback is if the text file evolve, you have to recompile the application after the changes vs an external text file.
I was happy to experiment that way / do not wrote the whole code to copy the file in Application SUpport, etc…