Combobox et fichier texte [RESOLU]

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
MetalOS
Messages : 1510
Inscription : mar. 20/juin/2006 22:17
Localisation : Lorraine
Contact :

Combobox et fichier texte [RESOLU]

Message par MetalOS »

Salut tous le monde, pour un de mes projets je doit charger le contenu d'un fichier texte dans un ComboBoxGadget. Jusque la, pas vraiment de problème j'utilise ce code.

Code : Tout sélectionner

If OpenFile(0, "Type.txt")
     While Eof(0) = 0
        Texte$ = ReadString(0)
         AddGadgetItem(#Gadget_Form1_ComboBox2, 0, Texte$)
      Wend
         CloseFile(0)
      Else
         MessageRequester("Information","Impossible d'ouvrir le fichier Type.txt!") 
EndIf  
Voici le contenu du fichier texte:

Code : Tout sélectionner

Type 1
Type 2
Type 3
Type 4
Mais le problème est que quand je clique plusieurs fois sur le ComboBoxGadget, son contenu augmente de plus en plus.

Voici le code complet :

Code : Tout sélectionner

;- Global Variables and Constants
Global BubbleTipStyle.l:BubbleTipStyle=0

;- Window Constants
Enumeration 1
  #Window_Form1
EndEnumeration
#WindowIndex=#PB_Compiler_EnumerationValue


;- Gadget Constants
Enumeration 1
  ;Window_Form1
  #Gadget_Form1_ComboBox2


EndEnumeration
#GadgetIndex=#PB_Compiler_EnumerationValue


Procedure.l Window_Form1()
  If OpenWindow(#Window_Form1,80,80,259,65,"Work Form1",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      ComboBoxGadget(#Gadget_Form1_ComboBox2,10,20,240,20)
      HideWindow(#Window_Form1,0)
      ProcedureReturn WindowID(#Window_Form1)
  EndIf
EndProcedure

;- Main Loop
If Window_Form1()

  quitForm1=0
  Repeat
    EventID  =WaitWindowEvent()
    MenuID   =EventMenu()
    GadgetID =EventGadget()
    WindowID =EventWindow()

    Select EventID
      Case #PB_Event_CloseWindow
        If WindowID=#Window_Form1
          quitForm1=1
        EndIf


      Case #PB_Event_Gadget
        Select GadgetID
          Case #Gadget_Form1_ComboBox2
             If OpenFile(0, "Type.txt")
               While Eof(0) = 0
                Texte$ = ReadString(0)
                AddGadgetItem(#Gadget_Form1_ComboBox2, 0, Texte$)
               Wend
                CloseFile(0)
               Else
                MessageRequester("Information","Impossible d'ouvrir le fichier Type.txt!") 
           EndIf  
        EndSelect

    EndSelect
  Until quitForm1
  CloseWindow(#Window_Form1)
EndIf
End

Merci d'avance pour vos réponses.[/code]
Dernière modification par MetalOS le ven. 27/févr./2009 9:49, modifié 1 fois.
Atomo
Messages : 207
Inscription : lun. 17/sept./2007 12:27

Message par Atomo »

Salut,
Il suffit simplement d'éffacer le contenu du combobox juste avant de le remplir à l'aide de cette commande : ClearGadgetItemList(#Gadget).
Dans ton exemple ça donnerait ceci :

Code : Tout sélectionner

Case #Gadget_Form1_ComboBox2 
  If OpenFile(0, "Type.txt")
    ClearGadgetItemList(#Gadget_Form1_ComboBox2)
    While Eof(0) = 0 
      Texte$ = ReadString(0) 
      AddGadgetItem(#Gadget_Form1_ComboBox2, -1, Texte$) 
    Wend 
    CloseFile(0) 
  Else 
    MessageRequester("Information","Impossible d'ouvrir le fichier Type.txt!") 
  EndIf 
edit : j'ai mit -1 à la position du AddGadgetItem(), ça permet d'afficher les infos de ta liste dans l'ordre d'enregistrement.
Avatar de l’utilisateur
MetalOS
Messages : 1510
Inscription : mar. 20/juin/2006 22:17
Localisation : Lorraine
Contact :

Message par MetalOS »

Oui mais ClearGadgetItemList(#Gadget) est devenue obsolète depuis la version 4.30 de PB.
Avatar de l’utilisateur
Droopy
Messages : 1151
Inscription : lun. 19/juil./2004 22:31

Message par Droopy »

ClearGadgetItems(#Gadget) avec la 4.30 ?
Avatar de l’utilisateur
MetalOS
Messages : 1510
Inscription : mar. 20/juin/2006 22:17
Localisation : Lorraine
Contact :

Message par MetalOS »

Je met donc le ClearGadgetItems(#Gadget) qui fait parti de la version 4.30 de PB seulement il m'efface le texte une fois sélectionné.

Code : Tout sélectionner

;- Global Variables and Constants
Global BubbleTipStyle.l:BubbleTipStyle=0

;- Window Constants
Enumeration 1
  #Window_Form1
EndEnumeration
#WindowIndex=#PB_Compiler_EnumerationValue


;- Gadget Constants
Enumeration 1
  ;Window_Form1
  #Gadget_Form1_ComboBox2


EndEnumeration
#GadgetIndex=#PB_Compiler_EnumerationValue


Procedure.l Window_Form1()
  If OpenWindow(#Window_Form1,80,80,259,65,"Work Form1",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      ComboBoxGadget(#Gadget_Form1_ComboBox2,10,20,240,20)
      HideWindow(#Window_Form1,0)
      ProcedureReturn WindowID(#Window_Form1)
  EndIf
EndProcedure

;- Main Loop
If Window_Form1()

  quitForm1=0
  Repeat
    EventID  =WaitWindowEvent()
    MenuID   =EventMenu()
    GadgetID =EventGadget()
    WindowID =EventWindow()

    Select EventID
      Case #PB_Event_CloseWindow
        If WindowID=#Window_Form1
          quitForm1=1
        EndIf


      Case #PB_Event_Gadget
        Select GadgetID
          Case #Gadget_Form1_ComboBox2
             If OpenFile(0, "Type.txt")
              ClearGadgetItems(#Gadget_Form1_ComboBox2)
               While Eof(0) = 0
                Texte$ = ReadString(0)
                AddGadgetItem(#Gadget_Form1_ComboBox2, -1, Texte$)
               Wend
                CloseFile(0)
               Else
                MessageRequester("Information","Impossible d'ouvrir le fichier Type.txt!")
           EndIf 
        EndSelect

    EndSelect
  Until quitForm1
  CloseWindow(#Window_Form1)
EndIf
End
Avatar de l’utilisateur
Kwai chang caine
Messages : 6989
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Message par Kwai chang caine »

Bonjour METALOS
J'ai pas tout compris ce que tu veux exactement faire, mais moi j'aurais fait comme ça :roll:

Code : Tout sélectionner

;- Global Variables and Constants 
Global BubbleTipStyle.l:BubbleTipStyle=0 

;- Window Constants 
Enumeration 1 
  #Window_Form1 
EndEnumeration 
#WindowIndex=#PB_Compiler_EnumerationValue 


;- Gadget Constants 
Enumeration 1 
  ;Window_Form1 
  #Gadget_Form1_ComboBox2 


EndEnumeration 
#GadgetIndex=#PB_Compiler_EnumerationValue 


Procedure.l Window_Form1() 
  If OpenWindow(#Window_Form1,80,80,259,65,"Work Form1",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible) 
      ComboBoxGadget(#Gadget_Form1_ComboBox2,10,20,240,20) 
      HideWindow(#Window_Form1,0) 
      
      If OpenFile(0, "c:\Type.txt") 
       ClearGadgetItems(#Gadget_Form1_ComboBox2) 
        While Eof(0) = 0 
         Texte$ = ReadString(0) 
         AddGadgetItem(#Gadget_Form1_ComboBox2, -1, Texte$) 
        Wend 
         CloseFile(0) 
         Else 
         MessageRequester("Information","Impossible d'ouvrir le fichier Type.txt!") 
    EndIf 
  EndIf 
  ProcedureReturn WindowID(#Window_Form1) 
EndProcedure 

;- Main Loop 
If Window_Form1() 

  quitForm1=0 
  Repeat 
    EventID  =WaitWindowEvent() 
    MenuID   =EventMenu() 
    GadgetID =EventGadget() 
    WindowID =EventWindow() 

    Select EventID 
      Case #PB_Event_CloseWindow 
        If WindowID=#Window_Form1 
          quitForm1=1 
        EndIf 


      Case #PB_Event_Gadget 
        Select GadgetID 
          Case #Gadget_Form1_ComboBox2 
                     
           If TexteSelectionner$ <>GetGadgetText(#Gadget_Form1_ComboBox2)
            MessageRequester("Programme de MetalOS", "Tu viens de selectionner " + GetGadgetText(#Gadget_Form1_ComboBox2))
           EndIf 
           TexteSelectionner$ = GetGadgetText(#Gadget_Form1_ComboBox2)
                
        EndSelect 

    EndSelect 
  Until quitForm1 
  CloseWindow(#Window_Form1) 
EndIf 
End 
Avatar de l’utilisateur
MetalOS
Messages : 1510
Inscription : mar. 20/juin/2006 22:17
Localisation : Lorraine
Contact :

Message par MetalOS »

Merci Kwai chang caine, je vient de me rendre compte de mon erreur d'avoir placé mon code dans la boucle Repeat ce qui m'effaçais systématiquement le contenue de mon ComboBoxGadget. Merci à toi.
Avatar de l’utilisateur
Kwai chang caine
Messages : 6989
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Message par Kwai chang caine »

De rien, pour une fois que je peux aider 8)
Avatar de l’utilisateur
Le psychopathe
Messages : 764
Inscription : jeu. 03/mars/2005 19:23

Message par Le psychopathe »

Kwai chang caine a écrit :De rien, pour une fois que je peux aider 8)
Mdr ;) Sois pas si negatif ;)
Avatar de l’utilisateur
Kwai chang caine
Messages : 6989
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Message par Kwai chang caine »

Merci, c'est gentil, mais realiste serait le terme plus approprié :D
Répondre