How to update a ComboBox when I save an image using CommonDialog Save As??


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: How to update a ComboBox when I save an image using CommonDialog Save As??

  1. #1
    Join Date
    Jan 2006
    Posts
    82

    How to update a ComboBox when I save an image using CommonDialog Save As??

    Hi Guys,

    I need some help here and hope you guys may have some tips for me...here goes....:

    I have a cmdSave button and a cmdLoad button. I also have a cboHistory ComboBox and an image control. I have a dlgCommonDialog in my form too.

    So whenever I clicked on cmdSave, the Save As dialog box will open up. When I clicked on the save button on the Save As dialog box, the image displayed on that particular image control will be saved, with the user typing in their desired file name and folder.

    What I need help in is:
    1) When this image is saved, I need the cboHistory to be updated with the file name of the image that I have just saved. Is it possible?

    2) And whenever I clicked on the file name in the cboHistory, the picture will be loaded like a normal Load dialog box, displaying the image on the image control.

    So the whole idea is something like our common MS Documents histroy (accessible from the Start|Documents menu), whereby whenever we open a document, it will automatically keep a list of recently opened documents.

    Can somebody help me please.....I really need this badly...I hope you guys can help me out with this...

    thank you guys in advance for your help...

    Regards,
    Justin

  2. #2
    Join Date
    Dec 2004
    Posts
    717
    >>>1) When this image is saved, I need the cboHistory to be updated with the file name >>>of the image that I have just saved. Is it possible?

    Yeah. It's possible.

    >>2) And whenever I clicked on the file name in the cboHistory, the picture will be >>loaded like a normal Load dialog box, displaying the image on the image control.

    Check it out..
    Code:
    Private Sub cboHistory_Click()
       Picture1.Picture = LoadPicture(cboHistory.Text)
    End Sub
    Private Sub cmdSave_Click()
     With dlgCommonDialog
        .ShowSave
        If Trim(.FileName) <> "" Then
          cboHistory.AddItem .FileName
          Picture1.Picture = LoadPicture(.FileName)
          End If
     End With
    End Sub
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  3. #3
    Join Date
    Jan 2006
    Posts
    82
    Hello Mic,

    Thanks alot for your help...Can u just help me with another problem...what if I just want the file name to be loaded into the cboHistory??

    Let say we have the full image path as c:\Good Images\Image 1. So when i clicked cmdSave, I only want the filename Image 1 to be loaded into the cboHistory only.

    I realised the method that you have provided gives the whole address of the image location.

    Thanks a million man....you rock!!

    Cheers,
    Justin

  4. #4
    Join Date
    Jan 2006
    Posts
    82
    Hi Guys,

    I hope somebody can help me with this....thanks a million...i really need it urgently...

    Regards,
    Justin

  5. #5
    Join Date
    Feb 2006
    Posts
    133
    Hi, this will do the job:

    Code:
    ...
    cboHistory.AddItem .FileTitle
    ...
    Benjamin

  6. #6
    Join Date
    Jan 2006
    Posts
    82
    Hi Ben,

    Thank you for your help..I notice that if i were to continue adding the items into the combobox, the list will just go on and on...even if i open the same file again, it will still add it into the combobox...

    Is there any way whereby i can limit the number of files in the History combobox? also, how is it possible to check that if that file has been opened before so that i can load the latest one into the combobox instead of having mutiple entries of the same file...

    Thanks for your help once again...

    Justin

  7. #7
    Join Date
    Feb 2006
    Posts
    133
    Hi,

    this you can use to check how much items in the combobox are:

    Code:
    If cboHistory.ListCount = 5 Then MsgBox "No more then 5 items alowed"
    You can use an exit sub instead of the message box or both of course.
    I'll post back to you to loop trough the cbo.
    Benjamin

  8. #8
    Join Date
    Feb 2006
    Posts
    133
    Here 's the code the check if the filetitle already exists. 1 remark, you want to overwrite the old one? How can you do that if the fileTitle is the same, the path is nowhere stored.

    Code:
    Dim intIndex        As Integer
        With dlgCommonDialog
        .ShowSave
        If Trim(.Filename) <> "" Then
            Do While intIndex < cboHistory.ListCount
                cboHistory.ListIndex = intIndex
                If cboHistory.Text = .FileTitle Then
                    MsgBox "File already exists in the box"
                    Exit Sub
                End If
             intIndex = intIndex + 1
            Loop
            cboHistory.AddItem .FileTitle
            Picture1.Picture = LoadPicture(.Filename)
        End If
        End With
    Benjamin

  9. #9
    Join Date
    Jan 2006
    Posts
    82
    Hi Ben,

    Yes, I wanna overwrite it...Can we use array...?? As in we assign the filename to the string array, and we write the array string into the combobox insted of writing the filename directly into the combobox...

    So when the limit is reached, we simply overwrite the previously stored array with the filename, remove the combobox item, then write the new array string into the combobox...is this feasible?

    I am not sure i am clear enough but the general idea is something like that...

    Justin

  10. #10
    Join Date
    Feb 2006
    Posts
    133
    Hi,

    that's the method you can use. (I use it)
    Give it a try and let me know how it goes.
    Benjamin

  11. #11
    Join Date
    Jan 2006
    Posts
    82
    Hi Ben,
    This is what I did...
    Code:
    Private Sub cmdSave_Click()
    
        Static strTempFileName(0 To 20) As String   'array to store filename temp
        Static iLimit As Integer    'to indicate the max number of files
        
    With dlgCommonDialog1
        .ShowSave
        
            'To update History for the first 3 images saved
            If iLimit < 3 Then          'Max of 3 entries in History
                If Trim(.FileTitle) <> "" Then
                    strTempFileName(iLimit) = .FileTitle
                    cboHistory.AddItem strTempFileName(iLimit)
                    iLimit = iLimit + 1
                    Exit Sub
                End If
            'when max History entries reached
            Else
                If Trim(.FileTitle) <> "" Then
                    cboHistory.RemoveItem 0 'remove the 1st item in History
                    strTempFileName(iLimit) = .FileTitle 'assign image file title to array
                    cboHistory.AddItem strTempFileName(iLimit)
                    iLimit = iLimit + 1
                    Exit Sub
                End If
            End If
    End With
        
    End Sub
    After running the app, I realise a bug...this works only if there is only 1 combo box...what i mean is this:
    lets say we have 2 history combox, cboHistory1 and cboHistory2...when i save doc1 to cboHistory1, the FileTitle of doc1 will be updated into cboHistory1...and since there is only 1 active path, when i clicked on the FileTitle in cboHistory1, it is able to launch the file...

    But lets say I have cboHistory2, when i save doc1, cboHistory1 will be updated, and then when I save a 2nd file, doc2, cboHistory2 will be updated...In this case, when I try to open doc1 from cboHistory1 again, I have an error saying "Attempt to open specific file failed" with an runtime error "2016"...

    So now, I guess the error is that FileTitle does not gives the path right so this error occurs is it? Can someone help me with this?? Ben, any thoughts??

    Regards,
    Justin

  12. #12
    Join Date
    Feb 2006
    Posts
    133
    Hi Justin,

    first of all nice work.

    If I see the code I've 1 remark to start with, are you sure the file opens when clicking the cboHistorybox? The problem is here:

    Code:
    'Adding filename to table
    strTempFileName(iLimit) = .FileTitle 
    'Adding filename to box
    cboHistory.AddItem strTempFileName(iLimit)
    You have to store the path in the table and the name in the box:

    Code:
    strTempFileName(iLimit) = .FileName
    cboHistory.AddItem .FileTitle
    Here is also a simplified version of your code:

    Code:
    Static strTempFileName(0 To 20) As String   'array to store filename temp
        Static iLimit As Integer    'to indicate the max number of files
        
        With dlgCommonDialog1
        .ShowSave
        'To update History for the first 3 images saved
            'Max of 3 entries in History
            If iLimit = 3 And Trim(.FileTitle) <> "" Then
                cboHistory.RemoveItem 0 'remove the 1st item in History
            End If
        
            If Trim(.FileTitle) <> "" Then
                strTempFileName(iLimit) = .FileName
                cboHistory.AddItem .FileTitle
                iLimit = iLimit + 1
                Exit Sub
            End If
        End With

    The problem of 2 history-boxes we can solve with two arrays or a multidimensional array. I'll post back to you when I have a small example.
    Benjamin

  13. #13
    Join Date
    Feb 2006
    Posts
    133

    Post

    Ok,

    here's an example.
    Check the Vb-code:
    Code:
    Option Explicit
    Private strTempFileName() As String    'array to store filename temp
    
    Private Sub cboHistory_Change()
        If cboHistory.ListIndex <> -1 Then MsgBox strTempFileName(cboHistory.ListIndex)
    End Sub
    
    Private Sub cmdSave_Click()
        Static iLimit As Integer    'to indicate the max number of files
        
        With dlgCommonDialog1
        .ShowSave
        'To update History for the first 3 images saved
            'Max of 3 entries in History
            If iLimit = 3 And Trim(.FileTitle) <> "" Then
                cboHistory.RemoveItem 0 'remove the 1st item in History
                cboHistory.ListIndex = -1  'deselect item
                iLimit = 2
            End If
        
            If Trim(.FileTitle) <> "" Then
                strTempFileName(iLimit) = .Filename
                cboHistory.AddItem .FileTitle
                iLimit = iLimit + 1
                Exit Sub
            End If
        End With
    End Sub
    
    Private Sub Form_Load()
        ReDim strTempFileName(2) As String 'Initialize tabel
    End Sub
    The greatest adaptations:
    Your table has to be defined on top of the module so you can call the values stored in it from the cboHistory_Change event.
    Your index iLimit has to be set to 2 when deleting an item, otherwise your index keeps growing and you won't be able to track your paths.

    Ok now your problem with 2 boxes: it depends on what the program has to do of course.

    Situation 1: 1ste save -> History1
    2nd save -> History2
    3rd save -> History1
    4th save -> History2
    5th Save -> History1
    6th Save -> History2?

    Situation2: 1-3 -> History1
    4-6 -> History2

    What to do when boxes are full => removeitem history1 or 2?
    ....

    A lot of questions, so you have to be more specific what the idea is or what the program has to do.
    Benjamin

  14. #14
    Join Date
    Dec 2004
    Posts
    717
    If you want to execute a file (eg: *.doc), you have to store the path of each file.

    Here is coding~

    Code:
    Option Explicit
    ''Constant
    Const intNoOfAllowListItems = 3
    
    ''API
    Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
    String, ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
     
    Dim strHistoryPath(1 To intNoOfAllowListItems) As String
    Private Sub cboHistory_Click()
      If cboHistory.ListCount > 0 Then
         DoEvents
         ''Normal Shell function is not working here. ''Err: Invalid procedure call or argument
         ''Call Shell("D:\phone.txt", vbNormalFocus) ''strHistoryPath(cboHistory.ListIndex)
         Call Shell(strHistoryPath(cboHistory.ListIndex + 1)) ''
      End If
    End Sub
    Private Sub cmdSave_Click()
        With dlgCommonDialog1
            .ShowSave
            
            Dim intIndex As Integer
            ''Checking existing items
            Do While intIndex < cboHistory.ListCount
                If cboHistory.List(intIndex) = .FileTitle Then
                    Exit Sub
                End If
             intIndex = intIndex + 1
            Loop
            
            If cboHistory.ListCount >= intNoOfAllowListItems Then _
               cboHistory.RemoveItem 0 'remove the 1st item in History
                
            cboHistory.AddItem .FileTitle
            
            Dim str As String
            Dim strLastString As String
            strLastString = ""
            Dim i As Integer
            For i = intNoOfAllowListItems To 1 Step -1
              If i > 1 Then
                 strHistoryPath(i) = strHistoryPath(i - 1)
              Else
                 strHistoryPath(i) = .FileName
              End If
            Next
        End With
    End Sub
    '#############################################################
    '# This code was written by Emmett Dixson (c)1999.
    '  Ref : Alternative to VB's Shell Function
    '        http://freevbcode.com/ShowCode.asp?ID=264
    '#############################################################
    Function Shell(Program As String, Optional ShowCmd As Long = _
    vbNormalNoFocus, Optional ByVal WorkDir As Variant) As Long
    
        Dim FirstSpace As Integer, Slash As Integer
    
        If Left(Program, 1) = """" Then
            FirstSpace = InStr(2, Program, """")
    
    
            If FirstSpace <> 0 Then
                Program = Mid(Program, 2, FirstSpace - 2) & _
                  Mid(Program, FirstSpace + 1)
                FirstSpace = FirstSpace - 1
            End If
    
        Else
            FirstSpace = InStr(Program, " ")
        End If
    
        If FirstSpace = 0 Then FirstSpace = Len(Program) + 1
    
        If IsMissing(WorkDir) Then
    
            For Slash = FirstSpace - 1 To 1 Step -1
                If Mid(Program, Slash, 1) = "\" Then Exit For
            Next
    
            If Slash = 0 Then
                WorkDir = CurDir
            ElseIf Slash = 1 Or Mid(Program, Slash - 1, 1) = ":" Then
                WorkDir = Left(Program, Slash)
            Else
                WorkDir = Left(Program, Slash - 1)
            End If
    
        End If
    
        Shell = ShellExecute(0, vbNullString, _
        Left(Program, FirstSpace - 1), LTrim(Mid(Program, _
        FirstSpace)), WorkDir, ShowCmd)
        If Shell < 32 Then VBA.Shell Program, ShowCmd 'To raise Error
    End Function
    I'm not sure I get you for two combo boxes.
    Why do you want to use?
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  15. #15
    Join Date
    Jan 2006
    Posts
    82
    thanks ben, you have been of great help...

    Okie here goes:
    2 combobox, cboHistory1 and cboHistory2...
    2 save buttons, cmdSave1 and cmdSave2...

    Whenever i click cmdSave1, Save As dialog pops up and when I click save, the file, say doc1 (this is the FileTitle), will be updated into cboHistory1 as doc1..the same goes for 2nd and 3rd file that i am going to save...

    This applies to cmdSave2 and cboHistory2...

    So lets say the max entries both history combobox can hold is 3, so whenever we save the 4th file, the FileTitle of doc 4 will overwrite doc 1... <---clear so far?? i hope i am...

    So the problem now is that since only the FileTitle was loaded into combobox, it does not contain the full path to the FileTitle that was loaded into the combobox...

    So what i need now is:
    Is it possible to implement the full path whenever i click on the FileTitle in the combobox...So when i click on the any of the FileTitle in cboHistory1, it only works when I save the last file using cmdSave1...if the last save was from cmdSave2, there will be a loading error...

    hahaha...understand so far...sorry for being so naggy...hope that its clear....

    I forgot to put in the cboHistory click event previously...so here goes...:

    Code:
    Private Sub cboHistory1_Click()
        'ctlImageDevice is just some control to display images...just like a PictureBox
        ctlImageDevice1.ActiveScene = "GoldenBoard"
        ctlImageDevice1.LoadImage cboGBHistory.Text
        ctlImageDevice1.DisplayScene = "GoldenBoard" 
    End Sub
    Last edited by ootnitsuj; 03-08-2006 at 06:02 AM.

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2005, 09:05 AM
  2. Replies: 1
    Last Post: 12-09-2005, 09:12 AM
  3. upload an image file and save on webserver.
    By sandhyaharsh in forum Java
    Replies: 0
    Last Post: 11-15-2005, 07:23 AM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 12:45 PM
  5. How can I save a Image to disk
    By Abel Castellanos in forum Java
    Replies: 0
    Last Post: 01-06-2001, 12:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center