Import data from a txt.file to a trichtextbox - Help

Hi all,

I created a simple application with one button and one richtextbox. The
purpose of the button is to open a FileDialog Common Dialogs to choose a
.txt file:

I created the button event:

evt_button(@button.get_id()){|event| open_file(event)}

the FileDialog widget

@file_dialog = Wx::FileDialog.new(@panel,
message = “Choose a file”,
defaultDir = “”,
defaultFile = “”,
wildcard = “.”,
style = DD_DEFAULT_STYLE,
pos = DEFAULT_POSITION,
sz = DEFAULT_SIZE,
name = “filedlg”)

and the code to run the File.Dialog

def open_file(event)
@file_dialog.show_modal
end

What I don’t know is how can I import data from that file in order to
appear in my richtextbox, although I know that I must pass values to
richtextbox with rich_text_box.value = …

If someone can explain that to me I would be very thankfull

Thanks in advance

Ivo R.

Hello Ivo,

There’s several methods in which to load a file into a RichTextCtrl.
The
first method is to use File.read, as such:

def open_file(event)
if @file_dialog.show_modal == Wx::ID_OK
@mytextbox.value = File.read(@file_dialog.filename)
end
end

The second method, is to use the builtin function from RichTextCtrl to
load
the file

def open_file(event)
if @file_dialog.show_modal == Wx::ID_OK
@mytextbox.load_file(@file_dialog.filename)
end
end

The second method is quicker, and easier, if your just wanting to load a
simple file into the buffer. If you want to do custom highlighting,
then
you would probably be best to load the file through the first method,
but
instead of storing the value directly into @mytextbox.value, you would
probably do it through storing the data from the text file in a
variable,
and parse it, using the RichTextCtrl functions to do the
colorizing/formatting/what have ya.

hth,

Mario

P.S. Take a look at http://wxruby.rubyforge.org/doc for our current
API.
Most functions can be looked up there for basic handling of GUI
elements.

Hello Mario

On first post I forgot to refer some things, like my OS (Windows), Ruby
version (1.9 - OneClickInstaller) and wxRuby version (2.0.1.)

Erroneus I also refer that I want to import data to a textbox but it’s
not. It’s for a RichTextbox (I’m not sure if it’s very important but at
least it make me change some method names…).

def open_file(event)
if @file_dialog.show_modal == Wx::ID_OK
@rich_text_box.load_file(@file_dialog.get_filename, type =
RICHTEXT_TYPE_ANY)
end
end

The problem is that when I try to load the file I get an error message:

    File couldn't be loaded.

And when I ask to see the details the following message appears:

     Can't open file "ENA.txt"(error 2. the system cannot find the 

file specified.)
File couldn’t be loaded

I don’t understand why this happens because the file actually exists…

And if it’s not too much…Can you explain why do I need to use this
piece of code (== Wx::ID_OK)?

Thanks in advance

Ivo R.

Hello Mario,

Thanks for your help…

The code it’s working fine and it’s exactly what I wanted.

But know I get a doubt:

I have read on Ruby Cookbook that when I list a dir I must write on my
code “object_name.rewind” otherwise I can´t run more methods on that
object. Does this also apply in this case? Because after I load the file
to the textbox Enter key stops working.

And about your mistake, it allow me to learn one more thinh :slight_smile:

Thanks in advance

Ivo R.

Hello Ivo,

On Sun, May 23, 2010 at 5:45 PM, Ivo R. [email protected] wrote:

if @file_dialog.show_modal == Wx::ID_OK

    Can't open file "ENA.txt"(error 2. the system cannot find the

file specified.)
File couldn’t be loaded

I don’t understand why this happens because the file actually exists…

My Apologizes, it should actually be,
@rich_text_box.load_file(@file_dialog.get_path()) Not
@rich_text_box.load_file(@file_dialog.get_filename())

The difference, is that get_filename() returns only the name of the
file, in
this case ENA.txt, where as get_path() will return the full path, such
as
C:\ENA.txt

And if it’s not too much…Can you explain why do I need to use this

piece of code (== Wx::ID_OK)?

The check for Wx::ID_OK is to ensure that the user actually hit OK,
cause
the user can still cancel a File Dialog, or close it without selecting a
file, which the return from show_modal will be Wx::ID_CANCEL. Hence why
there’s the check for Wx::ID_OK, to ensure that it is alright to
progress
further.

Lastly, you don’t need to use type in the load_file method. If you need
to
specify a specific format, such as RTF, or something, you would use
that,
but the default value for type, is Wx::RICHTEXT_TYPE_ANY, which means it
will load any file type that is available.

Thanks in advance

Not a problem, and my apologizes for the mistake in the method to use on
the
FileDialog.

Ivo R.

Posted via http://www.ruby-forum.com/.


wxruby-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wxruby-users

hth,

Mario