0

I found out that my combobox Drawitem event doesn't fired sometimes, even when I change text in that combobox.

I have 2 questions:

  • When will combobox Drawitem event be triggered?

  • How can I trigger combobox drawitem in code?

by the way, this is my combobox drawitem event and that combobox related Func...

#Region "red tax "
Private Sub cboTaxRitsu_DrawItem(sender As Object, e As DrawItemEventArgs) Handles cboTaxRitsu.DrawItem
    Try
        Dim index As Integer = cboTaxRitsu.GetIndex(sender)
        Dim i As Object = cboTaxRitsu(index).Items(e.Index)
        'Dim i As Object = cboTaxRitsu(index).Text

        If i IsNot Nothing Then

            'draw disabled cbb
            If (e.State = (DrawItemState.Disabled Or DrawItemState.NoAccelerator Or DrawItemState.NoFocusRect Or DrawItemState.ComboBoxEdit)) Then
                cboTaxRitsu(index).DropDownStyle = ComboBoxStyle.DropDownList
                If e.Index = colorTax Then 
                    e.Graphics.DrawString(i, e.Font, New SolidBrush(Color.Red), e.Bounds)
                Else
                    e.Graphics.DrawString(i, e.Font, New SolidBrush(Color.Black), e.Bounds)
                End If
                'e.DrawFocusRectangle()
            Else
                If e.Index = colorTax Then
                    e.Graphics.DrawString(i, e.Font, New SolidBrush(Color.Red), e.Bounds)
                Else
                    e.Graphics.DrawString(i, e.Font, New SolidBrush(Color.Black), e.Bounds)
                End If

            End If

        End If
    Catch ex As Exception

    End Try
End Sub

Private Sub cboTaxRitsu_TextChanged(sender As Object, e As EventArgs) Handles cboTaxRitsu.TextChanged
    Dim index As Integer = cboTaxRitsu.GetIndex(sender)
    If cboTaxRitsu(index).Text = cboTaxRitsu(index).Items(colorTax).ToString Then
        cboTaxRitsu(index).ForeColor = Color.Red
    Else
        cboTaxRitsu(index).ForeColor = Color.Black
    End If
End Sub

Private Sub cboTaxRitsu_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboTaxRitsu.SelectedIndexChanged
    Dim index As Integer = cboTaxRitsu.GetIndex(sender)
    If cboTaxRitsu(index).Text = cboTaxRitsu(index).Items(colorTax).ToString Then
        cboTaxRitsu(index).ForeColor = Color.Red
    Else
        cboTaxRitsu(index).ForeColor = Color.Black
    End If
    If Check_Kingaku(txtKingaku(Index).Text) Then '金額適性チェック
        Set_GT_Kingaku(index)
        Calc_G_Gokei()
    End If
End Sub

Private Sub cboTaxRitsu_EnabledChanged(sender As Object, e As EventArgs) Handles cboTaxRitsu.EnabledChanged
    Dim index As Integer = cboTaxRitsu.GetIndex(sender)
    If cboTaxRitsu(index).Enabled Then
        Debug.Print(index & " : " & cboTaxRitsu(index).Text)
        If cboTaxRitsu(index).Text = "" Then
            cboTaxRitsu(index).SelectedIndex = defaultTax
        End If
    Else
        cboTaxRitsu(index).Text = cboTaxRitsu(index).SelectedText
    End If
    cboTaxRitsu(index).DropDownStyle = ComboBoxStyle.DropDownList
End Sub
#End Region

When ever I use this combobox, I clear all combobox in form

    sForm.cboTaxRitsu(RecNo).DropDownStyle = ComboBoxStyle.DropDown
    sForm.cboTaxRitsu(RecNo).Text = "" 

Then set

        .sngTaxRitsu = sngTaxritsuHolder
        If .sngTaxRitsu = 0 Then
            sForm.cboTaxRitsu(Index).Text = ""
        Else
            sForm.cboTaxRitsu(Index).Text = VB6.Format(.sngTaxRitsu, "0.0#")  
        End If

items will be added when the form is loaded and will not be changed when the form in use

        sForm.cboTaxRitsu(i).Items.Clear()
        If cuTaxRitsu1 <> -1 Then
            sForm.cboTaxRitsu(i).Items.Add(cuTaxRitsu1)
        End If

        If cuTaxRitsu2 <> -1 Then
            sForm.cboTaxRitsu(i).Items.Add(cuTaxRitsu2)
        End If

        If cuTaxRitsu3 <> -1 Then
            sForm.cboTaxRitsu(i).Items.Add(cuTaxRitsu3)
        End If

Thank you for reading!

MarioWu
  • 73
  • 1
  • 13
  • 1
    You never need to trigger `DrawItem` from code. If you chane the data and you want to see the changes immediately in combo box, you need two things, implementing INotifyPropertChanged for your model, in case you update its properties. The next thing is using a BindingList in case you add or remove items to or from the list. Take a look at [Data binding to list](https://stackoverflow.com/a/33625054/3110834). – Reza Aghaei Dec 03 '19 at 09:18
  • Thank you for your answer, but that link you sent me does not fit in this situation, What go wrong here is data in that combobox changed, but the string color- suppose to changed red - grayed out. Which is unexpected behaviour... – MarioWu Dec 03 '19 at 09:34
  • Where the data of the items are coming from. I assume you are using data binding. – Reza Aghaei Dec 03 '19 at 09:36
  • I edited my question, data will be added once, when the form is opened, and will not change when this form is in use – MarioWu Dec 03 '19 at 09:42
  • Do you have the `DrawMode = OwnerDrawFixed` of the combo box? –  Dec 03 '19 at 12:59
  • 1
    Thank you JQSOFT, I set DrawMode in form load event. My code actually do work, but sometimes, it doesn't... It work well on first load, but when i refresh data, or set selectedIndex, or Text, sometimes 1 or some combobox gray out... Even with same data, Some combobox's text color changed, some gray out... – MarioWu Dec 03 '19 at 13:18
  • 1
    You need to call `yourComboBox.Invalidate()` to refresh the drawing. Call it in the mentioned events in your last comment. –  Dec 03 '19 at 15:25
  • 1
    Thank you for being so helpful! I will try it out! – MarioWu Dec 03 '19 at 16:58
  • Most welcome. Please add @ just before any name to message a user otherwise he/she won't be notified. –  Dec 03 '19 at 18:05
  • Thank you @JQSOFT I dont use stackoverflow quite much! I notice your comment, i will use it from now on, thank you! – MarioWu Dec 03 '19 at 23:58

0 Answers0