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

Coding Delete sub in text editor

This one is pretty simple but deceptively hard to find on Google or in help:

I'm making an MDI text editor. How do I code a subroutine for deleting the selected text, or if no text is selected, deleting the next character in the text file? This is a straightforward thing, except that there is no Delete() method as there are Cut() and Copy() for a RichTextBox.

Thanks
0
k3n51mm
Asked:
k3n51mm
  • 7
  • 4
  • 4
  • +1
1 Solution
 
TimCotteeCommented:
Hi k3n51mm,

Something like:

With RTB1
If .SelLength = 0 Then
.Text = Left(.Text,.SelStart) & Right(.Text,Len(.Text) - .SelStart + 1)
Else
.Text = Left(.Text,.SelStart) & Right(.Text,Len(.Text) - .SelStart - .SelLength)
End If

Or close to that anyway, may need to experiment to see whether I have got the indices correct for the selection positions.

Tim Cottee
0
 
RonaldBiemansCommented:
       RichTextBox1.SelectedText.Remove(RichTextBox1.SelectionStart, RichTextBox1.SelectionLength)
0
 
RonaldBiemansCommented:
or to be more precise

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If RichTextBox1.SelectionLength = 0 Then
            RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.SelectionStart, 1)
        Else
            RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.SelectionStart, RichTextBox1.SelectionLength)
        End If
    End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RonaldBiemansCommented:
Hi TimCottee,

I think your code is for vB6 not for .net :-)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
RonaldBiemans (and TimCottee for that matter),

Your second submission will destroy the formatting in the RTB.  Anytime you use this construction:

    RichTextBox1.Text = something....

you lose the previous formatting.  TimCottee is using .Text so this will also cause the formatting to be destroyed.

How about simply using SendKeys() instead:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Focus()
        SendKeys.Send("{DEL}")
    End Sub




0
 
RonaldBiemansCommented:
Good point, Idle_Mind. Didn't think about the formating :-).

But even so, pressing the delete button on the keyboard should work too :-) LoL
0
 
k3n51mmAuthor Commented:
Thanks everyone... Idle_Mind, it seems like you're babysitting me this week... you came up with a great answer for my last question on Tuesday as well :-)

But I do have one further question... when I go to the RTB and I hit delete, nothing happens, so I imagined I had to bind this key to a function. The rest of the keys to my knowledge do work. I mean, I can enter text and stuff. What would keep the Delete key from working? I haven't done anything (consciously) to disable it.

And yes, my Delete key does work in other applications. :-)

cheers,
k3n51mm
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Is the ReadOnly() property set by chance?

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.ReadOnly = False
        RichTextBox1.Focus()
        SendKeys.Send("{DEL}")
        RichTextBox1.ReadOnly = False
    End Sub


0
 
k3n51mmAuthor Commented:

You know, that's *exactly* what I was just looking at. No, I checked the child form template in the Designer view and the Read-only property is defitintely set to False. Unless it changes programmatically without my knowledge, which makes no sense, then it should be okay.

I have another fact to relate: using your idea above, I added the two lines of code to the context menu on the child form template, and the main menu in the parent form:

        RichTextBox1.Focus()
        SendKeys.Send("{DEL}")

From the parent form, it had no effect. From the child form itself via its context menu, it maxed out the CPU (as seen in the Task Manager icon in the taskbar) and completely froze my computer in 3 seconds. No mouse, no nothing. I had to reboot twice to verify the error. The Delete key still has no effect in the text box, but all the other functions work such as Cut (ctrl+x) etc. so it can't be read-only.
0
 
k3n51mmAuthor Commented:
One more fact - I just tried putting the commands above in a try...catch statement and got he same result. I wonder what on Earth could be going on. Anyway, I will be happy to award more points for a solution since this is much more of a pain than I thought originally.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something fishy definitely going on.

Try this.  Create a new project with just a button and a richtextbox and the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Focus()
        SendKeys.Send("{DEL}")
    End Sub

Does this work as expected?
0
 
k3n51mmAuthor Commented:
Hehe, again, that's exactly what I was going to do... got a meeting in 5 minutes, so will post results within the hour...thanks so much for your help.

0
 
k3n51mmAuthor Commented:
increased points, THX
0
 
k3n51mmAuthor Commented:
Thanks again for your help... here it is: I have recreated the error precisely, except that the new simple RTB does not freeze the development machine. This is not MDI- just a form. Delete still does not work. All other commands work, including keyboard Backspace, etc. Is there some assembly I am not including? I thought all standard keyboard commands worked in any RTB. It's so damned simple I'm lost:

'This does not work with either the keyboard or this button event
'Delete
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Focus()
        SendKeys.Send("{DEL}")
    End Sub

'Works fine
'Exit
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        End
    End Sub

'Works fine
'Undo
    Private Sub MenuItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem10.Click
        RichTextBox1.Focus()
        RichTextBox1.Undo()
    End Sub

I see no reason for repetition... Copy, Cut and Select All work fine.

thx
0
 
k3n51mmAuthor Commented:
Something fishy.

When I created the Delete menu item in the Designer, I set the DEL key as the shortcut for the menu item. VS.Net does not like that. When I removed the shortcut setting, it started working.

Just that simple. Seems weird to me, but it doesn't matter... it's fixed. Thanks for your help.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Good catch...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now