943,865 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 671
  • VB.NET RSS
Feb 5th, 2009
0

Field flashes when assigned value then disappears

Expand Post »
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?

VB.NET Syntax (Toggle Plain Text)
  1. Dim strUserName As String
  2. strUserName = Environment.GetEnvironmentVariable("USERNAME")
  3. MsgBox(strUserName)
  4. LogInNameTextBox.Text = Convert.ToString(strUserName)
Reputation Points: 10
Solved Threads: 1
Light Poster
Bill Purkins is offline Offline
42 posts
since Jul 2008
Feb 5th, 2009
0

Re: Field flashes when assigned value then disappears

You didn't post the actual code you're using, right?

You're probably doing something like this
VB.NET Syntax (Toggle Plain Text)
  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
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Feb 6th, 2009
0

Re: Field flashes when assigned value then disappears

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
Bill Purkins is offline Offline
42 posts
since Jul 2008
Feb 6th, 2009
0

Re: Field flashes when assigned value then disappears

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?
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Feb 6th, 2009
0

Re: Field flashes when assigned value then disappears

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!
Reputation Points: 18
Solved Threads: 2
Light Poster
edgar5 is offline Offline
30 posts
since Dec 2008
Feb 6th, 2009
0

Re: Field flashes when assigned value then disappears

thanks. will try.
bill p.
Reputation Points: 10
Solved Threads: 1
Light Poster
Bill Purkins is offline Offline
42 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: How to move a SQL Server Database from One Server to another server
Next Thread in VB.NET Forum Timeline: Insert Image + ListView Subitem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC