Solved

use of vbKeyBack

Posted on 2001-08-21
12
1,540 Views
Last Modified: 2008-01-16
I want to be able to use vbKeyBack in a RichTextBox. The idea is as follows:

If I have a text string in the RTB such as 112233, I want to then use vbKeyBack & vbKeyBack so as to delete the last two characters, in this example the rtb would finally read 1122.

I tried the following:
Form1!rtbTemp.text = vbKeyBack & vbKeyback  (etc...)

but what I got was the ASCII value (88 in other words) in the rtb.

What I want is a way to delete a certain number of characters from the rtb

Thanks

Ron Lesser
0
Comment
Question by:rlesser
12 Comments
 
LVL 28

Accepted Solution

by:
AzraSound earned 100 total points
ID: 6410720
One way may to be to simply use SendKeys:

RichTextBox1.SetFocus
SendKeys "{BACKSPACE}"
SendKeys "{BACKSPACE}"


If you are always removing from the end of the text, you could try:

RichTextBox1.Text = Left$(RichTextBox1.Text, Len(RichTextBox1.Text) - 2)
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6410802
Or.. use the Replace function

Form1!rtbTemp.text = Replace(Form1!rtbTemp.text, "112233", "1122")

Or the.. SelStart, SelLength, SelText properties of the RichTextBox (<--- Great for reducing flicker)

I suppose we need a better context for your question.. <smile>.

0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6410835
to expand on AzraSound's comment, i believe you can also go

SendKeys "{BACKSPACE 2}" to backspace twice...

Correct me if Im wrong!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6410845
Yup, youre right.    :-)
0
 

Author Comment

by:rlesser
ID: 6410847
Thanks for your comments. Here is more information about the context (I'm trying to be brief):

I have a multiselect list box that I'm using to supply a set of descriptors. The user selects the descriptors that then are "printed" to the rtb, with a comma after each selected descriptor. For example, I want

"the colors are red, green, yellow" and not
"the colors are red, green, yellow,"

Therefore I want to use my loop to list all of the descriptors, ie red, green, yellow in this example, and then delete the last comma.

Thanks again

Ron Lesser
0
 

Author Comment

by:rlesser
ID: 6410848
Thanks for your comments. Here is more information about the context (I'm trying to be brief):

I have a multiselect list box that I'm using to supply a set of descriptors. The user selects the descriptors that then are "printed" to the rtb, with a comma after each selected descriptor. For example, I want

"the colors are red, green, yellow" and not
"the colors are red, green, yellow,"

Therefore I want to use my loop to list all of the descriptors, ie red, green, yellow in this example, and then delete the last comma.

Thanks again

Ron Lesser
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Expert Comment

by:AzraSound
ID: 6410872
Option #1

The first item is written outside of the loop, and then the rest is written, e.g.,

strTemp = lstBox.List(0)
For i = 1 to lstBox.ListCount - 1
   strTemp = strTemp & ", " & lstBox.List(i)
Next



Option #2
Use something like the Left$ operator to chop off that last comma once you have the string completely built.


Option #3 (VB6 only)
Create an array of the items selected, and then use the Join function to biuld your string, e.g.,

Dim vArr As Variant
Redim vArr(0 To lstBox.ListCount - 1)
For i = 0 To lstBox.ListCOunt - 1
   vArr(i) = lstBox.List(i)
Next
strFinal = "the colors are " & Join(varr, ", ")
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6410898
Here's another way

Private Sub Command1_Click()
    Dim x As String
    x = "121212,2121212,212121,"
   
    If InStr(Len(x), x, ",") Then
       'show it
       MsgBox x
       'trim it
       MsgBox Left(x, (Len(x) - 1))
    End If
End Sub
0
 

Author Comment

by:rlesser
ID: 6411005
I like the solution because of its simplicity.  I also thought that {BACKSPACE 2} would be a simple way of doing this.

Thanks!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6411062
Glad we could help  :-)
0
 

Author Comment

by:rlesser
ID: 6412124
I thought I had this working, but now it's not. I've obviously missed a step somewhere. Here is the whole code snippet. You can see where I put the comma in on the third line and I've commented below where I try to remove the last comma.

Can you help me figure out where I've gone astray?

Thanks

For intLoopIx = 0 To lstTech1.ListCount - 1
    If lstTech1.Selected(intLoopIx) Then
    Form1!rtbTemp.text = lstTech1.List(intLoopIx) & ",  "
   
        Form1!rtbSelection.SetFocus
    strWork = Form1!rtbSelection.text
    Wstart = Form1!rtbSelection.SelStart
    Wlength = Form1!rtbSelection.SelLength
   
    Clipboard.Clear
    Clipboard.SetText Form1!rtbTemp.text
   
    strClip = Clipboard.GetText()
    strWork = Left$(strWork, Wstart) + strClip + _
        Mid$(strWork, Wstart + Wlength + 1)
    Form1!rtbSelection.text = strWork
    Form1!rtbSelection.SelStart = Wstart + Len(strClip)
   
   
    End If
    Next intLoopIx

'get rid of comma

If lstTech1 > "" Then
    Form1!rtbSelection.SetFocus
    Wstart = Form1!rtbSelection.SelStart
    Wlength = Len(strWork)

Form1!rtbSelection.text = Left$(Form1!rtbSelection.text, Len(Form1!rtbSelection.text) - 2)
Form1!rtbSelection.SelStart = Wstart + Wlength
End If
0
 

Author Comment

by:rlesser
ID: 6413151
never mind. I just figured out that I had an extra space in my string: ",   " instead of "  "

:.<   :.>

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now