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

**URL:** <https://www.ruby-forum.com/t/import-data-from-a-txt-file-to-a-trichtextbox-help/189065>\
**Category:** wxRuby\
**Created:** [May 23, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/import-data-from-a-txt-file-to-a-trichtextbox-help/189065 "2010-05-23T18:51:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 23, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/import-data-from-a-txt-file-to-a-trichtextbox-help/189065/1 "2010-05-23T18:51:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 23, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/import-data-from-a-txt-file-to-a-trichtextbox-help/189065/2 "2010-05-23T19:12:07Z")

</div>

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](http://wxruby.rubyforge.org/doc) for our current  
API.  
Most functions can be looked up there for basic handling of GUI  
elements.

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 23, 2010, 11:45pm UTC](https://www.ruby-forum.com/t/import-data-from-a-txt-file-to-a-trichtextbox-help/189065/3 "2010-05-23T23:45:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 27, 2010, 2:32am UTC](https://www.ruby-forum.com/t/import-data-from-a-txt-file-to-a-trichtextbox-help/189065/4 "2010-05-27T02:32:02Z")

</div>

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 🙂

Thanks in advance

Ivo R.

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 24, 2010, 11:11am UTC](https://www.ruby-forum.com/t/import-data-from-a-txt-file-to-a-trichtextbox-help/189065/5 "2010-05-24T11:11:25Z")

</div>

Hello Ivo,

On Sun, May 23, 2010 at 5:45 PM, Ivo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.ruby-forum.com/).
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

hth,

Mario
