opening / saving files with MS rich text box control (viewing open files) 
Author Message
 opening / saving files with MS rich text box control (viewing open files)

Hi,

Very simple question that I really should know the answer to but I don't...
how do I get a selected file in the Open common dialog box to open and it's
contents shown in the MS common dialog box control?

The code I have so far is this:
-------
Private Sub imgOpen_Click()

Dim strOpenFileName As String

cdbOpen.DialogTitle = "File Open"
cdbOpen.Filter = "*.txt"
cdbOpen.ShowOpen    'Triggers the dialog box
strOpenFileName = cdbOpen.FileName
rtbMain.Text = strOpenFileName
End Sub
--------
But the second from last line, rtbMain.Text =strOpenFileName only prints the
actual path to the file, not the file's contents...

Thanks
Simon Yu



Sun, 28 Sep 2003 21:03:54 GMT  
 opening / saving files with MS rich text box control (viewing open files)
Simon

First I have to say that, in one of your other posts you said that you're
using the Learning Edition which doesn't have the Help files, and it's
obvious that you're trying things out before asking for help, we don't mind
answering your many :) posts.

I think you've misunderstood what the CommonDialog File Open/Save does. Its
sole purpose is to return a path to the file, then it's up to you to write
the code that gets/sets the file's contents.

Now once you have a path to the file, it it's just a text file you do this

