Sign in to follow this  
Followers 0
Turtlix21

Deleting string from ComboBox

7 posts in this topic

#1 ·  Posted (edited)

I have a problem with deleting string from ComboBox. I found _GUICtrlComboBox_DeleteString command but it doesn't work.

I found next problem, why when I add string started by any number, _ArrayBinarySearch  return -1, when the string is in the array ?

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

$Window = GUICreate("Window", 540, 600)

GUICtrlCreateLabel("Autorzy:", 5, 36)
$Autor = GUICtrlCreateCombo("", 44, 33, 388)
$Add = GUICtrlCreateButton("Add", 435, 30, 50)
$Delete = GUICtrlCreateButton("Delete", 485, 30, 50)
GUISetState(@SW_SHOW)


Local $AutorCombo[0 + 1][1 + 1]

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Add
            If StringIsSpace(GUICtrlRead($Autor)) <> 1 And (_ArrayBinarySearch($AutorCombo, GUICtrlRead($Autor)) = -1) Then
                _ArrayAdd($AutorCombo, GUICtrlRead($Autor))
                $AutorCombo[UBound($AutorCombo) - 1][1] = _GUICtrlComboBox_AddString($Autor, GUICtrlRead($Autor))
            EndIf
        Case $Delete
            MsgBox(0, "", _ArrayBinarySearch($AutorCombo, GUICtrlRead($Autor)))
            If (_ArrayBinarySearch($AutorCombo, GUICtrlRead($Autor)) <> -1) Then
                _GUICtrlComboBox_DeleteString($AutorCombo, $AutorCombo[_ArrayBinarySearch($AutorCombo, GUICtrlRead($Autor))][1])
            EndIf
    EndSwitch
WEnd

 

Edited by Turtlix21

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

Hello Turtlix21,

The command work just fine. Read again the helpfile of the "_GUICtrlComboBox_DeleteString" and you will see that it requires the "Control ID/Handle to the control", not the array. Try this and see if it works for you.

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

$Window = GUICreate("Window", 540, 600)

GUICtrlCreateLabel("Autorzy:", 5, 36)
$Autor = GUICtrlCreateCombo("", 44, 33, 388)
$Add = GUICtrlCreateButton("Add", 435, 30, 50)
$Delete = GUICtrlCreateButton("Delete", 485, 30, 50)
GUISetState(@SW_SHOW)


Local $AutorCombo[0 + 1][1 + 1]

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Add
            If StringIsSpace(GUICtrlRead($Autor)) <> 1 And (_ArrayBinarySearch($AutorCombo, GUICtrlRead($Autor)) = -1) Then
                _ArrayAdd($AutorCombo, GUICtrlRead($Autor))
                $AutorCombo[UBound($AutorCombo) - 1][1] = _GUICtrlComboBox_AddString($Autor, GUICtrlRead($Autor))
            EndIf
        Case $Delete
            $iIndex = _ArrayBinarySearch($AutorCombo, GUICtrlRead($Autor))
            MsgBox(0, "", $iIndex)
            If $iIndex <> -1 Then
                _GUICtrlComboBox_DeleteString($Autor, $iIndex-1)
                _ArrayDelete($AutorCombo, $iIndex)
            EndIf
    EndSwitch
WEnd
Exit

 

Edited by MichaelHB
1 person likes this

Share this post


Link to post
Share on other sites

@MichaelHB Ok, one problem solved, thanks.

But I have more problems, adding works perfect (I think so), but deleting strings still doesn't work correctly, I think part of problem is in the _ArrayBinarySearch command, becouse sometimes when command should find string in array it just can't find it (returns -1), I can't understand why. Next part of problem is in the deleting string from ComboBox command (_GUICtrlComboBox_DeleteString), command just delete bad string (It could be bad declarated array variable, but I can't solve this problem too.)

I made code a little more clearly, and added some descriptions.

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

$Window = GUICreate("Window", 540, 600)

GUICtrlCreateLabel("Autorzy:", 5, 36)
$Autor = GUICtrlCreateCombo("", 44, 33, 388)
$Add = GUICtrlCreateButton("Add", 435, 30, 50)
$Delete = GUICtrlCreateButton("Delete", 485, 30, 50)
GUISetState(@SW_SHOW)


Local $AutorCombo[0 + 1][1 + 1] ;[String][Handle to string in ComboBox]

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Add
            $StringAdd = StringStripWS(GUICtrlRead($Autor), 3);..............................................Deleting White Characters before and after string
            $SearchingAdd = _ArrayBinarySearch($AutorCombo, $StringAdd);.....................................Searching for string in array
;~          MsgBox(0,"$SearchingAdd", $SearchingAdd)
            If StringIsSpace($StringAdd) <> 1 And $SearchingAdd = -1 Then;...................................Checking isn't the string white character only and isn't occured in array
                _ArrayAdd($AutorCombo, $StringAdd);..........................................................Adding String to array
                $AutorCombo[UBound($AutorCombo) - 1][1] = _GUICtrlComboBox_AddString($Autor, $StringAdd);....Adding string to ComboBox
            EndIf
            GUICtrlSetData($Autor, $StringAdd);..............................................................Setting string in ComboBox for the same string without white characters before and after string
;~          _ArrayDisplay($AutorCombo)
        Case $Delete
            $StringDelete = StringStripWS(GUICtrlRead($Autor), 3);...........................................Deleting White Characters before and after string
            $SearchingDelete = _ArrayBinarySearch($AutorCombo, $StringDelete);...............................Searching for string in array
