Results 1 to 5 of 5

Thread: Just a newbie needing some help with the common dialog

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    2

    Question

    I have just started VB programming, and I got a book to help me, it gives me lessons on how to do things...

    One of the programs it is helping me make is a word process. Its just a simple one using buttons and a text box. It told me to put a CommonDialog box into it as well.

    The problem is, in the Open button the code they asked me to put in is this:

    Private Sub cmdOpen_Click()

    CommonDialog1.ShowOpen
    If CommonDialog1.FileName <> "" Then Text1.Text = CommonDialog1.FileName

    End Sub

    When i tried it, it just put the filepath into the textbox, how do i fix this.

    I am completely new to VB (started yesterday), and so I really would appreciate any help
    Thanx
    Diablo_2097

  2. #2
    Member
    Join Date
    Jul 2000
    Location
    BFE in Oregon
    Posts
    33

    Cool

    When the Dialog comes up you have to select a file, otherwise all you'll get is the path that was either set in code or navigated to by the user (you).
    Glacius Cool
    Concept Designer
    VB5sp4,VC++6sp3

  3. #3
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    Actually, that is exactly what that code does..

    The common dialog only gives you the name of the file, it does not actually open the file...

    Code:
    Dim intFileHandle as Integer
    Dim intFileName as String
    Dim strBuffer as String
    
    CommonDialog1.ShowOpen 
    If CommonDialog1.FileName <> "" Then 
        strFileName = CommonDialog1.FileName
    Else
        Exit Sub
    End If
    
    intFileHandle = FreeFile(0)
    Open strFileName For Read As #intFileHandle
    
    Do Until EOF(intFileHandle)
       Line Input #iErrorHandle, strBuffer
       Text1.Text = Text1.Text & strBuffer & Chr(13) & Chr(10)
    Loop
    
    Close #intFileHandle
    Although this would work, it is probably better to replace the textbox with a Richtextbox then you can use the .LoadFile method to easily load the contents of a file into it...

    oOOo--oOOo
    __/\/\onte96
    oOOo--oOOo
    Senior Programmer/Analyst
    MCP
    [email protected]
    [email protected]


    Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    2

    Unhappy

    Thanx Glacius, but what do you mean?? when i click on the open button, then click on a text file, it puts the filepath only in the text box, it doesnt load the contents.

    Monte, so there isn't an easy way to open a file without a rich text box is there?? What is this??

    Thanx i'm really appreciating the replys

  5. #5
    Guest
    Try this:

    Code:
    Private Sub Command1_Click()
    
    Dim intFileHandle as Integer
    Dim strFileName as String
    CommonDialog1.CancelError = True
    CommonDialog1.ShowOpen 
    If CommonDialog1.FileName <> "" Then 
        strFileName = CommonDialog1.FileName
    Else
        Exit Sub
    End If
    
    intFileHandle = FreeFile
    Open strFileName for Input as #intFileHandle
    Text1.text = Input(LOF(1), intFileHandle)
    Close #intFileHandle
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width