Dim strOpenFileName As String
Dim strFileContents As String
Dim fnum As Integer
strOpenFileName = cdbOpen.FileName
If Len(strOpenFileName) Then
    fnum = FreeFile
    Open strOpenFileName For Input As #fnum
    strFileContents = Input(LOF(fnum), #fnum)
    'or you can get each line by doing this
    'Do Until EOF(fnum)
    '    Input #fnum, strFileContents
    '    rtbMainText.SelStart = Len(rtbMainText.Text)
    '    rtbMainText.SelText = strFileContents & vbNewLine
    'Loop
    rtbMainText.Text = strFileContents
    Close #fnum
End If

But as you're using a RichTextBox you can miss out all of the above and just
do this

strOpenFileName = cdbOpen.FileName
If Len(strOpenFileName) Then rtbMainText.FileName = strOpenFileName

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/


Quote:
> Hi,

> Very simple question that I really should know the answer to but I
don't...
> how do I get a selected file in the Open common dialog box to open and
it's
> contents shown in the MS common dialog box control?

> The code I have so far is this:
> -------
> Private Sub imgOpen_Click()

> Dim strOpenFileName As String

> cdbOpen.DialogTitle = "File Open"
> cdbOpen.Filter = "*.txt"
> cdbOpen.ShowOpen    'Triggers the dialog box
> strOpenFileName = cdbOpen.FileName
> rtbMain.Text = strOpenFileName
> End Sub
> --------
> But the second from last line, rtbMain.Text =strOpenFileName only prints
the
> actual path to the file, not the file's contents...

> Thanks
> Simon Yu



Sun, 28 Sep 2003 23:31:01 GMT  
 opening / saving files with MS rich text box control (viewing open files)
Hi,

Thanks for continued help, however one thing I need clearing up:

Quote:
> Dim strOpenFileName As String
> Dim strFileContents As String
> Dim fnum As Integer
> strOpenFileName = cdbOpen.FileName
> If Len(strOpenFileName) Then

This line above right there; as it uses as "If" statement, surely there
should be a comparison?  Otherwise, will the If statement always return
"true"?  Is it possible for the above statement to return *false* (in which
case the If would be unnecessary, right?)

And that's it; again thanks for help!

Cheers
Simon Yu



Tue, 30 Sep 2003 02:50:52 GMT  
 opening / saving files with MS rich text box control (viewing open files)
Hi,

Thanks for continued help, however one thing I need clearing up:

Quote:
> Dim strOpenFileName As String
> Dim strFileContents As String
> Dim fnum As Integer
> strOpenFileName = cdbOpen.FileName
> If Len(strOpenFileName) Then

This line above right there; as it uses as "If" statement, surely there
should be a comparison?  Otherwise, will the If statement always return
"true"?

And that's it; again thanks for help

Cheers
Simon Yu



Tue, 30 Sep 2003 02:50:51 GMT  
 opening / saving files with MS rich text box control (viewing open files)


Quote:
>Hi,

>Thanks for continued help, however one thing I need clearing up:

>> Dim strOpenFileName As String
>> Dim strFileContents As String
>> Dim fnum As Integer
>> strOpenFileName = cdbOpen.FileName
>> If Len(strOpenFileName) Then

>This line above right there; as it uses as "If" statement, surely there
>should be a comparison?  Otherwise, will the If statement always return
>"true"?  Is it possible for the above statement to return *false* (in which
>case the If would be unnecessary, right?)

The If <expression> Then

line evaluates <expression>. If that is zero it returns False, otherwise
it returns True.

HTH

Regards.

--
Martin Trump



Tue, 30 Sep 2003 03:10:53 GMT  
 opening / saving files with MS rich text box control (viewing open files)
Simon

You could write it as

If Len(strOpenFileName) > 0 Then

but in VB any number that's not 0 will equal True, so you don't need to use
the > operator. What you're really testing for in the above is the length of
the string is Not False, and the only number that represents False is 0, so
any other number will be True.

Now if you're checking 2 string lengths you do need to use the operator,
i.e.

If Len(strOpenFileName1) > 0 And Len(strOpenFileName2) > 0 Then

This is because of the way And works, it actually masks the bits of the
return values, so in the above we're checking for a bit comparison of 2
Boolean results, but if you write it as

If Len(strOpenFileName1) And Len(strOpenFileName2) Then

and use strOpenFileName1 = "abc" and strOpenFileName2 = "abcd", even though
both strings are not zero length, the comparison will return False because
when you do a bit mask of 3 which in binary is 0000011, and 4 - 0000100 no
bits match so the result will be 0 or False.

Hope this helps

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/


Quote:
> Hi,

> Thanks for continued help, however one thing I need clearing up:

> > Dim strOpenFileName As String
> > Dim strFileContents As String
> > Dim fnum As Integer
> > strOpenFileName = cdbOpen.FileName
> > If Len(strOpenFileName) Then

> This line above right there; as it uses as "If" statement, surely there
> should be a comparison?  Otherwise, will the If statement always return
> "true"?  Is it possible for the above statement to return *false* (in
which
> case the If would be unnecessary, right?)

> And that's it; again thanks for help!

> Cheers
> Simon Yu



Tue, 30 Sep 2003 04:27:29 GMT  
 opening / saving files with MS rich text box control (viewing open files)
Heh, for this part (well, one line of it) - "WHOOSH" :)

Quote:
> Now if you're checking 2 string lengths you do need to use the operator,
> i.e.

> If Len(strOpenFileName1) > 0 And Len(strOpenFileName2) > 0 Then

> This is because of the way And works, it actually masks the bits of the
> return values, so in the above we're checking for a bit comparison of 2
> Boolean results, but if you write it as

> If Len(strOpenFileName1) And Len(strOpenFileName2) Then

> and use strOpenFileName1 = "abc" and strOpenFileName2 = "abcd", even
though
> both strings are not zero length, the comparison will return False because
> when you do a bit mask of 3

What determines the "bit mask" i.e. in this case, what determines that the
bit mask in this case is 3?

Quote:
>... and 4 - 0000100

Why "(4 - )"?  and why 0000100 instead of 0000011?

Quote:
>no bits match

you mean that when the expression 4 - 0000100 is evaluated, the result is
not equivalent to 0000011?

The message did clear up the problem, thanks again Ian, and also Martin
Trump, for continued help

Cheers
Simon Yu



