Results 1 to 11 of 11

Thread: Controlling Color of some Combobox Items by Another Combobox Choice

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2015
    Posts
    17

    Lightbulb Controlling Color of some Combobox Items by Another Combobox Choice

    this is my form design:



    the lower comboboxes depend on each other in loading items from the database
    which is designed like this:



    all i want is when i choose (Available) in the combobox named (Status) like this:



    the items in the combobox (Name) which has (Yes) in Database corresponding
    to the column (Available) changes color to red.

    the project is attached and here is my code also:
    Code:
    Imports System.Data.OleDb
    Public Class Form1
        Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Stock.accdb")
        Dim da As OleDbDataAdapter
        Dim cm As OleDbCommandBuilder
        Dim cmd As OleDbCommand
        Dim itemRoute As String()
        Private Sub ComboType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboType.SelectedIndexChanged
    
            ComboName.Items.Clear()
            ComboQuantity.Items.Clear()
            ComboStore.Items.Clear()
    
            If ComboType.SelectedItem = "Food" Then
    
                Dim dt1 As New DataTable
                dt1.Clear()
                Dim sql As String = "SELECT * FROM Food"
                da = New OleDbDataAdapter(sql, cnn)
                cm = New OleDbCommandBuilder(da)
                da.Fill(dt1)
                For ii As Integer = 0 To dt1.Rows.Count - 1
                    ComboName.Items.Add(dt1(ii)(0))
                Next
    
            End If
    
            If ComboType.SelectedItem = "Clothes" Then
    
                Dim dt1 As New DataTable
                dt1.Clear()
                Dim sql As String = "SELECT * FROM Clothes"
                da = New OleDbDataAdapter(sql, cnn)
                cm = New OleDbCommandBuilder(da)
                da.Fill(dt1)
                For ii As Integer = 0 To dt1.Rows.Count - 1
                    ComboName.Items.Add(dt1(ii)(0))
                Next
    
            End If
        End Sub
    
        Private Sub ComboName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboName.SelectedIndexChanged
    
            ComboQuantity.Items.Clear()
            ComboStore.Items.Clear()
    
            If ComboType.SelectedItem = ("Food") Then
    
                Dim dt2 As New DataTable
                dt2.Clear()
                Dim sql2 As String = "SELECT * FROM Food WHERE Food = '" & ComboName.SelectedItem & "'"
                da = New OleDbDataAdapter(sql2, cnn)
                cm = New OleDbCommandBuilder(da)
                da.Fill(dt2)
                Dim quants As String = dt2(0)(1)
                Dim quant As String() = quants.Split("-")
                For ii As Integer = 0 To quant.Count - 1
                    ComboQuantity.Items.Add(quant(ii))
                Next
                Dim stores As String = dt2(0)(2)
                Dim store As String() = stores.Split("-")
                itemRoute = store
    
            End If
    
            If ComboType.SelectedItem = ("Clothes") Then
    
                Dim dt2 As New DataTable
                dt2.Clear()
                Dim sql2 As String = "SELECT * FROM Clothes WHERE Clothes = '" & ComboName.SelectedItem & "'"
                da = New OleDbDataAdapter(sql2, cnn)
                cm = New OleDbCommandBuilder(da)
                da.Fill(dt2)
                Dim quants As String = dt2(0)(1)
                Dim quant As String() = quants.Split("-")
                For ii As Integer = 0 To quant.Count - 1
                    ComboQuantity.Items.Add(quant(ii))
                Next
                Dim stores As String = dt2(0)(2)
                Dim store As String() = stores.Split("-")
                itemRoute = store
    
            End If
    
        End Sub
    
        Private Sub ComboQuantity_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboQuantity.SelectedIndexChanged
    
            ComboStore.Items.Clear()
            ComboStore.Items.Add(itemRoute(ComboQuantity.SelectedIndex))
    
        End Sub
    End Class
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    If you want to use coloured text for items in a ComboBox then you need to set the DrawMode property to OwnerDrawFixed and then handle the DrawItem event. That event is raised every time an item gets drawn so you put the logic in the event handler to determine what colour to use and then use it. There is lots of information around about the owner-drawing part so research that first and then try drawing the items in a different colour without conditional logic first. Once you can do that, then try adding the conditional logic. If you run into a specific issue along the way, post back and we can try to help with it. There's plenty for you to do before that stage though.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2015
    Posts
    17

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    i did my best and this is the code:

    Code:
        Private Sub ComboName_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboName.DrawItem
    
            If e.Index = -1 Then Return
    
            Dim myFont As Font = New Font("times new roman", 10, FontStyle.Regular)
            Dim mycolor As New Color
            Dim myBrush As Brush = Brushes.Black
            Dim rect As Rectangle = New Rectangle(0, e.Bounds.Top, e.Bounds.Width - 1, e.Bounds.Height - 1)
    
            e.DrawBackground()
            e.DrawFocusRectangle()
    
            Select Case ComboName.Items(e.Index).ToString
                Case "GOOD"
                    mycolor = Color.Pink
                Case Else
                    mycolor = Color.White
            End Select
    
            If e.State = DrawItemState.Selected Then
                myBrush = Brushes.Red
            Else
                myBrush = Brushes.Black
            End If
    
            e.Graphics.DrawRectangle(New Pen(mycolor), rect)
            e.Graphics.FillRectangle(New SolidBrush(mycolor), rect)
            rect.Inflate(1, 1)
            e.Graphics.DrawString(ComboName.Items(e.Index).ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault)
    
        End Sub
    now the brush always (black) with (white) background
    i don't know how to select case "GOOD" to change color to red
    and after that the last step to add a conditional logic
    to connect the database

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    If I'm understanding correctly, you want to query the Food table, display the data from the Food column and colour the text of each item based on the data in the Available column, right? In that case, you would query the database for both those columns and bind the DataTable to the ComboBox, e.g.
    vb.net Code:
    1. Dim table As New DataTable
    2.  
    3. Using adapter As New OleDbDataAdapter("SELECT Food, Available FROM Food", "connection string here")
    4.     adapter.Fill(table)
    5. End Using
    6.  
    7. With foodComboBox
    8.     .DisplayMember = "Food"
    9.     .DataSource = table
    10. End With
    In the DrawItem event handler, you just get the item being drawn, get the Available value from it and determine the text colour from that:
    vb.net Code:
    1. Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
    2.     e.DrawBackground()
    3.  
    4.     Dim item = DirectCast(ComboBox1.Items(e.Index), DataRowView)
    5.     Dim text = ComboBox1.GetItemText(item)
    6.     Dim available = CBool(item("Available"))
    7.  
    8.     Using brush As New SolidBrush(If(available, Color.Red, e.ForeColor))
    9.         e.Graphics.DrawString(text, e.Font, brush, e.Bounds)
    10.     End Using
    11. End Sub
    I gave that a quick test and it looked right to me.

    By the way, having a column named "Food" in a table named "Food" is bad design. You should think of a table as a type and a column as a property. A Food object doesn't have a Food property. The property contains the name of the food, so that's how the column should be named. The column should be named "Name" or, if you want to be more explicit, "FoodName". You generally don't need to do that unless there is another column containing another name and you need to differentiate.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2015
    Posts
    17

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    not working
    you understand my project well
    this is my project design:



    i changed the draw item event handler of (ComboName) as you said to:

    Code:
        Private Sub ComboName_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboName.DrawItem
    
            If e.Index = -1 Then Return
    
            Dim item = DirectCast(ComboName.Items(e.Index), DataRowView)
            Dim text = ComboName.GetItemText(item)
            Dim available = CBool(item("Available"))
    
            e.DrawBackground()
    
            Using brush As New SolidBrush(If(available, Color.Red, e.ForeColor))
                e.Graphics.DrawString(text, e.Font, brush, e.Bounds)
            End Using
    
        End Sub
    but i didn't understand how to query my database
    may you take a look at the code of database binding in the topic above
    and instruct me of any steps required
    by the way i took your advice and changed the column name to (Name) as you may notice form the pic
    Last edited by Doctor GME; Apr 12th, 2020 at 01:45 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    This thread is about colouring items in a ComboBox. If you don't even know how to query a database then you shouldn't even be trying to colour ComboBox items. If you want help querying a database, start a thread about that. The code I posted worked for me. If you're just going to say "not working" and expect us to take it from there then you can forget it. If it doesn't work for you then you did something wrong so you can spend some time and effort working out what that is. If it doesn't work as expected then explain how it does work. If you can't be bothered to debug your code and describe its actual behaviour then I can't be bothered doing anything more here.

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    Quote Originally Posted by Doctor GME View Post
    not working
    you understand my project well
    this is my project design:



    i changed the draw item event handler of (ComboName) as you said to:

    Code:
        Private Sub ComboName_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboName.DrawItem
    
            If e.Index = -1 Then Return
    
            Dim item = DirectCast(ComboName.Items(e.Index), DataRowView)
            Dim text = ComboName.GetItemText(item)
            Dim available = CBool(item("Available"))
    
            e.DrawBackground()
    
            Using brush As New SolidBrush(If(available, Color.Red, e.ForeColor))
                e.Graphics.DrawString(text, e.Font, brush, e.Bounds)
            End Using
    
        End Sub
    but i didn't understand how to query my database
    may you take a look at the code of database binding in the topic above
    and instruct me of any steps required
    by the way i took your advice and changed the column name to (Name) as you may notice form the pic
    'Name' is a restricted word in Access
    see here ..https://docs.microsoft.com/en-us/off...reserved-words
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2015
    Posts
    17

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    Quote Originally Posted by jmcilhinney View Post
    This thread is about colouring items in a ComboBox. If you don't even know how to query a database then you shouldn't even be trying to colour ComboBox items. If you want help querying a database, start a thread about that. The code I posted worked for me. If you're just going to say "not working" and expect us to take it from there then you can forget it. If it doesn't work for you then you did something wrong so you can spend some time and effort working out what that is. If it doesn't work as expected then explain how it does work. If you can't be bothered to debug your code and describe its actual behaviour then I can't be bothered doing anything more here.
    not just the (very simple) query
    i meant query the database and bind it to combobox which is already bounded
    but don't bother yourself anymore
    i don't need help from someone with this manner

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    Have you seen my reply over in the Microsoft forum for the same question?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2015
    Posts
    17

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    @ChrisE

    thanks for the note
    i modified the names and helped me while solving the issue

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2015
    Posts
    17

    Re: Controlling Color of some Combobox Items by Another Combobox Choice

    Quote Originally Posted by kareninstructor View Post
    Have you seen my reply over in the Microsoft forum for the same question?
    of course seen it and replied their
    your instructions were amazing and helped me a lot in solving the problem
    thanks for being helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width