Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am designing a program where only keyboard characters i mean only keyboard key values are accepted in text box and other are removed.

bu what happening is when textfile is getting loaded in textbox it gets formatted automatically.i mean lines get merged.

i dont want that lines should get merged.let the lines be same as input file but only characters which are not allowed should be deleted.

What I have tried:

Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ`~,;.:!""@#$%^&*()_-?/+=|\}]{[<>'"

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim theText As String = TextBox1.Text
Dim Letter As String
Dim sel_s As Integer = TextBox1.SelectionStart
Dim did_change As Boolean = False
For x As Integer = 0 To TextBox1.Text.Length - 1
Letter = TextBox1.Text.Substring(x, 1)
If charactersAllowed.Contains(Letter) = False Then
theText = theText.Replace(Letter, String.Empty)
did_change = True
End If
Next
TextBox1.Text = theText
If did_change = True Then
TextBox1.Select(sel_s, 0)
Else
TextBox1.Select(sel_s - 1, 0)
End If
End Sub
Posted
Updated 25-Jun-20 22:33pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

Not an answer, but suggestion...

I'd suggest to use ErrorProvider Class (System.Windows.Forms) | Microsoft Docs[^]
This enables you to Display Error Icons for Form Validation with ErrorProvider Component - Windows Forms | Microsoft Docs[^]

It would be more effective than validating text whenever key is pressed. All you need to do is to use Control.Validating Event (System.Windows.Forms) | Microsoft Docs[^]
When validation is passed then clear the error message from ErrorProvider object.

Here is basic example: Visual Studio 2010 ErrorProvider control in VB.NET[^]

As to your code... You can use Linq to get the list of not allowed chars. See:
Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ`~,;.:!""@#$%^&*()_-?/+=|\}]{[<>'"
Dim enteredText = "Kraków, Łódź, Poznań and Wrocław - cities of Poland with citizens between 500,000 and 999,999!"

Dim result = enteredText _
	.Where(Function(x) Not charactersAllowed.Any(Function(y) y=x)) _
	.Select(Function(x) x) _
	.ToList()
	
Console.WriteLine($"'{String.Join(", ", result)}' are not allowed!")
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

The problem is that when you load the text from your file into a Textbox it causes a TextChanged event - which removes characters which aren't in your list. And that list doesn't include a Newline character, so you remove them all and get a single line of text as a result. Add the newline to your "allowed" list and you should be OK.

But do also check that your TextBox is marked as MultiLine - if it isn't, then newlines won't have any effect anyway!
   
Comments
shaileshshinde 26-Jun-20 3:41am
   
how to add new line to "allowed " list sir?

this is my list;

Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ`~,;.:!""@#$%^&*()_-?/+=|\}]{[<>'"
OriginalGriff 26-Jun-20 3:45am
   
Try including this:
https://docs.microsoft.com/en-us/dotnet/api/system.environment.newline?view=netcore-3.1
shaileshshinde 26-Jun-20 3:54am
   
thanks issue resolved resolved.

but if bulk files has to be done in same way then what code will be added in button click event for the same above type of output?
OriginalGriff 26-Jun-20 5:05am
   
First off, don't do long operations in a Click handler - you will lock up the UI until they are all complete. Move long operations to a different thread - I'd suggest looking at a BackgroundWorker as they are easy to use and provide progress reporting to the UI.

Then it's trivial to make it work for multiple files: move your code into a method that takes the path as a parameter, and call it repeatedly.
shaileshshinde 26-Jun-20 5:48am
   
any sample code sir?
as i am not getting what you are saying exactly
OriginalGriff 26-Jun-20 6:19am
   
You are kidding, right?
You need a code sample to write a method that takes a string parameter?
shaileshshinde 26-Jun-20 6:22am
   
so sorry.i got it.i forgot.
OriginalGriff 26-Jun-20 6:58am
   
It happens.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100