Jump to content

Recommended Posts

Hello guys,

I've just encountered a very sneaky problem :

I have a combobox where users can select a department and, following, a 2nd combobox which content change according to the first choice.

Case $ComboService ;Dpt choice
    GUICtrlSetData($ComboCatN1,"") ;Reset Combo
    GUICtrlSetData($ComboCatN2,"") ;Reset Combo
    Global $Grp = GUICtrlRead($ComboService)
    $Ini = @AppDataDir & "\GLPI\" & $Grp ;path to ini file
    $array_N1 = IniReadSectionNames($Ini) ;read sections
    For $i = 1 To $array_N1[0]
       $N1 &= "|" & $array_N1[$i]
    Next
    GUICtrlSetData($ComboCatN1, $N1) ;Populate combo

It works great but... When I choose something in my first combo ($ComboService) and something in the second one ($ComboCatN1) and then I choose something new in the first one, the second combo repopulates but adds the previous selection.

Exemple : if my 2nd combo contains "Cat.A ; Cat.B ; Cat.C", if I select "Cat.B" then change the selection in my first combo, my 2nd combo will be "Cat.B ; Cat.A ; Cat.B ; Cat.C".

I also tried with  _GUICtrlComboBox_SetCurSel($ComboCatN1, -1) but with no more success...

 

Link to post
Share on other sites
  • Moderators

Slhama,

Welcome to the AutoIt forums.

You need to add a separator character to the start of the new data - then, as explained in the Help file, it will replace the existing content in the combo:

Quote

For Combo or List control :
If the "data" corresponds to an already existing entry it is set as the default.
If the "data" starts with GUIDataSeparatorChar or is an empty string "" the previous list is destroyed. A trailing GUIDataSeparatorChar is ignored.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites
2 hours ago, Melba23 said:

Slhama,

Welcome to the AutoIt forums.

You need to add a separator character to the start of the new data - then, as explained in the Help file, it will replace the existing content in the combo:

M23

Hi Melba23,

I may be wrong but I think I'm already using the intended separator here when I populate my combo :

$N1 &= "|" & $array_N1[$i]

And, as said, in the help file :

Quote

If the "data" starts with GUIDataSeparatorChar or is an empty string "" the previous list is destroyed

Thats's why i'm destroying the previous list here :

GUICtrlSetData($ComboCatN1,"") ;Reset Combo

But it does not work as intended : the combobox does not keep the entire previous list but adds the previous selected item at the beginning of the new list. I join pictures to explain it clearly.

2021-01-24 23_33_42-Demande IT.png

2021-01-24 23_34_02-Demande IT.png

Link to post
Share on other sites

It would be much more effective if you were to provide something runable for us.  By giving us images and parts of a script, we need to guess what is your issue.  For now on, please post a fully executable script with the effective way to replicate your problem.

Link to post
Share on other sites
35 minutes ago, Nine said:

It would be much more effective if you were to provide something runable for us.  By giving us images and parts of a script, we need to guess what is your issue.  For now on, please post a fully executable script with the effective way to replicate your problem.

You're right Nine.

Here's a fully functional part of my code with which I can reproduce the problem (I just removed the non-essential parts & still have the same behavior) :

#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <GDIPlus.au3>
#include <GuiComboBox.au3>
#include <GuiConstants.au3>
#include <GUIConstantsEx.au3>
#include <INet.au3>
#Include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <StringConstants.au3>
#Include <WinAPI.au3>
#include <WinAPIEx.au3>
#include <WinAPIFiles.au3>
#include <WindowsConstants.au3>

Global $N1 = ""

Global $Srv = ""
Global $NbrGrp = DirGetSize(@ScriptDir,3)-1
$Grps = _FileListToArray(@ScriptDir,"*.ini",1)
If $NbrGrp[1] = 1 Then
   GUICtrlSetData($ComboService, $Grps, $Grps)
EndIf
For $i = 1 to UBound($Grps)-1
   $Srv &= "|" & $Grps[$i]
Next

#Region ### START Koda GUI section ###
$FormDemandeIT = GUICreate("Demande IT", 507, 685, 481, 127, $WS_OVERLAPPEDWINDOW)
GUISetBkColor(0xFFFFFF)
GUIRegisterMsg($WM_DROPFILES, 'WM_DROPFILES')
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetColor(-1, 0xFFFFFF)

$ButtonSend = GUICtrlCreateButton("Envoyer", 8, 620, 489, 57)

$LabelService = GUICtrlCreateLabel("Service *", 8, 102, 60, 17)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$ComboService = GUICtrlCreateCombo("", 8, 118, 489, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
   GUICtrlSetData($ComboService,$Srv)
$LabelCatN1 = GUICtrlCreateLabel("Catégorie *", 8, 144, 70, 17)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$ComboCatN1 = GUICtrlCreateCombo("", 8, 160, 489, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$LabelCatN2 = GUICtrlCreateLabel("Sous-catégorie *", 8, 186, 100, 17)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$ComboCatN2 = GUICtrlCreateCombo("", 8, 202, 489, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

$LabelDescription = GUICtrlCreateLabel("Description *", 8, 230, 100, 17)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Description = GUICtrlCreateEdit("", 8, 248, 489, 217)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

$Logo = GUICtrlCreatePic("C:\Windows\WinSxS\amd64_microsoft-windows-s..l-wallpaper-windows_31bf3856ad364e35_10.0.18362.1_none_ad6f96302daa2dbf\img0_1366x768.jpg", 10, 0, 268, 81)
$Title = GUICtrlCreateLabel("Demande IT", 285, 8, 228, 40)
   GUICtrlSetFont(-1, 30, 400, 0, "Calibri")
   GUICtrlSetColor($Title, 0x004a98)

$Group1 = GUICtrlCreateGroup("Pièces jointes", 8, 475, 489, 140)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Label6 = GUICtrlCreateLabel("Glissez-déposez vos pièces jointes ici (maximum 5)", 91, 545, 306, 17)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Func Cat_ShortCode()
   $N1 = GUICtrlRead($ComboCatN1)
   $N2 = GUICtrlRead($ComboCatN2)
   Global $ShortCode = IniRead($Ini, $N1, $N2, "")
EndFunc

Func N2()
   $N1 = GUICtrlRead($ComboCatN1)
   $array_N2 = IniReadSection($Ini, $N1)
   If IsArray($array_N2) Then
      $N2 = ""
      For $i = 1 To $array_N2[0][0]
         $N2 &= "|" & $array_N2[$i][0]
      Next
      GUICtrlSetData($ComboCatN2, $N2)
   EndIf
EndFunc

While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         Exit
      Case $ComboCatN1
         GUICtrlSetData($ComboCatN2,"") ;Erase Combo2 content at every combo1 selection change
         N2() ;Call func
      Case $ComboService ;Dpt choice
         GUICtrlSetData($ComboCatN1,"") ;Reset Combo
         GUICtrlSetData($ComboCatN2,"") ;Reset Combo
         Global $Grp = GUICtrlRead($ComboService)
         $Ini = @ScriptDir & "\" & $Grp ;path to ini file
         $array_N1 = IniReadSectionNames($Ini) ;read sections
            For $i = 1 To $array_N1[0]
               $N1 &= "|" & $array_N1[$i]
            Next
         GUICtrlSetData($ComboCatN1, $N1) ;Populate combo
      Case $ComboCatN2
         Cat_ShortCode() ;Call func
   EndSwitch
WEnd

I join the 2 .ini files needed, they must be placed in the same folder as the script.

Select "A.ini" in the first combo, you will obtain "Cat A" & "Cat AA" in the second. Select "Cat AA". Change selection in first combo to "B.ini". The second combo will have "Cat AA", "Cat B" & "Cat BB" instead of just "Cat B" & "Cat BB" as intended with the ini files.

A.ini B.ini

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...