473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to remove extra character ' from the text box

Guys,
I have a textbox1.text on my form. If any person enters the name as D'Souza, i want to eliminate that " ' " from the text box before i could save it. How do i do this...
please comment

I am able to detect that extra character but dont know how to replace the text box without string (OR) removing the string from that name. The code to trace the string is as below:

dim str as string
str=textbox1.text
if str.contains( " ' " ) then
msgbox(" you had entered EXTRA character in the name box")
endif

Please comment, I am using VB.NET 2005
Mar 10 '07 #1
4 1578
kenobewan
4,871 Expert 4TB
Maybe something like:
Expand|Select|Wrap|Line Numbers
  1. Dim sr As String = "D'Souza"
  2. ' "'"c means use just a character
  3. Dim pos As Integer = sr.LastIndexOf("'"c)
  4. Dim place As String = sr.Remove(0, pos + 1)
Mar 10 '07 #2
Thanks,
Your code is working fine but its doing something more..
when I type name D'Souza

your code returns the value as Souza, in another words, its removing ( D' ) both the things and I just want to eliminate only the " ' " special character.

can you please do something for this. Thanks for ur time and patience.
subbu
Mar 11 '07 #3
I tried to modify your coding and is now giving me the desired results I was looking for. The modified code is as follows:-

Dim sr As String
Dim pos As Integer
sr = TextBox1.Text
pos = sr.LastIndexOf(" ' ")
Dim place As String
place = sr.Remove(pos, 1)
MsgBox(place)

If I enter name as D ' Souza, It gets name as D Souza, This is fine for now.

What if person types name as D' Souza D' Marie (this is just an example)

It should remove " ' " from both the places....? How is this possible..??
Mar 11 '07 #4
bergy
89
Another way to do it would be to do a replace...

Expand|Select|Wrap|Line Numbers
  1. Textbox1.Text = Textbox1.Text.Replace("'", "")
Now if you're loading this data from some where the above solution might be practical, but if you're just worried about user entered data there is a better way to do it (in my opinion).

In the KeyPress event of the textbox you can find out which key was pressed and then decide whether or not to allow that character to be entered in the textbox. Here is an example that would not allow the ' key to be pressed inside of TextBox1

Expand|Select|Wrap|Line Numbers
  1.     Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  2.         'MsgBox(e.KeyCode.ToString())
  3.         If e.KeyCode = Keys.Oem7 Then
  4.             e.SuppressKeyPress = True
  5.         End If
  6.     End Sub
Now, normally if you type Keys. You'll get a list of all the keys -- where "a" would be Keys.A, however since ' is a special character I had to uncomment the first msgbox line to find out what the key code was. You can uncomment and you'll get a message box for every key you press telling you the correct Key.CODE information for pressed key. Here is another example with multiple keys supressed:

Expand|Select|Wrap|Line Numbers
  1.     Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  2.         'MsgBox(e.KeyCode.ToString())
  3.         Select Case e.KeyCode
  4.             Case Keys.Oem7
  5.                 e.SuppressKeyPress = True
  6.             Case Keys.Oemcomma
  7.                 e.SuppressKeyPress = True
  8.             Case Keys.OemPeriod
  9.                 e.SuppressKeyPress = True
  10.             Case Keys.V
  11.                 If e.Control Then
  12.                     'if control+v stop from pasting
  13.                     e.SuppressKeyPress = True
  14.                 End If
  15.         End Select
  16.     End Sub
  17.  
Now keep in mind this solution is great if you're working with user input. Also take a look at the last Case statement in the second block of code. This will stop a user from pasting into the textbox - obviously since the user could paste data into the box without actually pressing the ' and our code stopping them we need to disable that. If you want to be able to paste, just run some String.Replace() functions (as shown above) in the TextChanged event to cover yourself.
Mar 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jon LaRosa | last post by:
I'm am trying to remove multiple line breaks ("\n"). Here's my code: $imageStr = trim($_POST); $imageStr = str_replace("\n\n", "\n", $imageStr); ---------- $_POST is a textarea field, and...
2
by: Nick | last post by:
In VS 2003 and VS 2005 beta 2 I am trying to write a simple program. It has a datagrid and a textbox along with a menubar. There is a command in the menu bar which is "Run Query" and has the...
1
by: Arjen | last post by:
Hello, In code view I use a tab as mark-up character. When I run my web application I can see the tabs with the source viewer in my browser. Is there a way to remove the tabs, extra spaces...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
6
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
2
by: Shafik | last post by:
Hello folks, I am having a really weird problem. Under windows XP, a simple C program designed to write to a file has been adding EXTRA characters to the output file. First I used "fwrite",...
3
by: afr02hrs | last post by:
Hi Experts I am trying to get the following program running correctly. I would like to be able to input a line of text containing multiple space characters between words and have the program...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.