Change Label foreground and background colout

Posted on 2005-05-04
Last Modified: 2010-04-23
Hi All,

I have a problem with some coding in a simple form based app.

What I am trying to do is have a lebel text and background change colours and text to indicate what the app is doing. (code samples below)

part of my code is:

    Private Sub txtName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtName.KeyPress
        If e.KeyChar = Chr(13) Then
            searching() : Lookup()
        Else : status_restart()
        End If
    End Sub

The lookup function works ok, but the searching part (below does not) in the same event.

    Public Function searching()
        lblStatus.Text = "Searching...."
        lblStatus.BackColor = System.Drawing.Color.Red
        lblStatus.ForeColor = System.Drawing.Color.White
    End Function

to get around the problem when using a mouse, I use the mousedown event for a button called query to call searching(), and the mouse buttonclick event to call the lookup() function.

Any ideas as to why I need two events and is there a solution?

Note - I am a great fan of using the keyboard as we can process so much quicker.


Question by:John_Mc
    LVL 19

    Expert Comment

    does the searching function do just what you have posted or are there some more codes that you have not posted. (Because there does not seem to be any problem here)

    Also what do you mean "searching part does not work" ???

    Do you mean the label does not change????
    After your Searching() you call LookUp() immediately
    Is it that your LookUp() function also changes the Text, Color etc. of the Label ???
    Now in your LookUp() if you are again changing the label, naturally the Searching() values of the label would be over-written and it would appear that Searching is not doing anything....


    Author Comment

    ok... I will try to clarify....

    I have a form with several labels and accompanying text boxes, with a datagrid at the bottom. There are also three buttons

    "clear" to clear out the text boxes and datagrid,

    "search" to perform my function "lookup()" using the data from the text boxes to generate an sql query which accesses an SQL server and then fills the datagrid with the returned results.

    A quit button.

    The user types data into any combination of text boxes, and when they click on the "search' button or press enter while the cursor is in a text box then some things happen:

    Mouse clicks on the search button:

    on the mousedown event, I change a label on the form - forecolour becomes white, backcolour becomes red, text becomes 'searching....'
    on the click event, I call the function 'lookup()'
    Any data returned is placed in the datagrid, and the label on the form is changed to backcolour green, text 'search complete'

    Note - I can either use the searching() function or the code for lblstatus - the function merely contains the commented code for lblstatus.....

    Code for both:

    Mousedown event

      Private Sub btnSearch_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnSearch.MouseDown
            'lblStatus.Text = "Searching...."
            'lblStatus.BackColor = System.Drawing.Color.Red
            'lblStatus.ForeColor = System.Drawing.Color.White
        End Sub

    Click event

        Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        End Sub

    The above coding works as I want it to (status changes to red before the database is queried)

    Enter is pressed while in a text field:

       Private Sub txtBorisNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBorisNo.KeyPress
            If e.KeyChar = Chr(13) Then
                searching() : lookup()
            End If
        End Sub

    The status_restart() function merely resets the form to what I want.

    An additional note - I could not get the status label that I am changing to do what I wanted with only one event - it exhibited identical symptoms to the problem I am having with the keypress.

    Here is a sequence:

    user does                                               lblstatus does                          lblstatus should do
    enters data in text boxes                        shows "enter data" black on uniform background(both)
    Presses enter key in text box                  as above                            white on red 'Searching"
    waits while datagrid is filled
    on datagrid fill complete                         shows 'search complete' white on green

    If I put a command after I change the label to white on red, it works correctly with only one event.

    LVL 19

    Accepted Solution

    So it looks like the GUI is not able to refresh,
    change your KeyPress to this...

    Private Sub txtBorisNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBorisNo.KeyPress
            If e.KeyChar = Chr(13) Then
            End If
        End Sub


    Author Comment

    Thanks Arif,

    Works like a treat.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now