Page 1 sur 1

Combobox et fichier texte [RESOLU]

Publié : jeu. 26/févr./2009 18:03
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]

Publié : jeu. 26/févr./2009 18:14
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.

Publié : jeu. 26/févr./2009 22:44
par MetalOS
Oui mais ClearGadgetItemList(#Gadget) est devenue obsolète depuis la version 4.30 de PB.

Publié : jeu. 26/févr./2009 22:56
par Droopy
ClearGadgetItems(#Gadget) avec la 4.30 ?

Publié : ven. 27/févr./2009 7:27
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

Publié : ven. 27/févr./2009 9:30
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 

Publié : ven. 27/févr./2009 9:49
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.

Publié : ven. 27/févr./2009 21:31
par Kwai chang caine
De rien, pour une fois que je peux aider 8)

Publié : sam. 28/févr./2009 15:55
par Le psychopathe
Kwai chang caine a écrit :De rien, pour une fois que je peux aider 8)
Mdr ;) Sois pas si negatif ;)

Publié : sam. 28/févr./2009 21:43
par Kwai chang caine
Merci, c'est gentil, mais realiste serait le terme plus approprié :D