Android Question Change color b4xComboBox in SelectecIndexChange Event?

netsistemas

Active Member
Licensed User
Longtime User
In B4A, after selecting the value of a Combo, I want its color to change. I have it set to the SelectedIndexChange, but the color changes on the next click event.
That is, we'll say that it first draws it and then selects it, so the color is changed with a delayed click. How can I refresh the control so that its color is immediately modified?

This is my code:

B4X:
Private Sub B4XComboHerramienta1_SelectedIndexChanged (Index As Int)
    SetColorCombo(Sender)
End Sub

Private Sub B4XComboMateriales1_SelectedIndexChanged (Index As Int)
    SetColorCombo(Sender)
End Sub

Private Sub B4XComboHerramienta2_SelectedIndexChanged (Index As Int)
    SetColorCombo(Sender)
End Sub

Private Sub B4XComboMateriales2_SelectedIndexChanged (Index As Int)
    SetColorCombo(Sender)
End Sub

private Sub SetColorCombo(Combo As B4XComboBox)
    Dim Col As Int
    If Combo.SelectedIndex = 0 Then
        Col = Colors.White
    Else
        Col = Colors.Cyan
    End If
    
    'different attempts
    'no'
    'Combo.mBase.SetColorAndBorder(Col, 0dip, Col,10dip)
    
    'this is my best code:
    Combo.cmbBox.TextColor = Col
    'i need, chage only de textcolor'
    'Combo.mBase.Color = Colors.Black
    
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
private Sub SetColorCombo(Combo As B4XComboBox)
    Dim Col As Int
    If Combo.SelectedIndex = 0 Then
        Col = Colors.Black
    Else
        Col = Colors.Red
    End If
    
    'different attempts
    'no'
    'Combo.mBase.SetColorAndBorder(Col, 0dip, Col,10dip)
    
    'this is my best code:
    Combo.cmbBox.TextColor = Col
    Combo.cmbBox.DropdownTextColor = Col
    
    Combo.cmbBox.Invalidate
    Sleep(50)
    'i need, chage only de textcolor'
    'Combo.mBase.Color = Colors.Black
    
End Sub

Like this?
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
yes, but no work:

thanks, but the effect is the same. I thought when I saw the INVALIDATE + SLEEP that that was my problem, but the color changes for the next click, producing that delay effect by 1
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
I think it's not the best solution, but it works.
If I can, I'll upload a miniproject, but for now I'll leave this solution.

i solved with this code:

B4X:
private Sub SetColorCombo(Combo As B4XComboBox)
    Dim M As List
    M.Initialize
    Dim F As Int
    Dim IdSel As Int
    IdSel = Combo.SelectedIndex

    For f = 0 To Combo.Size-1
        M.Add(Combo.GetItem(F))
    Next
    
    Dim Col As Int
    If IdSel = 0 Then
        Col = Colors.White
    Else
        Col = Colors.Cyan
    End If
    
    'Combo.mBase.SetColorAndBorder(0xFF262626 , 0dip, 0x00FFFFFF, 10dip)
    'Combo.mBase.SetColorAndBorder(Col, 0dip, Col, 20dip)

    Combo.cmbBox.TextColor = Col
    Combo.mBase.Color = Colors.Black
    Combo.cmbBox.Invalidate
    
    Sleep(50)
    
    Combo.SetItems(M)
    Combo.SelectedIndex = IdSel
 
End Sub

I will try to optimize the code
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
Demo efect. with solution included
 

Attachments

  • DemoComboColor.zip
    10.5 KB · Views: 40
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Demo efect. with solution included

Here is your complete project.optimised The layout has only the B4XCombobox1. No need to have the checkbox
B4X:
#Region Shared Files
'#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private B4XComboBox1 As B4XComboBox
    Private l As List   
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")  'has only B4XComboBox1
   
    l.Initialize
    l.Add("ITEM 0 SELECT ITEM (RED)")
    l.Add("ITEM 1 (BLUE)")
    l.Add("ITEM 2 (GREEN)")
    l.Add("ITEM 3 (MAGENTA)")
   
    B4XComboBox1.SetItems(l)
    B4XComboBox1_SelectedIndexChanged(0) 
End Sub

Private Sub B4XComboBox1_SelectedIndexChanged (Index As Int)       
    SetColorCombo(B4XComboBox1, Index)
End Sub

private Sub SetColorCombo(Combo As B4XComboBox, IdSel As Int)
    IdSel = Combo.SelectedIndex   
'    Log("idx " & IdSel)   
    Dim Col As Int
    If IdSel = 0 Then
        Col = xui.Color_Red 
    else if IdSel = 1 Then
        Col = xui.Color_Blue
    else if IdSel = 2 Then
        Col = xui.Color_Green
    Else
        Col = xui.Color_Magenta
    End If
    Combo.cmbBox.TextColor = Col
'    Combo.mBase.Color = xui.Color_White
    Log(Combo.GetItem(IdSel))
       
    Combo.cmbBox.Clear
    Combo.SetItems(l)
    Combo.SelectedIndex = IdSel
End Sub
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
The problem is not optimizing the code, it's avoiding that final data reload.
The check only allows you to see the effect without removing the data, or assigning it.
The function, I want to use it in ALL combos in my app, so I can't use an initial global variable, or at least, that's not the problem.
The point is that the color does not change immediately. That is, the control is PAINTED first, then the color changes, but is not displayed, until the next interaction with the control. Thanks for the input though.
 
Upvote 0
Top