Tue, 30 Sep 2003 03:54:31 GMT  
 opening / saving files with MS rich text box control (viewing open files)
Simon

Here we go, Binary Arithmetic 101 :)

Data is stored in bytes, each byte is made up of 8 bits which represent the
powers of 2 from 0 to 7, therefore a single byte can represent numbers from
0 - 255, a 2 byte number, VB Integer, can hold, using only positive values,
numbers from 0 - 32767, a 4 byte number, VB Long, 0 - 2147483647.

Now using just Byte data, the bits are

4 = 00000100 = 2 ^ 2
3 = 00000011 = 2 ^ 0 + 2 ^ 1

With the bit representation you read from right to left, so the right most
digit is 2 ^ 0 and the left most digit is 2 ^ 7.

Using just binary, this can only have 1 of 2 values, either 0 or 1, when you
mask 2 numbers it can only return 1 if both bits are 1, so when you bit mask
3 And 4

00000100
00000011
------------------
00000000

To look at each bit pair, from left to right

0 And 0 = 0, 0 And 0 = 0, 0 And 0 = 0, 0 And 0 = 0
0 And 0 = 0, 1 And 0 = 0, 0 And 1 = 0, 0 And 1 = 0

If you bit mask 3 & 5

00000011
00000101
-----------------
00000001

0 And 0 = 0, 0 And 0 = 0, 0 And 0 = 0, 0 And 0 = 0
0 And 0 = 0, 1 And 0 = 0, 0 And 1 = 0, 1 And 1 = 1

A good demonstration of this is determining if a number is Odd or Even

Function IsEven(Num) As Boolean
IsEven = Num And 1
End Function

When doing the If Len(strOpenFileName) Then, what this is doing is

VB True = -1 which in Binary is 1111111111111111

On Windows 32 platforms the maximum length of the path is 259, so if you had
a path that was that long the binary string would be

000000100000011

which when masked against True gives

000000100000011
111111111111111
---------------------------------
000000100000011

which is not 0 and therefore Not False

Conversely, if a zero length string is returned then

0000000000000000
1111111111111111
-----------------------------------
0000000000000000

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/


Quote:
> Heh, for this part (well, one line of it) - "WHOOSH" :)

> > Now if you're checking 2 string lengths you do need to use the operator,
> > i.e.

> > If Len(strOpenFileName1) > 0 And Len(strOpenFileName2) > 0 Then

> > This is because of the way And works, it actually masks the bits of the
> > return values, so in the above we're checking for a bit comparison of 2
> > Boolean results, but if you write it as

> > If Len(strOpenFileName1) And Len(strOpenFileName2) Then

> > and use strOpenFileName1 = "abc" and strOpenFileName2 = "abcd", even
> though
> > both strings are not zero length, the comparison will return False
because
> > when you do a bit mask of 3

> What determines the "bit mask" i.e. in this case, what determines that the
> bit mask in this case is 3?

> >... and 4 - 0000100

> Why "(4 - )"?  and why 0000100 instead of 0000011?

> >no bits match
> you mean that when the expression 4 - 0000100 is evaluated, the result is
> not equivalent to 0000011?

> The message did clear up the problem, thanks again Ian, and also Martin
> Trump, for continued help

> Cheers
> Simon Yu



Tue, 30 Sep 2003 06:19:19 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Saving open file and opening new file at a specified time of day

2. Opening a text files for viewing, like a ReadMe file

3. Replacing the File Open/File Save As box?

4. save file as .txt or as .rtf in rich text box

5. Saving Rich Text Box Content to .RTF File

6. Saving Rich Text Box Content to .RTF file

7. save file as .txt or as .rtf in rich text box

8. Saving Rich Text Box to .RTF File

9. Opening text file in a text box using common dialog

10. opening 1 text file to multiple text boxes

11. opening text files into text boxes and more!

12. opening text files into text boxes and more!

 

 
Powered by phpBB® Forum Software