Jump to content

Recommended Posts

Posted (edited)

Hi

so i ran into a Problem i didn't expect to get stuck on.:sweating:

It sounds simple (and probably is) but i can't seem to get it to work

So i created a Combobox and filled it with some things from an array which worked fine.

$combobox = GUICtrlCreateCombo($list[0], 160, 200, 265, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GuiCtrlSetData($list, "|" & _ArrayToString($list,Default,1),$list[2])
GUICtrlSetFont(-1, 9, 400, 0, "Arial")

Now i want to change the style of the combobox from $CBS_DROPDOWN to $CBS_DROPDOWNLIST via a checkbox.

$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 160, 96, 105, 25)

Case $Checkbox1
            If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
            Call ("Read_only")
            Else
            Call ("Edit")
            Endif 
            

Func Read_only()
    GUICtrlSetStyle ($Combobox1,something to change it to $CBS_DROPDOWNLIST)
EndFunc

Func Edit()
    GUICtrlSetStyle ($Combobox1,something to change it to $CBS_DROPDOW)
EndFunc

I tried using $CBS_DROPDOWN and $CBS_DROPDOWNLIST but it simply does nothing.
I also tried $ES_READONLY which kinda worked, it changed the combobox so i couldn't change the input via dropdown anymore but i could still type in it via keyboard (which i dont want).
It's essential that its Editable but once the checkbox is checked the combobox becomes readonly so you can't type in it anymore but you can still choose between the items in the list. (It does not have to save your current input if you write something in it and then check the Checkbox it would be best if it would simpy jump back to the first item in the list)

Im sure this is a simple thing but after long google search im tired of my stupidity can someone give me a push in the right direction?

Edited by Reher
Posted

I don't believe this can be done, the closest thing I can think of is the following:

Global $idComboBox
...
..
.
$idComboBox = GUICtrlCreateCombo($aComboList[0], 10, 10, 185, 20, $CBS_DROPDOWN)
GuiCtrlSetData($idComboBox, _ArrayToString($aComboList),$aComboList[2])
...
..
.
Case $idCheckBox
    If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
        Read_only(True)
    Else
        Read_only(False)
    Endif
...
..
.
Func Read_only($bReadOnly = False)
    Local $sComboBox = GUICtrlRead($idComboBox)
    Local $aComboBox = _GUICtrlComboBox_GetListArray($idComboBox)
    Local $iComboStyle = $bReadOnly ? $CBS_DROPDOWNLIST : $CBS_DROPDOWN
    GUICtrlDelete($idComboBox)
    $idComboBox = GUICtrlCreateCombo("", 10, 10, 185, 20, $iComboStyle)
    GUICtrlSetData($idComboBox, _ArrayToString($aComboBox, "|", 1), $sComboBox)
EndFunc

 

Posted

That is true, but seems the OP does not mind that?

10 hours ago, Reher said:

 (It does not have to save your current input if you write something in it and then check the Checkbox it would be best if it would simpy jump back to the first item in the list)

Your method is better though, should he need to change to preserve new entries.

Posted
12 hours ago, Subz said:

I don't believe this can be done, the closest thing I can think of is the following:

Oh i was sure it would only be my ignorance not a language restriction i was running into...

 

6 hours ago, dmob said:

Another way would be to create both then hide/unhide based on checkbox.

i tought of that aswell, but was certain there would be a more elegant solution to this. Guess i have to play the old smoke and mirror trick and just change between 2 comboboxes. I will paste my workaround later thanks for your help anyways guys^_^

 

Posted
22 hours ago, Reher said:

I will paste my workaround later

Whoops, well i guess better late then never.

 

$Combobox = GUICtrlCreateCombo($list[0], 160, 200, 265, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GuiCtrlSetData(-1, "|" & _ArrayToString($list,Default,1),$list[2])
GUICtrlSetFont(-1, 9, 400, 0, "Arial")
GUICtrlSetState (-1, $GUI_SHOW)

$iCombobox = GUICtrlCreateCombo($list[0], 160, 200, 265, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
GuiCtrlSetData(-1, "|" & _ArrayToString($list,Default,1),$list[2])
GUICtrlSetFont(-1, 9, 400, 0, "Arial")
GUICtrlSetState (-1, $GUI_HIDE)




$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 160, 96, 105, 25)

Case $Checkbox1
            If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
            Call ("Read_only")
            Else
            Call ("Edit")
            Endif 
            

Func Read_only()
        GUICtrlSetState ($Combobox, $GUI_HIDE)
        GUICtrlSetState ($iCombobox, $GUI_SHOW)
EndFunc

Func Edit()
        GUICtrlSetState ($Combobox, $GUI_SHOW)
        GUICtrlSetState ($iCombobox, $GUI_HIDE)
EndFunc

untested, just quickly thrown together from my code.
Keep in mind that it's not perfect, the combobox variants dropdown and dropdownlist differ slightly and you have to remember to read the correct combobox in your following code :sweating:.

Posted

You dont need to use

Call ("Read_only")

  Just call function directly:

If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
    Read_only()
 Else
    Edit()
Endif

Also:

5 hours ago, Reher said:

have to remember to read the correct combobox in your following code :sweating:.

You could use:

$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 160, 96, 105, 25)

Global $cCombo

Case $Checkbox1
    If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
        Read_only()
    Else
        Edit()
    Endif 
            

Func Read_only()
        GUICtrlSetState ($Combobox, $GUI_HIDE)
        GUICtrlSetState ($iCombobox, $GUI_SHOW)
        $cCombo = $iCombobox
EndFunc

Func Edit()
        GUICtrlSetState ($Combobox, $GUI_SHOW)
        GUICtrlSetState ($iCombobox, $GUI_HIDE)
        $cCombo = $Combobox
EndFunc

then just use $cCombo in your following code.

Posted
19 hours ago, Subz said:

Did you try my code at all?

I did and it worked like intendet, but since i have already used the method of hiding one gui item and showing the other one multiple times in my script before. I figured i wouldn't start switching it up towards the end of my project now to avoid confusion.

Anyways i realy like your Method of deleting the gui item and creating another one with the same Name. Thanks for showing me i will probably use that from now on to avoid cluttering my code with dozen of hidden gui items with different names. Also thanks for showing me how to create a switch function i could realy have needet that when starting with my project.

21 hours ago, dmob said:

Just call function directly:

21 hours ago, dmob said:

then just use $cCombo in your following code.

Also thanks for this, its so simple if someone tells you but the thought never even crossed my mind.



So all in all i realy got more out of this question then i expected :lmao:
its my first time writing code and i never realy "learned" it from an professional. I already found lots of ways i could do things cleaner or simpler from my first project, but i cant be bothered to go through 500+ lines of code to clean my beginner mess up :sweating:

Tanks both of you for helping me to get a litte bit better at coding :graduated:

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
×
×
  • Create New...