LostFocus event of a control

D

dbuchanan

Hello,

What event do I use to change the backcolor of a text box when I leave
it using any possible method.

I want to change the backcolor of the control to the color of the form
when the user leaves the control. Neither LostFocus or Leave works!
Why?

Here is the code...

Private Sub txtComponentDescription_LostFocus(ByVal sender As Object,
ByVal e As System.EventArgs) Handles txtComponentDescription.LostFocus
Me.txtComponentDescription.BackColor = Me.BackColor
Me.txtComponentDescription.Enabled = False
End Sub

The BackColor does not change as desired but the control does become
disabled.

How do I make this work?

dbuchanan
 
W

woo_derek

This is interesting... I just tried the code and it works fine for me.

I am using the Leave event rather than the LostFocus event, but you say
you've tried that too.

Why don't you try changing your colors to some really contrasting
colors to make sure that you're not switching the color to the same
exact color.

Note: The standard windows form color is the same as the color that a
text box changes to when it becomes disabled. You might have a
conflicting color here.

Also, do you set the backcolor of the textbox prior to the application
running (or during runtime) before the leave event would occur? In
other words, do you set the color of the textbox to something other
than the standard system color before the user would leave the text box?
 
D

dbuchanan

It does not work fine for me

Here is more information about my code.

In the load event I have:
\\
Me.txtComponentDescription.BackColor = Me.BackColor
Me.txtComponentDescription.Enabled = False
//

A button on the form runs this code

\\
Private Sub btnAllowEditOfDescription_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnAllowEditOfDescription.Click
Me.txtComponentDescription.Enabled = True
Me.txtComponentDescription.BackColor =
System.Drawing.SystemColors.Info
End Sub
//

The Leave code - I tried the leave code as you did.

\\
Private Sub txtComponentDescription_Leave(ByVal sender As Object, ByVal
e As System.EventArgs) Handles txtComponentDescription.Leave
Me.txtComponentDescription.BackColor = System.Drawing.Color.Black
'Note that I changed it to the color Black
Me.txtComponentDescription.Enabled = False
End Sub
//

When the form opens the control BackColor is the same color as the form
~ a kindof grayish.
When the button enables it the BackColor is SystemColor.Info ~ a light
yellow on my machine.
When I leave the form (either by the tab key or using the mouse to
click elsewhere) the BackColor turns a white-ish and the control is
disabled. Not the Black as it is instructed!

What is up here? Why will it not do as the code instructs and turn
Black

dbuchanan
 
W

wooster11

hmmm... I don't really know what to say. I can't seem to recreate the
problem you're having. I've run that same exact code that you have up
there (below) and I get the behavior that you want. Have you tried
opening up a dummy project and just testing that piece of code in the
new project. Do you have other text boxes that you do this to? Is it
only happening to one of them or all of them? I really don't know what
to say since I can't recreate it. I'm just trying to help possibly
guide you to find the solution.

Another thing you might want to check (doubt this is it, but worth a
check)... Did you happen to set the transparency key of your form to
black?

Here's the code I tried: I just have a form (Form2) with a textbox and
a button on it.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.txtComponentDescription.BackColor = Me.BackColor
Me.txtComponentDescription.Enabled = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.txtComponentDescription.Enabled = True
Me.txtComponentDescription.BackColor = SystemColors.Info
End Sub

Private Sub txtComponentDescription_Leave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles txtComponentDescription.Leave
Me.txtComponentDescription.BackColor =
System.Drawing.Color.Black
Me.txtComponentDescription.Enabled = False
End Sub

If there's possibly any other information you might have, we can work
on trying to pinpoint the issue.

Derek Woo
 
D

dbuchanan

Derek,

Thank you for your reply.
Have you tried opening up a dummy project and just testing that piece of code
I'll try that.
Do you have other text boxes that you do this to? No.

Did you happen to set the transparency key of your form to black?
No. And I used black only after you suggested to use more contrast.

Another thing is that this is in a derived form (visual inheritance),
I'll try it in the same project where I am not using visual inheritance
and see what I get.

dbuchanan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top