Hi, I was wondering if it is possible to create a field editor like xvalue, but which takes a string input like a file name. I'm trying to create a prompt window which asks the user to specify a binary file to be read in before running the simulation. Is such a thing possible?
Thanks,
Annik
xvalue for strings?
-
- Site Admin
- Posts: 6384
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
Re: xvalue for strings?
Good question. Use the File class's chooser() method; documented in the Programmer's Reference.
Re: xvalue for strings?
Great, I got a button to bring up a window to choose a file using the following code (as per the programmer's reference):
With in the code for the prompt window where the user specifies parameters for a simulation.
But when I choose a file, I get an error saying "Expecting string argument" - I thought the name of the file selected is the string argument given to the TC_f.chooser() ?
What has gone wrong here?
Thanks,
Annik
Code: Select all
objref TC_f
TC_f = new File()
proc read_TC() {
TC_f.chooser("r", "Select a File to Read", "*.txt", "Execute")
if (TC_f.chooser()) {
str = TC_f.getname(str)
xopen(str)
}
}
Code: Select all
xbutton("Choose A File...", "read_TC()")
But when I choose a file, I get an error saying "Expecting string argument" - I thought the name of the file selected is the string argument given to the TC_f.chooser() ?
What has gone wrong here?
Thanks,
Annik
Re: xvalue for strings?
Got it. The code below works:
Code: Select all
proc read_TC() {
TC_file_loc.chooser("", "Choose a File to Read", "*.txt", "Execute")
if (TC_file_loc.chooser()) {
strdef str
str = TC_file_loc.getname()
TC_read.ropen(str)
}
}
-
- Site Admin
- Posts: 6384
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
Re: xvalue for strings?
That does work, but it's a bit longer and more involved than it has to be, because it's based on the example for the workaround for the fact that the "x" style ("select and open a file for execution") is not implemented. For those who only need to read or write a file, something like this simpler example is sufficient:
Code: Select all
objref f
f = new File()
proc read_TC() {
f.chooser("r", "Choose a File to Read", "*.txt", "Select")
if (f.chooser()) { // clicking on the "Select" button makes f.chooser return 1
f.ropen()
// print "opened ", f.getname(), "for reading" // optional, for debugging
}
}