Les Snippets

Connexion

Boite de dialogue pour choisir un Fichier

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 08/12/2007 14:25:54 et initié par us_30 [Liste]
Date de mise à jour : 20/01/2009 02:16:10
Vue : 10871
Catégorie(s) : Fichier / Disque
Langages dispo pour ce code :
- VBA
- VB6, VBA
- VB 2005
- C# 2.x
- VB6
- VBScript



Langage : VBA
Date ajout : 08/12/2007
Posté par us_30 [Liste]
DateMAJ : 08/12/2007
Function FileOpen(Optional ByVal sTitle As String = "Choisir le(s)  fichier(s)"Optional ByVal bAllowMultiSelect As Boolean  = True, Optional ByVal sFiltreName As String = "Images"Optional ByVal sFiltreContent As String = "*.bmp; *.gif; *.jpg; *.jpeg;  *.png"As String
' CHOIX D'UN FICHIER PAR  VBA
    
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    Dim vrtSelectedItem As Variant
    With fd
        .Title = sTitle                   'Titre
        .AllowMultiSelect = bAllowMultiSelect 'Choix multiples
        .Filters.Add sFiltreName, sFiltreContent, 1 'Filtre image
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
                FileOpen = Trim(FileOpen & "|" & vrtSelectedItem)
            Next vrtSelectedItem
        Else
            'Bouton  Annuler
        End If
    End With
    Set fd = Nothing
End Function

Langage : VB6 , VBA
Date ajout : 15/12/2007
Posté par us_30 [Liste]
DateMAJ : 26/12/2007
Function FileOpen(oCD As CommonDialog, Optional ByVal  sTitle As String  = "Choix du  fichier", _
                  Optional ByVal sFiltreContent  As String = "*.*"As String
' CHOIX D'UN FICHIER pour  VB6, VBA
' mettre un contrôle "CommonDialog1" sur  une feuille (ici Form1)
With oCD
    .FileName = ""          'Efface,(re)met un  fichier
    .Flags = cdlOFNHideReadOnly + cdlOFNAllowMultiselect +  cdlOFNExplorer + cdlOFNFileMustExist
    .DialogTitle = sTitle
    .Filter = sFiltreContent
    .ShowOpen               'ouverture
    FileOpen = .FileName    'récupère le  nom
End With
End Function

Private Sub Command1_Click()     MsgBox FileOpen(Form1.CommonDialog1) End Sub
Remarque :
Il faut disposer du contrôle : "Microsft Common Dialog Control"
Langage : VB 2005
Date ajout : 07/11/2008
Posté par jrivet [Liste]
DateMAJ : 12/11/2008
   Public Function FileOpen(ByVal sTitle As String, ByVal sFiltre As String) As String
        Dim As New System.Windows.Forms.OpenFileDialog
        Dim As String = String.Empty
        With F
            .Title = sTitle
            .Filter = sFiltre
            .ShowDialog()
            S = .FileName
        End With
        F.Dispose()
        F = Nothing
        Return S
    End Function

'exemple  d'utilisation :
Dim ShosenFile As String = FileOpen("Choix Fichier""All files (*.*)|*.*")

Remarque :
Version .NET trouver pour cette discution.
http://www.vbfrance.com/forum/sujet-QUESTION-SUR-MESSAGE-BOX_1225760.aspx#1
Langage : C# 2.x
Date ajout : 12/11/2008
Posté par jrivet [Liste]
       
/// <summary>
/// Boite de dialogue pour choisir un OU plusieur fichier
/// </summary>
/// <param name="Title">Titre de la boite de dialogue</param>
/// <param name="Filter">Filtres actif de la boite de dialogue</param>
/// <param name="AllowMultiSelection">Autorise ou non la multiselection de fichiers</param>
/// <returns>Un tableau de string ave cle nom du ou des fichiers sélectionnés</returns>
public string[] FileOpen(string Title, string Filter, bool AllowMultiSelection)
{
    string[] result;
    using (System.Windows.Forms.OpenFileDialog F = new System.Windows.Forms.OpenFileDialog())
    {
        F.Title = Title;
        F.Filter = Filter;
        F.Multiselect = AllowMultiSelection;
        F.ShowDialog();
        if (AllowMultiSelection)
        {
            result = F.FileNames;
        }
        else
        {
            result = new string[] { F.FileName };
        }
     }
    return result;
}
//Exemple d'utilisation
string[] Ret = FileOpen("Chosir plusieur Fichiers", "txt files (*.txt)|*.txt|All files (*.*)|*.*", true);
foreach (string t in Ret)
{
    MessageBox.Show(t);
}
 
Remarque :
Petite version C# améliorée. Avec cette version il est possible d'effectuer la sélection de plusieurs fichiers. On notera l'utilisation de l'instruction using qui dixit MSDN "permet au programmeur de spécifier le moment où les objets qui utilisent des ressources doivent les libérer"
Langage : VB6
Date ajout : 12/11/2008
Posté par Charles Racaud [Liste]
Private Const MAX_PATH = 260
Private Const OFN_ALLOWMULTISELECT = &H200
Private Const OFN_EXPLORER = &H80000
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_READONLY = &H1
Private Type OpenFileName
  lStructSize As Long
  hWndOwner As Long
  hInstance As Long
  lpstrFilter As String
  lpstrCustomFilter As String
  nMaxCustFilter As Long
  nFilterIndex As Long
  lpstrFile As String
  nMaxFile As Long
  lpstrFileTitle As String
  nMaxFileTitle As Long
  lpstrInitialDir As String
  lpstrTitle As String
  flags As Long
  nFileOffset As Integer
  nFileExtension As Integer
  lpstrDefExt As String
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
End Type
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As LongByVal dwFlags As LongByVal lpMultiByteStr As StringByVal cchMultiByte As LongByVal lpWideCharStr As StringByVal cchWideChar As LongAs Long
Private Declare Function GetOpenFileNameW Lib "comdlg32.dll" (pOpenfilename As OpenFileName) As Boolean
Private Function GetOpenFileName(ByVal Title As String, _
                                 Files() As String, _
                                 Optional ByVal Filter As String = vbNullString, _
                                 Optional ByVal FilterIndex As Long = 0, _
                                 Optional ByVal InitialDir As String = vbNullString, _
                                 Optional ByVal MultiSelect As Boolean = False, _
                                 Optional ByVal FileMustExist As Boolean = False, _
                                 Optional ByVal HideReadOnly As Boolean = False, _
                                 Optional ByRef OpenReadOnly As Boolean = False, _
                                 Optional ByVal hWndOwner As Long = 0 _
                                 ) As Boolean
  Dim op As OpenFileName
  op.lStructSize = LenB(op)
  op.hWndOwner = hWndOwner
  op.hInstance = App.hInstance
  op.lpstrTitle = String$(2560)
  Call MultiByteToWideChar(00, Title, Len(Title), op.lpstrTitle, 256)
  op.lpstrInitialDir = String$(MAX_PATH, 0)
  Call MultiByteToWideChar(00, InitialDir, Len(InitialDir), op.lpstrInitialDir, MAX_PATH)
  op.lpstrFilter = String$(2560)
  Filter = VBA.Replace(Filter, "|", ChrW$(0))
  Call MultiByteToWideChar(00, Filter, Len(Filter), op.lpstrFilter, MAX_PATH)
  op.nFilterIndex = FilterIndex + 1
  op.nMaxFile = 4096
  op.lpstrFile = String$(op.nMaxFile, 0)
  If MultiSelect Then op.flags = op.flags Or OFN_ALLOWMULTISELECT Or OFN_EXPLORER
  If FileMustExist Then op.flags = op.flags Or OFN_FILEMUSTEXIST
  If HideReadOnly Then op.flags = op.flags Or OFN_HIDEREADONLY
  If OpenReadOnly Then op.flags = op.flags Or OFN_READONLY
  If GetOpenFileNameW(op) Then
    Dim OpenFileName As String
    OpenFileName = VBA.StrConv(op.lpstrFile, vbFromUnicode)
    Erase Files
    If MultiSelect Then
      Dim BufferFiles() As String, i As Integer
      BufferFiles = VBA.Split(OpenFileName, ChrW$(0))
      OpenFileName = vbNullString
      For i = 0 To UBound(BufferFiles)
        If BufferFiles(i) = vbNullString Then Exit For
        If i = 0 And BufferFiles(1= vbNullString Then
          OpenFileName = BufferFiles(0)
          Exit For
        ElseIf i > 0 Then
          If Not OpenFileName = vbNullString Then
            OpenFileName = OpenFileName & ChrW$(0& BufferFiles(0& BufferFiles(i)
          Else
            OpenFileName = BufferFiles(0& BufferFiles(i)
          End If
        Else
          If Not VBA.Right$(BufferFiles(0), 1= "\" Then BufferFiles(0= BufferFiles(0& "\"
        End If
      Next i
      Files = VBA.Split(OpenFileName, ChrW$(0))
    Else
      ReDim Files(0)
      Files(0= VBA.Left$(OpenFileName, VBA.InStr(1, OpenFileName, ChrW$(0)) - 1)
    End If
    OpenReadOnly = op.flags And OFN_READONLY
    GetOpenFileName = True
  Else
    GetOpenFileName = False
  End If
End Function
'Exemple d'utilisation :
Dim Files() As String
If GetOpenFileName("Chosir plusieur Fichiers", Files, "txt files (*.txt)|*.txt|All files (*.*)|*.*"1&"C:\"TrueTrueThen
  ' Ok
Else
  ' Annuler
End If
Remarque :
Méthode en utilisant les APIs
Langage : VBScript
Date ajout : 20/01/2009
Posté par PCPT [Liste]
DateMAJ : 20/01/2009
Function GetOpenFileName()
    Dim oCD
    Set oCD = CreateObject("UserAccounts.CommonDialog")
    If oCD.ShowOpen = -1 Then
        GetOpenFileName = oCD.FileName
    Else
        GetOpenFileName = ""
    End If
    Set oCD = Nothing
End Function


Snippets en rapport avec : Fichier, Boite, Choisir, Filedialog



Codes sources en rapport avec : Fichier, Boite, Choisir, Filedialog

{Visual Basic, VB6, VB.NET, VB 2005} PETITE BOITE DE DIALOGUE POUR LA MANIPULATION DE FICHIER
Ceci un code générant une petite boite de dialogue pour la manipulation de fichier... Cette boite n...

{C / C++ / C++.NET} EDITER UN FICHIER BIT PAR BIT
Bonjout, J'ai récemment eu besoin d'éditer un fichier bit à bit mais ne trouvant pas de moyen de ...

{PHP} CHARGER DES DONNÉES DEPUIS UN FICHIER TXT DANS UNE BASE DE DONNÉE
le titre dit tout dejàs ce script utilise une base de données Mysql les requêtes pour la création ...

{Visual Basic, VB6, VB.NET, VB 2005} INSERER TOUT TYPE DE FICHIERS DANS ORACLE EN VB.NET
Ce petit code permet d'ajouter tout type de fichiers dans oracle et par la suite de les récupérer, l...

{C / C++ / C++.NET} FICHIER ALBUM MUSICAL
.............................................................................creer un fichier conten...

{PHP} CLASSE SIMPLE DE GESTION DE FICHIERS
Bonjour, Dans le cadre d'un projet lié à mon lycée, j'ai dû développer un certain nombre de class...

{Visual Basic, VB6, VB.NET, VB 2005} CAPTURE ET ENREGISTREMENT D'UNE IMAGE DEPUIS UNE WEBCAM
Ce code permet de capturer une photo à partir d'un flux WebCam avec la technologie ActiveX. Inspiré ...

{Visual Basic, VB6, VB.NET, VB 2005} OUVRIR TOUS LES FICHIERS EXCEL D'UN DOSSIER ET DE SES SOUS-DOSSIERS
Petite macro permettant de choisir un dossier puis de parcourir le dossiers et ses sous-dossiers afi...

{Delphi} INTRA MESSENGER - DELPHI
Bonjour, je me suis décidé a vous transmettre un programme en cours de développement. Ce programme ...

{C# / C#.NET} RECHERCHE ET GESTION DE FICHIERS PERSONNALISÉES
FileManager permet de rechercher des fichiers d'un certain type défini dans le fichier de config, mo...