CodeItBetter Programming Another VB Programming Blog

How to add filters to a CommonDialog control

Posted on November 16, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'Controls - How to add filters to a CommonDialog control
'the CommonDialog 's Filter property is a string with the format:
'text1|value1|text2|value2...
'
'The text parts are displayed to the user. The values are used as the file 
'filters. Consider this example (all one line):
'Text Files (*.txt)|*.txt|Graphic Files (*.bmp;*.gif;*.jpg)|*.bmp;*.gif;*.jpg|All Files (*.*)|*.*
'
'This makes the control use these values:
'
'Text Value                          File Filter
'----------                          -----------
'Text Files (*.txt)                  *.txt
'Graphic Files (*.bmp;*.gif;*.jpg)   *.bmp;*.gif;*.jpg
'All Files (*.*)                     *.*
'
'This is a bit confusing.
'
'This example uses the AddFilter routine to make adding new filters to the property easy.
'
'Add a filter to the dialog in an easy way.

Private Sub AddFilter(ByVal dlg As CommonDialog, ByVal filter_title As String, _
    ByVal filter_value As String)
    Dim txt As String
 
    txt = dlg.Filter
    If Len(txt) > 0 Then txt = txt & "|"
    txt = txt & filter_title & " (" & filter_value & ")|" & filter_value
    dlg.Filter = txt
End Sub
 
Private Sub Form_Load()
    ' Load filters.
    CommonDialog1.Filter = ""
    AddFilter CommonDialog1, "Text Files", "*.txt"
    AddFilter CommonDialog1, "Graphic Files", "*.bmp;*.gif;*.jpg"
    AddFilter CommonDialog1, "All Files", "*.*"
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.