;~          MsgBox(0,"$SearchingDelete", $SearchingDelete)
            If $SearchingDelete <> -1 Then;..................................................................Checking is string occured in the array
                _ArrayDelete($AutorCombo, $SearchingDelete);.................................................Deleting string from array
                _GUICtrlComboBox_DeleteString($Autor, $AutorCombo[$SearchingDelete][1]);.....................Deleting string from ComboBox
            EndIf
            GUICtrlSetData($Autor, $StringDelete);...........................................................Setting string in ComboBox for the same string without white characters before and after string
;~          _ArrayDisplay($AutorCombo)
    EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites

There is not problem with any of these functions. You need to read the helpfile and understand what the func does and how its work (also is very important to read the *remarks*). For example in the "_ArrayBinarySearch" you will find in the remarks: "... MUST be sorted before the search is done. Otherwise undefined results will be returned.". So as i dont know if you will sort the array or how the data will be inputed i changed to "_ArraySearch". And again you are failing to understand the "GUICtrl..._DeleteString" parameters: "$iIndex | 0-based index of string".

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

$Window = GUICreate("Window", 540, 600)

GUICtrlCreateLabel("Autorzy:", 5, 36)
$Autor = GUICtrlCreateCombo("", 44, 33, 388)
$Add = GUICtrlCreateButton("Add", 435, 30, 50)
$Delete = GUICtrlCreateButton("Delete", 485, 30, 50)
GUISetState(@SW_SHOW)


Local $AutorCombo[0 + 1][1 + 1] ;[String][Handle to string in ComboBox]

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Add
            $StringAdd = StringStripWS(GUICtrlRead($Autor), 3);..............................................Deleting White Characters before and after string
            $SearchingAdd = _ArraySearch($AutorCombo, $StringAdd, 0, 0, 0, 2);.....................................Searching for string in array
;~          MsgBox(0,"$SearchingAdd", $SearchingAdd)
            If StringIsSpace($StringAdd) <> 1 And $SearchingAdd = -1 Then;...................................Checking isn't the string white character only and isn't occured in array
                _ArrayAdd($AutorCombo, $StringAdd);..........................................................Adding String to array
                $AutorCombo[UBound($AutorCombo) - 1][1] = _GUICtrlComboBox_AddString($Autor, $StringAdd);....Adding string to ComboBox
                GUICtrlSetData($Autor, $StringAdd);..............................................................Setting string in ComboBox for the same string without white characters before and after string
            EndIf
            _ArrayDisplay($AutorCombo)
        Case $Delete
            $StringDelete = StringStripWS(GUICtrlRead($Autor), 3);...........................................Deleting White Characters before and after string
            $SearchingDelete = _ArraySearch($AutorCombo, $StringDelete, 0, 0, 0, 2);...............................Searching for string in array
         MsgBox(0,"$SearchingDelete", $SearchingDelete)
            If $SearchingDelete <> -1 Then;..................................................................Checking is string occured in the array
                _ArrayDelete($AutorCombo, $SearchingDelete);.................................................Deleting string from array
                _GUICtrlComboBox_DeleteString($Autor, $SearchingDelete-1);.....................Deleting string from ComboBox
            EndIf
            _ArrayDisplay($AutorCombo)
    EndSwitch
WEnd
Exit

Let me know if this works for you. :)

Share this post


Link to post
Share on other sites

Wyh using a array only for the searchinG of the index. Using _GUICtrlComboBox_FindStringExact or _GUICtrlComboBox_FindString you get also the ID returned you want to delete.

2 people like this

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Turtlix21,

If you are just using the array to search you should use AutoBert suggestion. I assumed that you could be using the array for other purpose.

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

$Window = GUICreate("Window", 540, 600)

GUICtrlCreateLabel("Autorzy:", 5, 36)
$Autor = GUICtrlCreateCombo("", 44, 33, 388)
$Add = GUICtrlCreateButton("Add", 435, 30, 50)
$Delete = GUICtrlCreateButton("Delete", 485, 30, 50)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Add
            $StringAdd = StringStripWS(GUICtrlRead($Autor), 3);..............................................Deleting White Characters before and after string
            $SearchingAdd = _GUICtrlComboBox_FindStringExact($Autor, $StringAdd)
            If StringIsSpace($StringAdd) <> 1 And $SearchingAdd = -1 Then;...................................Checking isn't the string white character only and isn't occured in array
                _GUICtrlComboBox_AddString($Autor, $StringAdd);....Adding string to ComboBox
            EndIf
        Case $Delete
            $StringDelete = StringStripWS(GUICtrlRead($Autor), 3);...........................................Deleting White Characters before and after string
            $SearchingDelete = _GUICtrlComboBox_FindStringExact($Autor, $StringDelete)
            If $SearchingDelete <> -1 Then;..................................................................Checking is string occured in the array
                _GUICtrlComboBox_DeleteString($Autor, $SearchingDelete);.....................Deleting string from ComboBox
            EndIf
    EndSwitch
WEnd
Exit

 

Edited by MichaelHB
1 person likes this

Share this post


Link to post
Share on other sites

This is exceactly what I was looking for. I was using array becouse I didn't know about _GUICtrlComboBox_FindStringExact command.

Thank you so much guys.

Share this post


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
Sign in to follow this  
Followers 0