Field flashes when assigned value then disappears

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 42
Reputation: Bill Purkins is an unknown quantity at this point 
Solved Threads: 1
Bill Purkins Bill Purkins is offline Offline
Light Poster

Field flashes when assigned value then disappears

 
0
  #1
Feb 5th, 2009
Okay, someone explain this one. I retrieve the environment variable USERNAME and assign it to a string variable. I can even show it in a MSGBOX and it shows "Owner". In my code I want to assign the value to a text box when it gets focus. It does it, but it flashes blue and then disappears and the text box appears blank, but if I save the record and go back to it? The value is there.
Help?

  1. Dim strUserName As String
  2. strUserName = Environment.GetEnvironmentVariable("USERNAME")
  3. MsgBox(strUserName)
  4. LogInNameTextBox.Text = Convert.ToString(strUserName)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Field flashes when assigned value then disappears

 
0
  #2
Feb 5th, 2009
You didn't post the actual code you're using, right?

You're probably doing something like this
  1. Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
  2. '
  3. Dim strUserName As String
  4. strUserName = Environment.GetEnvironmentVariable("USERNAME")
  5. TextBox1.Text = Convert.ToString(strUserName)
  6. End Sub
Change it to
  1. Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
  2. '
  3. Dim strUserName As String
  4. strUserName = Environment.GetEnvironmentVariable("USERNAME")
  5. TextBox1.Text = strUserName
  6. TextBox1.SelectionStart = 0
  7. TextBox1.SelectionLength = 0
  8.  
  9. End Sub
and remove Convert.ToString(). You're converting a string to a string, no need to do that.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 42
Reputation: Bill Purkins is an unknown quantity at this point 
Solved Threads: 1
Bill Purkins Bill Purkins is offline Offline
Light Poster

Re: Field flashes when assigned value then disappears

 
0
  #3
Feb 6th, 2009
Thanks for the tip but it didn't work. The field it fails to initialize is bound to a database table. When I try it on a plain text box, it works without the .select stuff.

Any thoughts?

Thanks,
Bill P.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Field flashes when assigned value then disappears

 
0
  #4
Feb 6th, 2009
Ok. How's Environment.GetEnvironmentVariable("USERNAME") related to the db table? Do you read some value to the textbox, then change it in GotFocus and save back to DB?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 27
Reputation: edgar5 is an unknown quantity at this point 
Solved Threads: 2
edgar5 edgar5 is offline Offline
Light Poster

Re: Field flashes when assigned value then disappears

 
0
  #5
Feb 6th, 2009
I've had a similar problem and it ended up a variable scope situation. Try this as a test...
Create a global variable (I'm note sure where would be appropriate in your code, if the is a generic Form put it in):
Public Class Form1
        Public userName As New StringBuilder

Then, in the sub where you retrieve USERNAME, instead of using a pointer to it (assigning it to a string), make a copy of it in the Public StringBuilder:
userName = userName.Append(Environment.GetEnvironmentVariable("USERNAME"))
Then use userName.ToString when you want this data. There are some problems with re-using StringBuilder Class as there is no Clear method (it is easy to write your own). If this solves the problem let us know!
-Ed
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++

Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 42
Reputation: Bill Purkins is an unknown quantity at this point 
Solved Threads: 1
Bill Purkins Bill Purkins is offline Offline
Light Poster

Re: Field flashes when assigned value then disappears

 
0
  #6
Feb 6th, 2009
thanks. will try.
bill p.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC