Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2005

Tip: Looking for answers? Try searching our database.

LostFocus event of a control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dbuchanan - 28 Oct 2005 17:19 GMT
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
woo_derek@yahoo.com - 28 Oct 2005 18:57 GMT
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?
dbuchanan - 28 Oct 2005 20:04 GMT
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
wooster11 - 28 Oct 2005 21:30 GMT
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
dbuchanan - 28 Oct 2005 21:43 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.