Prevent the introduction of similar characters vb.net - vb.net

Is it possible to prevent entering characters that are already in the textbox?
like if I wrote in textbox
abcd
Then it is not allowed to write the same letters in it,
I searched and tried but could not find a way
All are about stopping letter gouging

It's pretty simple, just use the following code:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = TextBox1.Text.Contains(e.KeyChar)
End Sub
It'll help preventing typing the existing text(s) containing in a TextBox.
Hope it helps you!

Related

Textbox search accented characters problem?

I am looking for information on datagrid using textbox assign textchanged_event.
The problem I encountered was that when I typed the characters with a accented then datagrid would blink (it seems every change would be recorded in the textbox not just the final result).
Example: when typing "khang"
If I continue to type the letter "a", in english it will result in "khanga" and everything goes well, but in some languages it should be "khâng" and I see in textchanged it will take many steps as:
"khang" -> "khang•" -> "khang" -> "khan" -> "kha" -> "khâ" ->"khân"->"khâng"
in this moment, the datagrid will blink continuously.
what should I do to textchanged only handle the final result?
Thanks for advices!
You can't guarantee that you only handle the final value unless you use a Button.Click instead of a TextBox.TextChanged, or maybe force the user to hit Enter and handle the TextBox.KeyDown or the like. What you can do instead is ensure that you only act on the TextChanged if no other TextChanged has occurred for a specific length of time by using a Timer. That way, the delay is not so great that the user really feels it but it is long enough to allow multiple keystrokes without acting on them in between.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Start/restart the Timer every time the Text changes.
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Perform filter when time expires.
Timer1.Stop()
BindingSource1.Filter = $"SomeColumn LIKE '%{TextBox1.Text}%'"
End Sub
It's up to you want you set the Interval of the Timer to but I'd be looking at around 300. You can do a bit of experimentation to find what gives you the best balance.

Counting number of characters in TextBox

I have a TextBox by which if the user enters more than 10 characters it displays a MsgBox. That part works :D
The problem is the message also displays if the TextBox is empty and the user types the first character. I think thats because Null is seen as something greater than 10? but I'm not sure.
A) What is going on?
B) How can I fix this?
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
If TextBox3.Text.Length >= 10 Then
MsgBox("WARNING")
End If
End Sub
You can try this. By using trim, white space characters is ignored. For an example, if the user only entered 10 [Spacebar] keys it will trim it out.
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
If TextBox3.Text.Trim().Length() >= 10 Then
MsgBox("WARNING")
End If
End Sub

VB.net SendKeys to send keys combined not using "ALT" or "CTRL", like "{INS}(S)"

I need to send the keystroke combination INSERT + S keys simultaneously but it seems that VB only leaves use key combinations with ALT, CTRL and SHIFT.
Is there any way to do it?
I've tried:
{INS}(S)
{INS}S
and many others, but this does not work.
Thanks a lot.
It does. See:
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Had you tried "{INS}(S)" ?
UPDATE
I did the following test:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
SendKeys.Send("{Ins}S")
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
MsgBox(e.KeyCode)
End Sub
Look, I received in the KeyDown event 2 codes: the INS and the "S"
The problem is about the KeyDown event will file TWO TIMES, I mean, you cannot get the INS-S code with ONE reading.
So, you must consider have a FRIEND/PUBLIC variable to receive both codes and only after this, make the critic about these.
Good luck

Decimals where they shouldn't be

I nabbed a bit of code that prevents the user from entering non-numbers into a textbox, and monkeyed with it for a bit, trying to get it to include an exception for a decimal point. It didn't work, so I figured I'd just put the code back the way it was when I found it, because I don't yet know how to fix things if I break them.
Weird thing is, as soon as i put the code back to normal it suddenly started doing the thing I originally wanted it to, namely allowing the user to type a decimal into the textbox. This would be righteously excellent, if I could understand why in the name of Shiva my tiny program is doing something that it isn't programmed to do.
Have I somehow given birth to true AI and started the singularity?
EDIT (for clarity):
I am a total beginner.
I found this code that only allows numbers,
backspace, and delete to be added to a textbox.
I altered it, making assumptions about what it was doing, so that it would also allow a decimal point.
This didn't work.
I returned the code to how I found it.
Only after this point did it start allowing the decimal point.
This doesn't make sense, because it is now doing something that isn't in the code.
Does the same thing happen to you when you put the code into Visual Studio?
If so, why might this be happening?
Is the world going to end because my computer is interpreting my desires and performing them in spite of all of the rules of space, time, and logic?
Public Class Form1
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = Chr(Keys.Back) And Not e.KeyChar = Chr(Keys.Delete) Then
e.Handled = True
MessageBox.Show("numbers only", "baleted")
End If
End Sub
End Class
I miss-understood your question. I did a little rework on it. My bad!
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
If (e.KeyChar = "." OrElse e.KeyChar = Chr(Keys.Back) OrElse IsNumeric(e.KeyChar)) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
Not the prettiest thing I've ever barfed out, but basically this will check if the key pressed is a Decimal or a back space and allow those, I showed two different ways you can compare e.KeyChar in my code. If it isn't either on of those keys it will check if its a number using IsNumeric. If it's none of those things it will ignore it. Not my best work, but you can build upon this, make it your own, make prettier.
e.Keychar is information that is gathered when the keypress event happens. You can use the e.KeyChar to compare any button pressed on the keyboard and basically customize your textbox per keystroke. I hope this can get you started on the right path.
All else fails, search for key terms you find in code like e.KeyChar there is a ton of information out there.
Microsoft usually does a pretty decant job of explaining thing like e.KeyChar and e.Handled.
Best of luck!

Close Form VBA By Key

I'm looking for a routine that allows me to pick up a key combination such as Ctrl + L to allow me to close a form, all written in VB to someone've already shown how to manage?
This is not a very good question. You're asking users to do the coding for you, not showing an error/attempt that you need work with.
Please learn how to properly ask stackoverflow questions. Please refer to How To Ask
But, to answer:
You're looking for the key down event.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Control & Keys.L Then
Form1.Close()
End If
End Sub