textbox question
textbox question
textbox question
textbox question
textbox question
textbox question textbox question textbox question textbox question textbox question textbox question textbox question textbox question
textbox question textbox question
textbox question
Go Back  Xtreme Visual Basic Talk > > > textbox question


Reply
 
Thread Tools Display Modes
  #1  
Old 10-19-2005, 01:53 PM
bvpcvms bvpcvms is offline
Newcomer
 
Join Date: Oct 2004
Posts: 13
Default textbox question


Hi. I'm pretty good w/VBA for Excel 2000, but just now starting to play around w/VB 2005 Express. Beta 2. I'm having a problem formatting the text in my textbox. What I would do in VBA is:

Private Sub TextBox1_Change()
TextBox1.Value = StrConv(TextBox1.Value, vbProper) ' for example
End Sub

In VB, I guess they've changed the events and properties around, so I have:

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase)

End Sub

Incredibly, this somehow results in the characters in the textbox coming out in *reverse* order. Every time I type a character, the cursor/insertion point, moves to the first position in the textbox. I can't believe that MS would make ppl programmatically move the cursor after each TextChanged event (I also tried, I believe, the KeyPress event - with the same result), but I guess you never know.

Still, I have the nagging suspicion that there must be a really easy way to get this working.
Reply With Quote
  #2  
Old 10-19-2005, 02:39 PM
wayneph's Avatar
waynephtextbox question wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

If you use the LostFocus event instead of the change event, it will only do the conversion when you are done typing everything.

Is there any reason, you would need to do it after every character?
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #3  
Old 10-19-2005, 02:56 PM
bvpcvms bvpcvms is offline
Newcomer
 
Join Date: Oct 2004
Posts: 13
Default

Well, I'd prefer to do it after every character. As it is, when I use the Leave event it works ok, as it sounds like it would w/ the LostFocus event. But at the same time, there's clearly something I don't get and I'd like to figure out what that is...
Reply With Quote
  #4  
Old 10-19-2005, 03:07 PM
herilane's Avatar
herilanetextbox question herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

I believe that when you change the Text property, you are effectively creating an entirely new string, not changing an existing string. I.e. the code gets the current contents, converts them, clears the textbox and puts in the new string. So for a moment, the textbox is empty, and VB "loses" the cursor position.

Taking it to an extreme, if you used the TextChanged event to replace "hi" with "greetings", and the cursor was initially between the h and the i, where should it be put after the change?

It's up to you as the programmer to decide how you want the control to behave. And setting the cursor after changing the text is just one line of code, after all.
Reply With Quote
  #5  
Old 10-20-2005, 01:57 AM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

All it is doing is returning your cursor to the beginning, so every letter you type appears, then cursor moves to the beginning again, hence the "reversal". You just need to move the cursor to the end of the string after each event...
Code:
TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase) TextBox1.SelectionStart = Len(TextBox1.Text)
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...
Reply With Quote
  #6  
Old 10-20-2005, 02:01 PM
herilane's Avatar
herilanetextbox question herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

The proper VB.NET way would be using Textbox1.Text.Length instead of Len().
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
textbox question
textbox question
textbox question textbox question
textbox question
textbox question
textbox question textbox question textbox question textbox question textbox question textbox question textbox question
textbox question
textbox question
 
textbox question
textbox question
 
-->