• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Change Label foreground and background colout

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.

Cheers

John
0
John_Mc
Asked:
John_Mc
  • 2
  • 2
1 Solution
 
arif_eqbalCommented:
OK
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....


0
 
John_McAuthor Commented:
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
        searching()
        '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
        Lookup()
    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()
        Else
            status_restart()
        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 messagebox.show command after I change the label to white on red, it works correctly with only one event.



0
 
arif_eqbalCommented:
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
            searching()
            Application.DoEvents
            Application.DoEvents
            lookup()
        Else
            status_restart()
        End If
    End Sub

0
 
John_McAuthor Commented:
Thanks Arif,

Works like a treat.

Cheers
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now