I’m having a real stroke-out here. I’ve done this 100 times before in Realstudio, but can’t get it to work in Xojo.
I want to open a file and read in a csv file to put values into Textfields (though right now I’m thinking a listbox might be a better way to go).
I can open a dialog box, and navigate to my file, but no matter what I try, I can’t even read in the first field.
This is the code I have under my action for a button I push to go to and read in my .csv
[code]Sub Action()
dim f as FolderItem = GetFolderItem("\\MICHAELC-PC\Users\MichaelC\Documents")
dim tis as TextInputStream
dim st() as string
dim s as string
dim i as integer
if f <> nil and f.Exists then
Listbox1.DeleteAllRows
tis = TextInputStream.Open(f)
tis.Encoding = Encodings.UTF8
s = tis.ReadAll
st = split(s, EndOfLine)
end if
if tis.EOF = false then
Listbox1.AddRow
Listbox1.Cell( i , -1) = st(i).ReplaceAll(",", chr(9))
end
tis.Close
Listbox1.Heading(-1) = Listbox1.Cell( 0 , -1)
Listbox1.RemoveRow(0)
End Sub[/code]
looks to me like you only ever work with ROW ZERO (since the value of “i” is never changed that I can see)
and then you remove(0)
[code]
Sub Action()
dim f as FolderItem = GetFolderItem("\\MICHAELC-PC\Users\MichaelC\Documents")
dim tis as TextInputStream
dim st() as string
dim s as string
dim i as integer
if f <> nil and f.Exists then
Listbox1.DeleteAllRows
tis = TextInputStream.Open(f)
tis.Encoding = Encodings.UTF8
s = tis.ReadAll
tis.Close
st = split(s, EndOfLine)
for i=0 to st.ubound
Listbox1.AddRow
Listbox1.Cell( i , -1) = st(i).ReplaceAll(",", chr(9))
next i
end if
Listbox1.Heading(-1) = Listbox1.Cell( 0 , -1)
Listbox1.RemoveRow(0)
End Sub
Ok, but when I go to open the project, with the file, it keeps staying at a point that says "Loading simple CSV and asks me to select another file, but not telling me which file I have to select.
My users will undoubtedly want to use the program for different “csv” files. As stated above what I thought was a comma separator was actually a TAB. So on my window I have 3 checkboxes one for commas, one for spaces, one for tabs.
I have created a select case, as follows:
select case index
case 0
MsgBox "You clicked the first checkbox"
'tab = changeSeparator("44")
CheckBox1(2).Value = False
Checkbox1(1).Value = False
case 1
MsgBox "You clicked the second checkbox"
'tab = changeSeparator("9")
CheckBox1(2).Value = False
Checkbox1(3).Value = False
case 2
MsgBox "You clicked the thired checkbox"
'tab = changeSeparator("32")
CheckBox1(0).Value = False
Checkbox1(1).Value = False
end select
I know when I click the checkbox, I hit the right case. Great.
Now since I want to pass each of these values for different separators to the same variable I created a global module with the name I wanted, with a value as
changeSeparator
Parameters : tab as string
Return type : string
Scope : Global
However, I never seem to able able to get the value to get sent to the window.
In general for desktop applications, I prefer RadioButtons in a GroupBox as they are automatically mutually exclusive, while checkboxes are great when you can select any combination of entries.
There is some sample code in the RadioButton documentation that shows you can access which choice the user made. I’d suggest you setup a group box with radio buttons for each delimiter choice, making one of them the default.
But to more specifically address your question, your code block does not show how you get “index” the in the SELECT CASE statement, or where this code is located other than “on the window”. So (to me) it is too ambiguous to answer the question asked. That said, I’d consider refactoring to radio buttons then see if you have more follow up questions.
Well, I got the separator issue fixed. Thanks to everyone for their help so far.
Instead of setting up a global module and a method, I just created a Property and it works fine.
Man, why am I always trying to do things the hard way?
Anyway, that brings up this question…
When I tried to use the global module and method, I could never pass the variable to my pushbutton event.
Why would that be?
I’ve read the Introduction to Xojo book, and I think I am doing everything correctly.