Click here to Skip to main content
14,970,844 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hi,

I am programming a Virtual Keyboard, but I am not able to write a code on BACKSPACE button. I have written code on BACKSPACE button as shown below but its not complete i.e it is deleting from the last character. How to delete the character in middle (at cursor position)
VB
If TextBox1.Text.Length > 0 Then
       d = TextBox1.Text.Length
       TextBox1.Text = TextBox1.Text.Remove(d - 1, 1)
End If



Thanks in advance.
Posted
Updated 22-Feb-11 1:21am
v2
Comments
Sunasara Imdadhusen 22-Feb-11 7:21am
   
Added cod formatting!!
Sunasara Imdadhusen 22-Feb-11 7:27am
   
Good question! my vote is 5.

Hi,
Please try the following

VB
If TextBox1.Text.Length > 0 Then
    int CursorPosition = textBox1.SelectionStart;
    TextBox1.Text = TextBox1.Text.Remove(CursorPosition - 1, 1)
End If


Thanks,
Imdadhusen
   
Comments
Sergey Alexandrovich Kryukov 22-Feb-11 13:10pm
   
Yes, this is a fix, my 5, but this is just symptomatic treatment. Want to see a radical solution? Please see my answer.
--SA
You need to consider selectedtext length too, as the behavior of backspace changes.

VB
If TextBox1.SelectedText.Length > 0 Then
      TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) & TextBox1.Text.Substring(TextBox1.SelectionStart + TextBox1.SelectionLength)
    Else
      TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart - 1) & TextBox1.Text.Substring(TextBox1.SelectionStart)
    End If
   
Comments
Sergey Alexandrovich Kryukov 22-Feb-11 13:10pm
   
Yes, this is a fix, my 5, but this is just symptomatic treatment. Want to see a radical solution? Please see my answer.
--SA
Saumyavemula 23-Feb-11 2:17am
   
Hi,

Thank you, it really works. Can you help me for coding on CAPS Lock button.


Thanks in advance
Prerak Patel 23-Feb-11 3:12am
   
You are welcome.
For caps lock, you need to have a flag, or use user32.dll.
I've already answered it http://www.codeproject.com/Questions/160142/programming-on-CAPS-LOCK-button.aspx
Saumyavemula 23-Feb-11 3:33am
   
I tried using that code but its showing an error as
"Cannot find PInvoke DLL 'user32'"
Prerak Patel 23-Feb-11 4:23am
   
You don't need it. According to your logic, you just have to have a flag. Updated answer on previous question.
I think what you're writing is not a Virtual Keyboard at all.

If you assume that a target focused item is a TextBox, it means that you write some control to assist typing for your application only. My advice is: either rename you component or implement a real Virtual Keyboard.

To do this, you need just to things:

1) Use P/Invoke for Windows API SendInput.

2) Resolve the problem of keyboard focus and preventing both hiding and activation of you Virtual Keyboard, see this: Application focus getting and losing[^].

If you make this real thing, it will simulate normal user input, and your backspace problem will be solved simply by simulation of backspace key press. Mind you, backspace is not the only one, so better use the real technique instead of TextBox manipulations.

—SA
   
v2
Comments
Saumyavemula 23-Feb-11 2:15am
   
Hi,

This is in C#, I need in VB.Net. Can you please help.


Thanks in advance.
   
I did not write a word in C# in my Answer. If you're about the reference, this is just for reference. Also, there is nothing C# specific in it.

(And generally, if you're asking questions or simply search for help on .NET, don't hope for many VB.NET answers, most qualified people work on C#, as far as I can see. This is just a fact of life: not understanding C# will be a problem for you. And there is nothing wrong about learning a bit.)

--SA
Dim d As Integer
VB
If TextBox1.Text.Length > 0 Then
       d = TextBox1.Text.Length
       TextBox1.Text = TextBox1.Text.Remove(d - 1, 1)
End If
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900