in the below coding the files selected in the dialog box are dispalyed in the list-box, but while selecting the format i need to display only the selected format files to be displayed , how to modify the coding to keep like that.

Imports System.IO
Public Class Form1

    Dim fdlg As OpenFileDialog = New OpenFileDialog()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        fdlg.Title = "open"
        fdlg.InitialDirectory = "c:\"
        fdlg.Filter = "document files(*.RTF)|*.RTF|document files(*.doc)|*.doc"
        fdlg.FilterIndex = 1
        fdlg.RestoreDirectory = True
        If fdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListBox1.Items.Add(Path.GetFileName(fdlg.FileName))
            'MessageBox.Show("You selected " & fdlg.FileName)
        End If
    End Sub
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim item As Object = ListBox1.SelectedItem
        If Not item Is Nothing Then
            Dim index As Integer = ListBox1.Items.IndexOf(item)
            If index < ListBox1.Items.Count - 1 Then
                ListBox1.Items.RemoveAt(index)
                index += 1
                ListBox1.Items.Insert(index, item)
                ListBox1.SelectedIndex = index
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim item As Object = ListBox1.SelectedItem
        If Not item Is Nothing Then
            Dim index As Integer = ListBox1.Items.IndexOf(item)
            If index <> 0 Then
                ListBox1.Items.RemoveAt(index)
                index -= 1
                ListBox1.Items.Insert(index, item)
                ListBox1.SelectedIndex = index
            End If
        End If
    End Sub
End Class

Recommended Answers

All 2 Replies

I'm not sure what you mean by this, but maybe this will help?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fDLG As New OpenFileDialog
        fdlg.Title = "open"
        fdlg.InitialDirectory = "c:\"
        fdlg.Filter = "document files(*.RTF)|*.RTF|document files(*.doc)|*.doc"
        fdlg.FilterIndex = 1
        fdlg.RestoreDirectory = True
        'User Didn't Cancel?
        If fDLG.ShowDialog = Windows.Forms.DialogResult.OK Then

            'Get Filename...
            Dim sFileName As String = IO.Path.GetFileName(fDLG.FileName)

            'Which FilterIndex was chosen?
            Select Case fDLG.FilterIndex

                Case 1 ' RTF FilterIndex was chosen
                    'if file extension matches this filterindex then add it!
                    If LCase(Strings.Right(sFileName, 3)) = "rtf" Then ListBox1.Items.Add(sFileName)

                Case 2 ' DOC FilterIndex was chosen
                    'if file extension matches this filterindex then add it!
                    If LCase(Strings.Right(sFileName, 3)) = "doc" Then ListBox1.Items.Add(sFileName)

            End Select
        End If
    End Sub

I'm not sure how you could add a file with another extension in the first place, but this IS one way of making sure its not possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.