[Done] ComboBox content list size after ClearGadgetItems()

Just starting out? Need help? Post your questions and find answers here.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

[Done] ComboBox content list size after ClearGadgetItems()

Post by boddhi »

Hello,
 

Code: Select all

If OpenWindow(0, 0, 0, 270, 188, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ComboBoxGadget(0, 10, 10, 250, 21, #PB_ComboBox_Editable)
  TextGadget(#PB_Any,10,40,250,22,"1. Show ComboBox items")
  TextGadget(#PB_Any,10,62,250,22,"2. Click button")
  TextGadget(#PB_Any,10,84,250,22,"3. Show ComboBox items")
  TextGadget(#PB_Any,10,106,250,22,"4. Click button")
  TextGadget(#PB_Any,10,128,250,22,"5. Show ComboBox content size")
  ButtonGadget(1,10,154,250,22,"Populate ComboBox")
  Repeat 
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        exit=#True
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Select GetGadgetData(1)
              Case #False
                AddGadgetItem(0,-1,"Item 1")
                AddGadgetItem(0,-1,"Item 2")
                AddGadgetItem(0,-1,"Item 3")
                AddGadgetItem(0,-1,"Item 4")
                SetGadgetData(1,#True)
                SetGadgetText(1,"Clear ComboxBox content")
              Case #True
                SetGadgetData(1,#False)
                SetGadgetState(0,-1)
                ClearGadgetItems(0)
                SetGadgetText(1,"Populate ComboBox")
            EndSelect
        EndSelect
    EndSelect
  Until exit=#True
EndIf
 
Why doesn't the content list size reset after a ClearGadgetItems()?
 
Thanks.

Win10 x64 / PB 6.11 x64
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: ComboBox content list size after ClearGadgetItems()

Post by BarryG »

Correct; the drop-down list size stays the last height from the items. Maybe a bug, maybe not.

If it bothers you, you can always just redo the gadget since it holds no data anyway:

Code: Select all

Procedure DoComboBox()
  ComboBoxGadget(0, 10, 10, 250, 21, #PB_ComboBox_Editable)
EndProcedure

If OpenWindow(0, 0, 0, 270, 188, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  DoComboBox()
  TextGadget(#PB_Any,10,40,250,22,"1. Show ComboBox items")
  TextGadget(#PB_Any,10,62,250,22,"2. Click button")
  TextGadget(#PB_Any,10,84,250,22,"3. Show ComboBox items")
  TextGadget(#PB_Any,10,106,250,22,"4. Click button")
  TextGadget(#PB_Any,10,128,250,22,"5. Show ComboBox content size")
  ButtonGadget(1,10,154,250,22,"Populate ComboBox")
  Repeat 
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        exit=#True
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Select GetGadgetData(1)
              Case #False
                AddGadgetItem(0,-1,"Item 1")
                AddGadgetItem(0,-1,"Item 2")
                AddGadgetItem(0,-1,"Item 3")
                AddGadgetItem(0,-1,"Item 4")
                SetGadgetData(1,#True)
                SetGadgetText(1,"Clear ComboxBox content")
              Case #True
                DoComboBox()
                SetGadgetData(1,#False)
                SetGadgetText(1,"Populate ComboBox")
            EndSelect
        EndSelect
    EndSelect
  Until exit=#True
EndIf
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBox content list size after ClearGadgetItems()

Post by RASHAD »

Code: Select all

              Case #True 
                For i = 0 To CountGadgetItems(0)
                  RemoveGadgetItem(0,0)
                Next
                SetGadgetItemText(0,0,"")                
                SetGadgetText(1,"Populate ComboBox")
Egypt my love
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: ComboBox content list size after ClearGadgetItems()

Post by boddhi »

Hi BarryG & Rashad,

Thank you for your replies.
BarryG wrote: If it bothers you, you can always just redo the gadget since it holds no data anyway:
...and your suggestion to delete and recreate the gadget.

However, it's going to be more complicated because I'm using the Dialog library which does not allow to do this simply, except at the cost of container nesting :wink: .
Rashad wrote: For [...] : RemoveGadgetItem(0,0) : Next
I used ClearGadgetItems() in my example for a simple demonstration. :wink:
I'm already using RemoveGadgetItem() because I don't necessarily have to delete all the items in the list.
But when I delete the last item, the list size is not reset to 0 and "retains memory” of the number of items it has contained.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: ComboBox content list size after ClearGadgetItems()

Post by BarryG »

You won't believe this, but ChatGPT indirectly told me a hacky way to do it. :shock:

This is what I asked: "using purebasic, how do i clear a comboboxgadget so that its drop down list is one item high".

Its answer was (bolded by me to the clue):
ChatGPT wrote:In PureBasic, you can clear a ComboBoxGadget by removing all the items from it. To make the drop-down list appear with only one item height, you would typically want to remove all items except one.
One item, you say? Okay, so let's do it:

Code: Select all

If OpenWindow(0, 0, 0, 270, 188, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ComboBoxGadget(0, 10, 10, 250, 21, #PB_ComboBox_Editable)
  TextGadget(#PB_Any,10,40,250,22,"1. Show ComboBox items")
  TextGadget(#PB_Any,10,62,250,22,"2. Click button")
  TextGadget(#PB_Any,10,84,250,22,"3. Show ComboBox items")
  TextGadget(#PB_Any,10,106,250,22,"4. Click button")
  TextGadget(#PB_Any,10,128,250,22,"5. Show ComboBox content size")
  ButtonGadget(1,10,154,250,22,"Populate ComboBox")
  Repeat 
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        exit=#True
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Select GetGadgetData(1)
              Case #False
                AddGadgetItem(0,-1,"Item 1")
                AddGadgetItem(0,-1,"Item 2")
                AddGadgetItem(0,-1,"Item 3")
                AddGadgetItem(0,-1,"Item 4")
                SetGadgetData(1,#True)
                SetGadgetText(1,"Clear ComboxBox content")
              Case #True
                SetGadgetData(1,#False)
                ;
                ; These 3 lines thanks to ChatGPT.
                ClearGadgetItems(0)
                AddGadgetItem(0,-1,"")
                ClearGadgetItems(0)
                ;
                SetGadgetText(1,"Populate ComboBox")
            EndSelect
        EndSelect
    EndSelect
  Until exit=#True
EndIf
It's clear that ChatGPT does have its uses. :)
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: ComboBox content list size after ClearGadgetItems()

Post by boddhi »

BarryG wrote:
ChatGPT wrote:In PureBasic, you can clear a ComboBoxGadget by removing all the items from it. To make the drop-down list appear with only one item height, you would typically want to remove all items except one.
Thanks to you (and ChatGPT :mrgreen: ) for your research

In fact, it has the same result as Rashad's code: A height of an item. There seems to be a constraint here 🤔 .

Note : However, a worth difference in gadget initialization between using the Dialog library and the classic Gadget library
With Gadget library:
Image
 
With Dialog library:
Image
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: ComboBox content list size after ClearGadgetItems()

Post by BarryG »

Post your snippet for the Dialog version so I can test?
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: ComboBox content list size after ClearGadgetItems()

Post by boddhi »

BarryG wrote: Post your snippet for the Dialog version so I can test?
 

Code: Select all

ChaineXML.s="<?xml version='1.0' encoding='UTF-16'?>"+Chr(10)+ ;{
            "<dialogs>"+Chr(10)+
            "  <window id='0' name='RF_FENPRINCIPALE' text='Test' xpos='0' ypos='0' width='400' height='auto' minwidth='auto' minheight='auto' maxheight='auto' margin='10' flags='#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_WindowCentered'>"+Chr(10)+
            "    <vbox expand='yes' align='' spacing='2'>"+Chr(10)+
            "      <combobox id='0' name='GAD_FP_LS_ACTION' height='24' flags='#PB_ComboBox_Editable'/> "+Chr(10)+
            "      <empty height='8'/>"+Chr(10)+
            "    </vbox> "+Chr(10)+
            "  </window>"+Chr(10)+
            "</dialogs>" ;}

If ParseXML(0,ChaineXML)
  If XMLStatus(0)=#PB_XML_Success
    If CreateDialog(0)
      NoDialogue=OpenXMLDialog(0,0,"RF_FENPRINCIPALE")
      If NoDialogue
        Repeat
        Until WaitWindowEvent()=#PB_Event_CloseWindow
        FreeDialog(0)
      EndIf
    EndIf
    FreeXML(0)
  EndIf
EndIf
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: ComboBox content list size after ClearGadgetItems()

Post by BarryG »

Sorry, I thought you'd post a Dialog version of the code above for testing. I haven't used the Dialog lib before so don't have time to work this out from scratch. I was just going to tinker with a Dialog version that matched the above.
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: ComboBox content list size after ClearGadgetItems()

Post by AZJIO »

Code: Select all

If OpenWindow(0, 0, 0, 270, 188, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	ComboBoxGadget(0, 10, 10, 250, 21, #PB_ComboBox_Editable)
	ButtonGadget(1, 10, 124, 120, 22, "Add")
	ButtonGadget(2, 140, 124, 120, 22, "Clear")
	TextGadget(3,10,150,260,38,"Click 'Add'")
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_CloseWindow
				exit = #True
			Case #PB_Event_Gadget
				Select EventGadget()
					Case 1
						For i = 0 To 3
							AddGadgetItem(0,  - 1, "Item " + Str(i))
						Next
						SendMessage_(GadgetID(0),#CB_SHOWDROPDOWN,1,1)
						Delay(330)
						SendMessage_(GadgetID(0),#CB_SHOWDROPDOWN,0,1)
						SetGadgetText(3, "Click 'Clear'")
					Case 2
						SendMessage_(GadgetID(0), #CB_SETMINVISIBLE, 1, 0)
						SetGadgetState(0,  - 1)
						ClearGadgetItems(0)
; 						For i = CountGadgetItems(0) - 1 To 0 Step  -1
; 							RemoveGadgetItem(0,i)
; 						Next
; 						For i = 0 To CountGadgetItems(0) - 1
; 							RemoveGadgetItem(0,0)
; 						Next
; 						SendMessage_(GadgetID(0), #CB_INITSTORAGE, 22, 88)
; 						SendMessage_(GadgetID(0), #CB_RESETCONTENT, 0, 0)
; 						SendMessage_(GadgetID(0), #CB_SETDROPPEDWIDTH, 400, 0)
						SendMessage_(GadgetID(0), #CB_SETMINVISIBLE, 30, 0)
						SendMessage_(GadgetID(0),#CB_SHOWDROPDOWN,1,1)
						SetGadgetText(3, "Did you understand anything? Count=" + CountGadgetItems(0))
				EndSelect
		EndSelect
	Until exit = #True
EndIf
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Done] ComboBox content list size after ClearGadgetItems()

Post by Fred »

Fixed.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: [Done] ComboBox content list size after ClearGadgetItems()

Post by boddhi »

@AZJIO
As your last post has no message, I don't know whether it's intended for me or not, and what to make of your code having, in the end, the same result as those proposed before.

So, to answer the question asked when I ran your code: No, I haven't understood anything! :mrgreen:

Fred wrote:Fixed.
Thanks. Merci. 😉
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply