• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1779
  • Last Modified:

use of vbKeyBack

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
rlesser
Asked:
rlesser
1 Solution
 
AzraSoundCommented:
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
 
wsh2Commented:
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
 
JonFish85Commented:
to expand on AzraSound's comment, i believe you can also go

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

Correct me if Im wrong!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AzraSoundCommented:
Yup, youre right.    :-)
0
 
rlesserAuthor Commented:
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
 
rlesserAuthor Commented:
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
 
AzraSoundCommented:
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
 
Dave_GreeneCommented:
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
 
rlesserAuthor Commented:
I like the solution because of its simplicity.  I also thought that {BACKSPACE 2} would be a simple way of doing this.

Thanks!
0
 
AzraSoundCommented:
Glad we could help  :-)
0
 
rlesserAuthor Commented:
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
 
rlesserAuthor Commented:
never mind. I just figured out that I had an extra space in my string: ",   " instead of "  "

:.<   :.>

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now