Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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
[1283 byte] By [ootnitsuj] at [2007-11-11 8:04:17]
# 1 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
>>>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..

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
Sync at 2007-11-11 17:26:02 >
# 2 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
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
ootnitsuj at 2007-11-11 17:27:01 >
# 3 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
Hi Guys,

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

Regards,
Justin
ootnitsuj at 2007-11-11 17:28:00 >
# 4 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
Hi, this will do the job:

...
cboHistory.AddItem .FileTitle
...
Benjamin at 2007-11-11 17:29:02 >
# 5 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
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
ootnitsuj at 2007-11-11 17:30:12 >
# 6 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
Hi,

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

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 at 2007-11-11 17:31:10 >
# 7 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
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.

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 at 2007-11-11 17:32:14 >
# 8 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
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
ootnitsuj at 2007-11-11 17:33:08 >
# 9 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
Hi,

that's the method you can use. (I use it)
Give it a try and let me know how it goes.
Benjamin at 2007-11-11 17:34:06 >
# 10 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
Hi Ben,
This is what I did...
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
ootnitsuj at 2007-11-11 17:35:17 >
# 11 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
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:

'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:

strTempFileName(iLimit) = .FileName
cboHistory.AddItem .FileTitle

Here is also a simplified version of your 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 at 2007-11-11 17:36:15 >
# 12 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
Ok,

here's an example.
Check the Vb-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 at 2007-11-11 17:37:20 >
# 13 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
If you want to execute a file (eg: *.doc), you have to store the path of each file.

Here is coding~

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?
Sync at 2007-11-11 17:38:15 >
# 14 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
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...:

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
ootnitsuj at 2007-11-11 17:39:21 >
# 15 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
Yes that's clear enough I think. So actually you can use all the code applied to 1 combobox also for the other(only other variables).
Benjamin at 2007-11-11 17:40:16 >
# 16 Re: How to update a ComboBox when I save an image using CommonDialog Save As?
This should be the entire code, it can be simplified but this is the easiest way to understand I think. The counters iLimit have to be declared outside the subs, because otherwise the counters start everytime you click counting from 0...

Option Explicit
Private strTempFileName1() As String 'History1
Private iLimit1 As Integer
Private strTempFileName2() As String 'History2
Private iLimit2 As Integer

Private Sub cboHistory1_Change()
If cboHistory1.ListIndex <> -1 Then MsgBox strTempFileName1(cboHistory1.ListIndex)
End Sub

Private Sub cboHistory2_Change()
If cboHistory2.ListIndex <> -1 Then MsgBox strTempFileName2(cboHistory2.ListIndex)
End Sub

Private Sub cmdSave1_Click()
With dlgCommonDialog1
.ShowSave
'To update History for the first 3 images saved
'Max of 3 entries in History
If iLimit1 = 3 And Trim(.FileTitle) <> "" Then
cboHistory1.RemoveItem 0 'remove the 1st item in History
cboHistory1.ListIndex = -1 'deselect item
iLimit1 = 2
End If

If Trim(.FileTitle) <> "" Then
strTempFileName1(iLimit1) = .Filename
cboHistory1.AddItem .FileTitle
iLimit1 = iLimit1 + 1
Exit Sub
End If
End With
End Sub

Private Sub cmdSave2_Click()
With dlgCommonDialog1
.ShowSave
'To update History for the first 3 images saved
'Max of 3 entries in History
If iLimit2 = 3 And Trim(.FileTitle) <> "" Then
cboHistory2.RemoveItem 0 'remove the 1st item in History
cboHistory2.ListIndex = -1 'deselect item
iLimit2 = 2
End If

If Trim(.FileTitle) <> "" Then
strTempFileName2(iLimit2) = .Filename
cboHistory2.AddItem .FileTitle
iLimit2 = iLimit2 + 1
Exit Sub
End If
End With
End Sub

Private Sub UserForm_Activate()
ReDim strTempFileName1(2) As String 'Initialize tabel
ReDim strTempFileName2(2) As String 'Initialize tabel
ilimit1 = 0
ilimit2=0
End Sub

My advice implement first the code of box1 and then of box2 => implementing and testing, piece by piece. I globally tested it but there can always be an error.

Hope you can solve it out.
Benjamin at 2007-11-11 17:41:20 >