Solved

use of vbKeyBack

Posted on 2001-08-21
12
1,569 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 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

21 Experts available now in Live!

Get 1:1 Help Now