Results 1 to 8 of 8

Thread: [RESOLVED] listbox and commondialogue

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    214

    Resolved [RESOLVED] listbox and commondialogue

    hi friends;

    do you have any idea how i can save contents of list box by a common dialogue?

    I know how to di it when I have a rich text box;
    Code:
    With CommonDialog1 'use this because we will have a lot of code with the box
         .DialogTitle = "Open"  'this sets the caption for the title bar on the dialog
         .InitDir = "C:\"  'this sets the initial director for the dialog box
         .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
         'the filter property is what specifies which file types to display.  The
         '(*.txt) is not necessary.
         'as you can see, the file name comes first then the *.type comes
         'next after the |. This is important
    
         .ShowOpen  'this event actually displays the open dialog box
    End With  'Close the with statement
    
    RTB1.LoadFile CommonDialog1.FileName
    but I want to use list box.
    thanks

  2. #2
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: listbox and commondialogue

    Maybe this well do it.
    Code:
    Private Sub Command2_Click()
     Dim filenum As Integer, i As Long
       CommonDialog1.Filter = "Text File|*.txt"
       CommonDialog1.DefaultExt = ".txt"
       CommonDialog1.FileName = "*.txt"
       CommonDialog1.ShowSave
       FileName = CommonDialog1.FileName
       If CommonDialog1.CancelError = True Then Exit Sub 
       filenum = FreeFile
     On Error GoTo FileError
      Open FileName For Output As filenum
            For i = 0 To List1.ListCount - 1
                Print #filenum, List1.List(i)
            Next i
      Close filenum 
     Exit Sub
    FileError:
    MsgBox Err.Description, vbCritical, "Error Number: " &  Err.Number 
    Exit Sub
    
    End Sub
    Or you can call these with the common dialogue i.e
    Code:
    Private Sub Command2_Click()
     Dim filenum As Integer, i As Long
      CommonDialog1.Filter = "Text File|*.txt"
      CommonDialog1.DefaultExt = ".txt"
      CommonDialog1.FileName = "*.txt"
      CommonDialog1.ShowSave
      FileName = CommonDialog1.FileName
     If CommonDialog1.CancelError = True Then Exit Sub 
     On Error GoTo FileError
     SaveListBox "C:/TheList.txt", List1
    'OR
     Loadlistbox "C:/TheList.txt", List1
     Exit Sub
    FileError:
    MsgBox Err.Description, vbCritical, "Error Number: " &  Err.Number 
    End Sub
    
    Public Sub SaveListBox(theFile As String, TheList As ListBox)
       Dim i As Long, fNum As Integer
        On Error Resume Next
         fNum = FreeFile
        Open theFile$ For Output As #fNum
            For i = 0 To TheList.ListCount - 1
                Print #fNum, TheList.List(savelist&)
            Next i
        Close #fNum
    End Sub
    
    Public Sub Loadlistbox(theFile As String, TheList As ListBox)
       Dim MyString As String, fNum As Integer
        On Error Resume Next
         fNum = FreeFile
        Open theFile$ For Input As #fNum
            While Not EOF(1)
                Input #fNum, MyString$
                DoEvents
                TheList.AddItem MyString$
            Wend
        Close #fNum
    End Sub
    .

    The answer to your question is Here
    And here
    C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98

    Please go to the "Thread Tools" menu at the top of this Thread, and click "Mark Thread Resolved" when you have your answer.
    So I can fine the answer when I need it.

    how to stop the playsound when it is in loop..Play more than 1 sound at a time..
    ini file Check if IP changed Strings 'Split', 'Left' and 'Right'
    Save And Load an Array of list-boxes, to and from a file......list-box and CommonDialog
    Quote Originally Posted by RobDog888

    So please install VB6 service pack 6 as its the latest and last supported service pack MS will ever make.
    http://support.microsoft.com/kb/q198880/ I'm sure this will fix your issue
    The only reason some people get lost in thought is because it’s unfamiliar territory. —Paul Fix

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    214

    Re: listbox and commondialogue

    hi and thank you so muche ,your code helped me to save my items in .txt file but for loading a file I have still problem,can you explain more

  4. #4
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: listbox and commondialogue

    TipeOh!
    OK OK TipeOh (S)!
    There you go this we run, I'm not good at explaining, so if you run this with F8 you'll see.
    Attached Files Attached Files
    Last edited by 5ms?; Oct 2nd, 2011 at 02:54 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    214

    Re: listbox and commondialogue

    my question is,when I want to load a txt file and transfer its content to a listbox I have problem,
    look in your code you wrote

    CommonDialog1.ShowSave
    but I wanna open a txt file?

  6. #6
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: listbox and commondialogue

    Try
    CommonDialog1.ShowOpen

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    214

    Re: listbox and commondialogue

    thank u so much for your answers

  8. #8
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: [RESOLVED] listbox and commondialogue

    No prob!
    Here's a little more about the CommonDialog, from (MSDN)

    ShowColor, ShowFont, ShowHelp, ShowOpen, ShowPrinter, ShowSave Methods Example
    This example uses the CommonDialog control and the ShowColor, ShowFont, ShowHelp, ShowOpen, ShowPrinter, and ShowSave methods to display the common dialog boxes. To try this example, paste the code into the Declarations section of a form with CommandButton, OptionButton (set the option button's Index property to 0), and CommonDialog controls. Press F5 and select the option button for the common dialog box you want and choose the command button.
    Code:
    Private Sub Form_Paint ()
       Static FlagFormPainted As Integer
       ' When form is painting for first time,
       If FlagFormPainted <> True Then
          For i = 1 To 5
             Load Option1(i)   ' Add five option buttons to array.
             Option1(i).Top = Option1(i - 1).Top + 350
             Option1(i).Visible = True
          Next i
          Option1(0).Caption = "Open"   ' Put caption on each option button.
          Option1(1).Caption = "Save"
          Option1(2).Caption = "Color"
          Option1(3).Caption = "Font"
           Option1(4).Caption = "Printer"
          Option1(5).Caption = "Help"
          Command1.Caption = "Show Dlg"   ' Label command button.
          FlagFormPainted = True   ' Form is done painting.
       End If
    End Sub
    
    Private Sub Command1_Click ()
       If Option1(0).Value Then   ' If Open option button selected,
          CommonDialog1.ShowOpen   ' display Open common dialog box.
       ElseIf Option1(1).Value Then   ' Or,
          CommonDialog1.ShowSave   ' display Save common dialog box.
       ElseIf Option1(2).Value Then   ' Or,
          CommonDialog1.ShowColor   ' display Color common dialog box.
       ElseIf Option1(3).Value Then   ' Or,
          CommonDialog1.Flags = cdlCFBoth   ' Flags property must be set
                ' to cdlCFBoth,                      ' cdlCFPrinterFonts, 
                ' or cdlCFScreenFonts before                ' using ShowFont method.
          CommonDialog1.ShowFont   ' Display Font common dialog box.
       ElseIf Option1(4).Value Then   ' Or,
          CommonDialog1.ShowPrinter   ' display Printer common dialog box.
       ElseIf Option1(5).Value Then   ' Or,
          CommonDialog1.HelpFile = "VB5.hlp"
          CommonDialog1.HelpCommand = cdlHelpContents
          CommonDialog1.ShowHelp   ' Display Visual Basic Help contents topic.
          End If
    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