why e.Keychar is not working in keypress event? - vb.net

I have a problem regarding the KeyChar e.KeyChar. It is not working. I want to get the value of e from, please have a look and your comments are highly appreciated.
Private Sub txtTax_KeyPress(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtTax.KeyPress
If (Strings.Asc(e.KeyChar) > 0) Then
Try
If (Me.conn.State = ConnectionState.Closed) Then
Me.conn.Open()
End If
Dim adapter As New SqlDataAdapter(("Select * from TaxGroup where TaxName='" & Me.txtTax.Text & "'"), Me.conn)
Dim dataSet As New DataSet
adapter.Fill(dataSet, "TaxGroup")
Me.dgrdTax.DataSource = dataSet.Tables.Item(0)
Me.dgrdTax.Visible = True
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Interaction.MsgBox(exception.Message, MsgBoxStyle.ApplicationModal, Nothing)
ProjectData.ClearProjectError()
End Try
End If
End Sub

Your event declaration is wrong. The correct signature is:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs)
Handles TextBox1.KeyPress
KeyChar is not a part of EventArgs, which is largely just a base class.
I am guessing maybe your thought you needed to type it in yourself. Just pick the control from the left drop down and the event from the right and the IDE will create it for you. I am not sure how that could happen otherwise.

You can also try this:
If Not (Asc(e.KeyChar) = 8) Then
Dim allowedChars As String = "1234567890" ' allowed In textbox
If Not allowedChars.Contains(e.KeyChar.ToString.ToLower) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End If

Related

VB Entering a Non_Numeric value crashes the program

Please consider adding a description to this question to attract more helpful responses.
Public Class Form1
Private Sub BtnCalculateRevenue_Click(sender As Object, e As EventArgs) Handles BtnCalculateRevenue.Click
Dim intvalue As Integer = CInt(TxtInputClassA.Text)
Dim intvalue2 As Integer = CInt(TxtInputClassB.Text)
Dim intvalue3 As Integer = CInt(TxtinputClassC.Text)
Dim total As Double
Try
LblStatus.Text = String.Empty
LblClassAResult.Text = (intvalue * 15).ToString("c")
LblClassBResult.Text = (intvalue2 * 12).ToString("c")
LblClassCResult.Text = (intvalue3 * 9).ToString("c")
total = CDbl((intvalue * 15) + (intvalue2 * 12) + (intvalue3 * 9))
LblTotal.Text = total.ToString("c")
Catch
LblStatus.Text = "Please Enter a Number"
End Try
End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
TxtInputClassA.Clear()
TxtInputClassB.Clear()
TxtinputClassC.Clear()
LblClassAResult.Text = String.Empty
LblClassBResult.Text = String.Empty
LblClassCResult.Text = String.Empty
LblTotal.Text = String.Empty
End Sub
End Class
VB Entering a Non_Numeric value crashes the program
Validation is built right into Windows Forms so you should make use of it. If you want to force the user to enter numbers in each of three TextBoxes then you can do this:
Private Sub TextBoxes_Validating(sender As Object, e As ComponentModel.CancelEventArgs) Handles TextBox3.Validating, TextBox2.Validating, TextBox1.Validating
Dim tb = DirectCast(sender, TextBox)
If Not Integer.TryParse(tb.Text, Nothing) Then
'Select all the invalid text and highlight it.
tb.SelectAll()
tb.HideSelection = False
MessageBox.Show("Please enter a whole number",
"Invalid Input",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
'Remove highlight when TextBox is not focused.
tb.HideSelection = True
'Don't let the control lose focus while data is invalid.
e.Cancel = True
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ValidateChildren() Then
'All data is valid so proceed.
Dim n1 = CInt(TextBox1.Text)
Dim n2 = CInt(TextBox2.Text)
Dim n3 = CInt(TextBox3.Text)
'...
End If
End Sub
The ValidateChildren method will raise the Validating event for every control on the form and return False if any fail validation, i.e. e.Cancel is set to True in any event handlers, so that ensures that even controls that never received focus will be validated before the data is used.
Its bombing out on your cast "CInt(*value*)" so you can fix the code a couple ways. You can move your try above the casts like...
Try
Dim intvalue As Integer = CInt(TxtInputClassA.Text)
Dim intvalue2 As Integer = CInt(TxtInputClassB.Text)
Dim intvalue3 As Integer = CInt(TxtinputClassC.Text)
Dim total As Double
LblStatus.Text = String.Empty
You can do a data validation on your inputs and exit if they aren't all numeric (put this above your Dim intvalue code)
For Each value As String In {TxtInputClassA.Text, TxtInputClassA.Text, TxtInputClassA.Text}
If Not IsNumeric(TxtInputClassA.Text) Then
LblStatus.Text = "Please Enter a Number"
Exit Sub
End If
Next
Instead of casting as an int, use the tryparse method on Int32...
Dim intvalue As Integer
If Not Int32.TryParse(TxtInputClassA.Text, intvalue) Then
Exit Sub
End If
Or you could intercept the keypresses on each text box so that only numbers can be entered
Private Sub TxtInputClassA_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtInputClassA.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub
You can make that routine universal and append the event handler for all three text boxes like...
Private Sub EnforceOnlyNumericKeyPresses(sender As Object, e As KeyPressEventArgs) Handles TxtInputClassA.KeyPress, TxtInputClassB.KeyPress, TxtInputClassC.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub
Choose your favorite or do all of them, lots of choices.
Replace your textboxes with NumericUpDown controls and retrieve their .Value for your calculation. NUDs don't allow non numeric input and can have a fixed number of decimal places which may also be useful in a financial context

Visual basic If and else not working 2015

I am a newbie and I am facing some problem with the 'if' and 'else' function of visual basic.
I have added a button labeled as Save, its code is as given below:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If CheckBox1.Checked = True Then
Dim savedata7 As New System.IO.StreamWriter("dat0C.bin")
savedata7.WriteLine("checked")
savedata7.Close()
End If
If CheckBox1.Checked = False Then
Dim savedata7 As New System.IO.StreamWriter("dat0C.bin")
savedata7.WriteLine("unchecked")
savedata7.Close()
End If
If CheckBox2.Checked = True Then
Dim savedata8 As New System.IO.StreamWriter("dat1C.bin")
savedata8.WriteLine("checked")
savedata8.Close()
End If
If CheckBox2.Checked = False Then
Dim savedata8 As New System.IO.StreamWriter("dat1C.bin")
savedata8.WriteLine("unchecked")
savedata8.Close()
End If
End Sub
And another button labeled Load, its code is given below:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim dataloader As New System.IO.StreamReader("dat0C.bin")
Label17.Text = dataloader.ReadToEnd
dataloader.Close()
Catch ex As Exception
End Try
Try
Dim dataloader As New System.IO.StreamReader("dat1C.bin")
label10.Text = dataloader.ReadToEnd
dataloader.Close()
Catch ex As Exception
End Try
If Label17.Text = "checked" Then
CheckBox1.Checked = True
Else
CheckBox1.Checked = False
End If
If Label10.Text = "checked" Then
CheckBox2.Checked = True
Else
CheckBox1.Checked = False
End If
End Sub
The Save button(Button5) is working perfectly fine, but the Load button(Button1) is not working as it should, on clicking Button1 the files 'dat0C.bin' and 'dat1c.bin' are loaded in 'Label17' and 'Label10' respectively but it is not working(i.e. it is not Checking the CheckBoxes1 or 2) from
If Label17.Text = "checked" Then
CheckBox1.Checked = True
Else
CheckBox1.Checked = False
End If
If Label10.Text = "checked" Then
CheckBox2.Checked = True
Else
CheckBox1.Checked = False
End If
Please correct me, If I'm doing anything wrong in it.
This happens because in the reading code you use ReadToEnd instead of ReadLine.
This method reads back the newline character added by WriteLine and doesn't strip it away as ReadLine
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Using dataloader = New System.IO.StreamReader("dat0C.bin")
Label17.Text = dataloader.ReadLine()
End Using
Catch ex As Exception
MessageBox.Show("Exception=" & ex.Message)
End Try
....
A part from this you really should put the creation of disposable objects like the reader and the write inside a using statement to be sure that these objects are properly closed and disposed ALSO in case of exceptions.
Said that, your code could be simplified a lot if you use the static methods of the File class
WRITING
File.WriteAllText("dat0C.bin", If(CheckBox1.Checked, "checked", "unchecked"))
File.WriteAllText("dat1C.bin", If(CheckBox2.Checked, "checked", "unchecked"))
READING
Label17.Text = File.ReadAllText("dat0C.bin")
Label10.Text = File.ReadAllText("dat1C.bin")
CheckBox1.Checked = (Label17.Text = "checked")
CheckBox2.Checked = (Label10.Text = "checked")
Asside from not naming your controls, and not disposing properly of your StreamReader or StreamWriter instances the reason this is not working correctly is because when you write your file you call WriteLine - which appends a carriage return and line feed onto the end of what you've written.
You then read the file, and put the content in a label, which itself will render the CR/LF albeit not visible to you. You then compare to the Text of this label but without the CR/LF - so that comparison evaluates false.

Is there any way for Retrieve Record Value from Table Column , Loop and Loop in vb.net

Sorry for my poor English
When the user clicks the Start button, I am trying to display all of the records from the Phone column in the Textbox1 control. I want to see all those records pass into Textbox1 While it is processing the For loop. But it is currently processing very fast so that I only see the last record in Textbox1. What i'm going wrong?
While it is processing the For loop, I change the Start button to a Stop button. So when I click the button and it currently has a Text value equal to "Stop", I want it to skip the For loop and pass the value from TextBox1 to my FirstWin. And then it should change the BtnStart.Text back to "Start"
Here's my code:
Public Class PhoneFortune
Dim CN As OleDbConnection
Dim CM As OleDbCommand
Dim DA As OleDbDataAdapter
Dim DT As New DataTable
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
If BtnStart.Text = "Start" Then
CM = New OleDbCommand("SELECT * FROM TblPhoneNumber", CN)
DA = New OleDbDataAdapter(CM)
DA.Fill(DT)
For i = 0 To DT.Rows.Count - 1
TextBox1.Text = DT.Rows(i)("Phone")
Next
BtnStart.Text = "Stop"
End If
If BtnStart.Text = "Stop" Then
If FirstWin.Text = "" Then
FirstWin.Text = TextBox1.Text
BtnStart.Text = "Start"
ElseIf SecondWin.Text = "" Then
SecondWin.Text = TextBox1.Text
BtnStart.Text = "Start"
ElseIf ThirdWin.Text = "" Then
ThirdWin.Text = TextBox1.Text
BtnStart.Text = "Start"
End If
End If
End Sub
Private Sub PhoneFortune_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CN = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\db.accdb;Jet OLEDB:Database Password=12345;")
CN.Open()
End Sub
End Class
I hope we are on the same page but if u want to slow down the process for a bit u use these two lines of code within the FOR loop
System.Threading.Thread.Sleep(1000)
Me.Refresh() Where 1000 is one second(value is represented in milliseconds).
Now what is that FirstWin.Text="" etc used for? Are you trying to start the looping,then change the text to "stop" so that when the user clicks stops it stops at the current record? Go to youtube and look for multithreading videos,not sure how to help u on that one hope the rest helps. If this is useful please acknowledge
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Start" Then
If TextBox1.Text = "" Then
t1 = New Thread(AddressOf Me.PhoneThread)
t1.Start()
Button1.Text = "Stop"
Else
t1.Resume()
Button1.Text = "Stop"
End If
Else 'Click Stop
t1.Suspend()
Button1.Text = "Start"
If FirstWin.Text = "" Then
FirstWin.Text = TextBox1.Text
ElseIf SecondWin.Text = "" Then
SecondWin.Text = TextBox1.Text
ElseIf ThirdWin.Text = "" Then
ThirdWin.Text = TextBox1.Text
End If
End If
End Sub
Sub PhoneThread()
'Dim ThreadsArray As List(Of Threading.Thread) = New List(Of Threading.Thread)
Dim s As Integer
'MsgBox(DT.Rows.Count)
For s = 0 To DT.Rows.Count
If s = DT.Rows.Count Then
s = 0
Else
TextBox1.Text = DT.Rows(s)("Phone")
' ThreadsArray.Add(t1)
Thread.Sleep(19)
'TextBox1.Text = s.ToString()
Me.Refresh()
End If
Next
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
Me.Close()
End Sub
For something simple like this, System.Threading.Thread.Sleep(100) (to block the thread) followed by Application.DoEvents() (to process any button clicks) seems like it would do the trick.
For something more complicated, this approach will result in re-entrancy and difficult-to-reproduce bugs; in that case, you definitely need asynchronous programming.

VB.net Need Text Box to Only Accept Numbers

I'm fairly new to VB.net (self taught) and was just wondering if someone out there could help me out with some code. I'm not trying to do anything too involved, just have a TextBox that accepts a numeric value from 1 to 10. I don't want it to accept a string or any number above 10. If someone types a word or character an error message will appear, telling him to enter a valid number. This is what I have; obviously it's not great as I am having problems. Thanks again to anyone who can help.
If TxtBox.Text > 10 Then
MessageBox.Show("Please Enter a Number from 1 to 10")
TxtBox.Focus()
ElseIf TxtBox.Text < 10 Then
MessageBox.Show("Thank You, your rating was " & TxtBox.Text)
Total = Total + 1
ElseIf IsNumeric(TxtBox.Text) Then
MessageBox.Show("Thank you, your rating was " & ValueTxtBox.Text)
End If
ValueTxtBox.Clear()
ValueTxtBox.Focus()
You can do this with the use of Ascii integers. Put this code in the Textbox's Keypress event. e.KeyChar represents the key that's pressed. And the the built-in function Asc() converts it into its Ascii integer.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'97 - 122 = Ascii codes for simple letters
'65 - 90 = Ascii codes for capital letters
'48 - 57 = Ascii codes for numbers
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
This is what I did in order to handle both key entry and copy/paste.
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub
Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox.TextChanged
Dim digitsOnly As Regex = New Regex("[^\d]")
TextBox.Text = digitsOnly.Replace(TextBox.Text, "")
End Sub
If you want to allow decimals and negative amount, add
AndAlso Not e.KeyChar = "." AndAlso Not e.keyChar = "-"
to the if statement in the KeyPress section.
Simplest ever solution for TextBox Validation in VB.NET
First, add new VB code file in your project.
Go To Solution Explorer
Right Click to your project
Select Add > New item...
Add new VB code file (i.e. example.vb)
or press Ctrl+Shift+A
COPY & PASTE following code into this file and give it a suitable name. (i.e. KeyValidation.vb)
Imports System.Text.RegularExpressions
Module Module1
Public Enum ValidationType
Only_Numbers = 1
Only_Characters = 2
Not_Null = 3
Only_Email = 4
Phone_Number = 5
End Enum
Public Sub AssignValidation(ByRef CTRL As Windows.Forms.TextBox, ByVal Validation_Type As ValidationType)
Dim txt As Windows.Forms.TextBox = CTRL
Select Case Validation_Type
Case ValidationType.Only_Numbers
AddHandler txt.KeyPress, AddressOf number_Leave
Case ValidationType.Only_Characters
AddHandler txt.KeyPress, AddressOf OCHAR_Leave
Case ValidationType.Not_Null
AddHandler txt.Leave, AddressOf NotNull_Leave
Case ValidationType.Only_Email
AddHandler txt.Leave, AddressOf Email_Leave
Case ValidationType.Phone_Number
AddHandler txt.KeyPress, AddressOf Phonenumber_Leave
End Select
End Sub
Public Sub number_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim numbers As Windows.Forms.TextBox = sender
If InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then
e.KeyChar = Chr(0)
e.Handled = True
End If
End Sub
Public Sub Phonenumber_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim numbers As Windows.Forms.TextBox = sender
If InStr("1234567890.()-+ ", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then
e.KeyChar = Chr(0)
e.Handled = True
End If
End Sub
Public Sub OCHAR_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If InStr("1234567890!##$%^&*()_+=-", e.KeyChar) > 0 Then
e.KeyChar = Chr(0)
e.Handled = True
End If
End Sub
Public Sub NotNull_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim No As Windows.Forms.TextBox = sender
If No.Text.Trim = "" Then
MsgBox("This field Must be filled!")
No.Focus()
End If
End Sub
Public Sub Email_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Email As Windows.Forms.TextBox = sender
If Email.Text <> "" Then
Dim rex As Match = Regex.Match(Trim(Email.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase)
If rex.Success = False Then
MessageBox.Show("Please Enter a valid Email Address", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Email.BackColor = Color.Red
Email.Focus()
Exit Sub
Else
Email.BackColor = Color.White
End If
End If
End Sub
End Module
Now use following code to Form Load Event like below.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AssignValidation(Me.TextBox1, ValidationType.Only_Digits)
AssignValidation(Me.TextBox2, ValidationType.Only_Characters)
AssignValidation(Me.TextBox3, ValidationType.No_Blank)
AssignValidation(Me.TextBox4, ValidationType.Only_Email)
End Sub
Done..!
You must first validate if the input is actually an integer. You can do it with Integer.TryParse:
Dim intValue As Integer
If Integer.TryParse(TxtBox.Text, intValue) AndAlso intValue > 0 AndAlso intValue < 11 Then
MessageBox.Show("Thank You, your rating was " & TxtBox.Text)
Else
MessageBox.Show("Please Enter a Number from 1 to 10")
End If
Try this:
Private Sub txtCaseID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCaseID.KeyPress
If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then e.KeyChar = ""
End Sub
You could avoid any code by using a NumericUpDown control rather than a text box, this automatically only allows numbers and has a max and min.
It also allow accessing the number directly with NumericUpDown1.Value as well as using up and down arrows to set the number.
Also if a number higher/over the max is entered it will jump to the nearest allowed number.
Private Sub MyTextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyTextBox.KeyPress
If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
e.Handled = True
End If
End Sub
Private Sub textBox5_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles textBox5.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
Private Sub Data_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Data.KeyPress
If (Not e.KeyChar = ChrW(Keys.Back) And ("0123456789.").IndexOf(e.KeyChar) = -1) Or (e.KeyChar = "." And Data.Text.ToCharArray().Count(Function(c) c = ".") > 0) Then
e.Handled = True
End If
End Sub
Dim ch(10) As Char
Dim len As Integer
len = TextBox1.Text.Length
ch = TextBox1.Text.ToCharArray()
For i = 0 To len - 1
If Not IsNumeric(ch(i)) Then
MsgBox("Value you insert is not numeric")
End If
Next
If Not Char.IsNumber(e.KeyChar) AndAlso Not e.KeyChar = "." AndAlso Not Char.IsControl(e.KeyChar) Then
e.KeyChar = ""
End If
This allow you to use delete key and set decimal points
I know this post is old but I wanted to share something I have implemented to turn a TextBox into what I call an IntBox.
First you need to make an extension with:
<Runtime.CompilerServices.Extension()> _
Public Function HandledStringtoInteger(s As String) As Integer
Try
If s = String.Empty Then
Return 0
Else
Return Integer.Parse(s)
End If
Catch
Dim result As String = String.Empty
Dim ReturnInt As Integer
Dim Parsed As Integer
For Each Character In s.ToCharArray
If Character = "-" Then
If s.Substring(0, 1).ToString <> "-" Then
result = Character + result
End If
End If
If Character = "." Then
Exit For
End If
If Integer.TryParse(Character, Parsed) Then
result = result + Parsed.ToString
End If
Next
If result <> String.Empty Then
If Integer.TryParse(result, ReturnInt) Then
Return Integer.Parse(ReturnInt)
Else
If Double.Parse(result) > Double.Parse(Integer.MaxValue.ToString) Then
Return Integer.MaxValue
ElseIf Double.Parse(result) < Double.Parse(Integer.MinValue.ToString) Then
Return Integer.MinValue
Else
Return Integer.Parse(ReturnInt)
End If
End If
Else
Return 0
End If
End Try
End Function
Then make a TextChanged event sub:
Private Sub TextBox_to_IntBox(sender As Object, e As TextChangedEventArgs) Handles YourTextBox.TextChanged
If DirectCast(sender, TextBox).IsKeyboardFocused Then
DirectCast(sender, TextBox).Text = DirectCast(sender, TextBox).Text.HandledStringtoInteger
DirectCast(sender, TextBox).CaretIndex = DirectCast(sender, TextBox).Text.Length
End If
End Sub
Then whenever the user enters text it evaluates the string and only returns numeric values that are within the bounds of a standard Integer. With the "-" character you can change the integer from positive to negative and back again.
If anyone sees anything that can improve this code let me know but my tests show this works fantastic to make an IntBox.
EDIT:
I found another method that can work if you use properties in your code. (Note this will need a separate property per TextBox)
First create the property:
Public Class Properties
Implement INotifyPropertyChanged
Private _Variable as Integer
Public Property YourProperty as Object
get
Return _Variable
end get
set(value as Object)
_Variable = value.ToString.ToInteger 'I will give the ToInteger extension code later
end set
end property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub OnPropertyChange(ByVal e As PropertyChangedEventArgs)
If Not PropertyChangedEvent Is Nothing Then
RaiseEvent PropertyChanged(Me, e)
End If
End Sub
End Class
Then make the binding in your window's main class:
Public WithEvents _YourVariable as New Properties
Public Sub New()
InitializeComponent()
With YourTextBox
.SetBinding(Textbox.TextProperty, New Binding("YourProperty"))
.DataContext = _YourVariable
End With
End Sub
Finally here is the ToInteger Extension Code I set up:
''' <summary>
''' Handles conversion of variable to Integer.
''' </summary>
''' <param name="X"></param>
''' <param name="I">Returned if conversion fails.</param>
''' <returns>Signed 32bit Integer</returns>
''' <remarks></remarks>
<Runtime.CompilerServices.Extension()> _
Public Function toInteger(Of T)(ByRef X As T, Optional I As Integer = 0) As Integer
Dim S As String = X.ToString
Try
If S = String.Empty Then
Return I
Else
Return Integer.Parse(S)
End If
Catch
Dim result As String = String.Empty
Dim ReturnInt As Integer
Dim Parsed As Byte
For Each Character In S.ToCharArray
If Character = "-" Then
If S.Substring(0, 1).ToString <> "-" Then
result = Character + result
End If
End If
If Character = "." Then
Exit For
End If
If Byte.TryParse(Character, Parsed) Then
result = result + Parsed.ToString
End If
Next
If result <> String.Empty Then
If Integer.TryParse(result, ReturnInt) Then
Return Integer.Parse(ReturnInt)
Else
If Double.Parse(result) > Double.Parse(Integer.MaxValue.ToString) Then
Return Integer.MaxValue
ElseIf Double.Parse(result) < Double.Parse(Integer.MinValue.ToString) Then
Return Integer.MinValue
Else
Return Integer.Parse(ReturnInt)
End If
End If
Else
Return I
End If
End Try
End Function
With all these combined whenever they type something into the box it will act as if it were a textbox but when they change focus the ToInteger extension will set the value as an integer into the property and return it to the textbox.
Meaning that if the operator entered "-1w3" after focus changes it will return as "-13" automatically.
This may be too late, but for other new blood on VB out there, here's something simple.
First, in any case, unless your application would require, blocking user's key entry is somehow not a good thing to do, users may misinterpret the action as problem on the hardware keyboard and at the same time may not see where their keypreesed entry error came from.
Here's a simple one, let user's freely type their entry then trap the error later:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim theNumber As Integer
Dim theEntry As String = Trim(TextBox1.Text)
'This check if entry can be converted to
'numeric value from 0-10, if cannot return a negative value.
Try
theNumber = Convert.ToInt32(theEntry)
If theNumber < 0 Or theNumber > 10 Then theNumber = -1
Catch ex As Exception
theNumber = -1
End Try
'Trap for the valid and invalid numeric number
If theNumber < 0 Or theNumber > 10 Then
MsgBox("Invalid Entry, allows (0-10) only.")
'entry was invalid return cursor to entry box.
TextBox1.Focus()
Else
'Entry accepted:
' Continue process your thing here...
End If
End Sub
I have the solution where it will check whether the text is range 1 to 10 : [1-9] will check for the range from 1 to 9. I use one more condition to check for 10.
If txtBox.Text Like "[1-9]" Or txtBox.Text Like "10" Then
MessageBox.Show("true")
Else
MessageBox.Show("false")
End If
First of all set the TextBox's MaxLength to 2 that will limit the amount of text entry in your TextBox. Then you can try something like this using the KeyPress Event. Since you are using a 2 digit maximum (10) you will need to use a Key such as Enter to initiate the check.
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tb As TextBox = CType(sender, TextBox)
If Not IsNumeric(e.KeyChar) Then 'Check if Numeric
If Char.IsControl(e.KeyChar) Then 'If not Numeric Check if a Control
If e.KeyChar = ChrW(Keys.Enter) Then
If Val(tb.Text) > 10 Then 'Check Bounds
tb.Text = ""
ShowPassFail(False)
Else
ShowPassFail(True)
End If
e.Handled = True
End If
Exit Sub
End If
e.Handled = True
ShowPassFail(False)
End If
End Sub
Private Sub ShowPassFail(pass As Boolean)
If pass Then
MessageBox.Show("Thank you, your rating was " & TextBox1.Text)
Else
MessageBox.Show("Please Enter a Number from 1 to 10")
End If
TextBox1.Clear()
TextBox1.Focus()
End Sub
Public Function Isnumber(ByVal KCode As String) As Boolean
If Not Isnumeric(KCode) And KCode <> ChrW(Keys.Back) And KCode <> ChrW(Keys.Enter) And KCode <> "."c Then
MsgBox("Please Enter Numbers only", MsgBoxStyle.OkOnly)
End If
End Function
Private Sub txtBalance_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtBalance.KeyPress
If Not Isnumber(e.KeyChar) Then
e.KeyChar = ""
End If
End Sub
This worked for me... just clear the textbox completely as non-numeric keys are pressed.
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
If IsNumeric(TextBox2.Text) Then
'nada
Else
TextBox2.Clear()
End If
End Sub
Copy this function in any module inside your vb.net project.
Public Function MakeTextBoxNumeric(kcode As Integer, shift As Boolean) As Boolean
If kcode >= 96 And kcode <= 105 Then
ElseIf kcode >= 48 And kcode <= 57
If shift = True Then Return False
ElseIf kcode = 8 Or kcode = 107 Then
ElseIf kcode = 187 Then
If shift = False Then Return False
Else
Return False
End If
Return True
End Function
Then use this function inside your textbox_keydown event like below:
Private Sub txtboxNumeric_KeyDown(sender As Object, e As KeyEventArgs) Handles txtboxNumeric.KeyDown
If MakeTextBoxNumeric(e.KeyCode, e.Shift) = False Then e.SuppressKeyPress = True
End Sub
And yes. It works 100% :)
You can use the onkeydown Property of the TextBox for limiting its value to numbers only.
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (!(event.keyCode>=65) && event.keyCode!=32);"></asp:TextBox>
!(keyCode>=65) check is for excludng Alphabets.
keyCode!=32 check is for excluding Space character inbetween the numbers.
If you want to exclude Symbols also from entering into the textbox, then include the below condition also in the 'onkeydown' property.
!(event.shiftKey && (event.keyCode >= 48 && event.keyCode <= 57))
Thus the TextBox will finally become
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (!(event.keyCode>=65) && event.keyCode!=32 && !(event.shiftKey && (event.keyCode >= 48 && event.keyCode <= 57)));"></asp:TextBox>
Explanation:
KeyCode for 'a' is '65' and 'z' is '90'.
KeyCodes from '90' to '222' which are other symbols are also not needed.
KeyCode for 'Space' Key is '32' which is also not needed.
Then a combination of 'Shift' key and 'Number' keys (which denotes Symbols) also not needed. KeyCode for '0' is '48' and '9' is '57'.
Hence all these are included in the TextBox declaration itself which produces the desired result.
Try and see.
This was my final... It gets around all the type issues also:
Here is a simple textbox that requires a number:
public Sub textbox_memorytotal_TextChanged(sender As Object, e As EventArgs) Handles textbox_memorytotal.TextChanged
TextboxOnlyNumbers(sender)
End Sub
and here is the procedure that corrects all bad input:
Public Sub TextboxOnlyNumbers(ByRef objTxtBox As TextBox)
' ONLY allow numbers
If Not IsNumeric(objTxtBox.Text) Then
' Don't process things like too many backspaces
If objTxtBox.Text.Length > 0 Then
MsgBox("Numerical Values only!")
Try
' If something bad was entered delete the last character
objTxtBox.Text = objTxtBox.Text.Substring(0, objTxtBox.Text.Length - 1)
' Put the cursor and the END of the corrected number
objTxtBox.Select(objTxtBox.Text.Length + 1, 1)
Catch ex As Exception
End Try
End If
End If
End Sub
Use this in your Textbox Keydown event.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
'you can enter decimal "if nonNumberEntered(e, TextBox1, True) then"
'otherwise just numbers "if nonNumberEntered(e, TextBox1) then"
If nonNumberEntered(e, TextBox1, True) Then
e.SuppressKeyPress = True
End If
If e.KeyCode = Keys.Enter Then
'put your code here
End If
End Sub
Copy this function in any module inside your vb.net project.
Public Function nonNumberEntered(ByVal e As System.Windows.Forms.KeyEventArgs, _
ByVal ob As TextBox, _
Optional ByVal decim As Boolean = False) As Boolean
nonNumberEntered = False
If decim Then
' Determine whether the keystroke is a number from the top of the keyboard.
If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
If e.KeyCode <> Keys.Decimal And e.KeyCode <> Keys.OemPeriod Then
If e.KeyCode <> Keys.Divide And e.KeyCode <> Keys.OemQuestion Then
' Determine whether the keystroke is a backspace.
If e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Delete _
And e.KeyCode <> Keys.Left And e.KeyCode <> Keys.Right Then
' A non-numerical keystroke was pressed.
nonNumberEntered = True
End If
ElseIf ob.Text.Contains("/") Or ob.Text.Length = 0 Then
nonNumberEntered = True
End If
ElseIf ob.Text.Contains(".") Or ob.Text.Length = 0 Then
nonNumberEntered = True
End If
End If
End If
Else
' Determine whether the keystroke is a number from the top of the keyboard.
If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
' Determine whether the keystroke is a backspace.
If e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Delete _
And e.KeyCode <> Keys.Left And e.KeyCode <> Keys.Right Then
' A non-numerical keystroke was pressed.
nonNumberEntered = True
End If
End If
End If
End If
'If shift key was pressed, it's not a number.
If Control.ModifierKeys = Keys.Shift Then
nonNumberEntered = True
End If
End Function
This will allow numbers like 2/4 or numbers like 3.5 to be entered in your textbox if using decim "nonNumberEntered(e,Textbox1, True)".
Allows only numbers to be entered in textbox if using "nonNumberEntered(e,Textbox1, False)" or "nonNumberEntered(e,Textbox1)".
Edit: added text.
I had a similar use requirement recently for a TextBox which could only take numbers.
In the end I used a MaskedTextBox instead of a TextBox. You define a "mask" for the textbox and it will only accept characters which you have defined - in this case, numbers. The downside is that it leaves a bit of an ugly line within the TextBox;
What I loved about the MaskedTextBox was it was so customisable. If, for whatever reason you wanted a TextBox to only accept an input in the format of 3 ints followed by 2 letters, all you need to do is set the TextMask to 000LL. There are a load of pre-defined masks within Visual Studio, and the full documentation can be found here.
Now, I know this doesn't fully solve your issue, but the use of a MaskedTextBox takes away a huge part of the complexity of the problem. You can now guarantee that the contents of the MaskedTextBox will only ever be an Int, allowing you to run a simple If statement to ensure the value is =<10
I know this post is old but I want to share my code.
Private Sub txtbox1_TextChanged(sender As Object, e As EventArgs) Handles txtbox1.TextChanged
If txtbox1.Text.Length > 0 Then
If Not IsNumeric(txtbox1.Text) Then
Dim sel As Integer = txtbox1.SelectionStart
txtbox1.Text = txtbox1.Text.Remove(sel - 1, 1)
txtbox1.SelectionStart = sel - 1
End If
End If
End Sub
On each entry in textbox (event - Handles RestrictedTextBox.TextChanged), you can do a try to caste entered text into integer, if failure occurs, you just reset the value of the text in RestrictedTextBox to last valid entry (which gets constantly updating under the temp1 variable).
Here's how to go about it. In the sub that loads with the form (me.load or mybase.load), initialize temp1 to the default value of RestrictedTextBox.Text
Dim temp1 As Integer 'initialize temp1 default value, you should do this after the default value for RestrictedTextBox.Text was loaded.
If (RestrictedTextBox.Text = Nothing) Then
temp1 = Nothing
Else
Try
temp1 = CInt(RestrictedTextBox.Text)
Catch ex As Exception
temp1 = Nothing
End Try
End If
At any other point in form:
Private Sub textBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles RestrictedTextBox.TextChanged
Try
temp1 = CInt(RestrictedTextBox.Text) 'If user inputs integer, this will succeed and temp will be updated
Catch ex As Exception
RestrictedTextBox.Text = temp1.ToString 'If user inputs non integer, textbox will be reverted to state the state it was in before the string entry
End Try
End Sub
The nice thing about this is that you can use this to restrict a textbox to any type you want: double, uint etc....
every text box has a validating and validated event you can use then as follows :-
Private Sub PriceTxt_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles PriceTxt.Validating
If Not IsNumeric(PriceTxt.Text) Then
PriceTxt.BackColor = Color.Red
MsgBox("The Price Should Be Numeric Only , Enter Again", vbCritical)
PriceTxt.Text = ""
PriceTxt.BackColor = Color.White
End If
End Sub
I know it's old.. I'll just leave this code here for the sake of convenience.
Integer only:
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
With TextBox1
If IsNumeric(.Text) Then .Text = .Text.Select(Function(x) If(IsNumeric(x), x, "")) : .SelectionStart = .TextLength
End With
' etc..
End Sub
Accepts Double:
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
With TextBox1
If IsNumeric(.Text) Then .Text = .Text.Select(Function(x) If(IsNumeric(x) Or x = ".", x, "")) : .SelectionStart = .TextLength
End With
' etc..
End Sub
Accepts basic operations + - * /, parentheses ( ) [ ] { } and Double:
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
With TextBox1
If IsNumeric(.Text) Then .Text = .Text.Select(Function(x) If(IsNumeric(x) Or ".+-*/()[]{}".Contains(x), x, "")) : .SelectionStart = .TextLength
End With
' etc..
End Sub
You Can use Follow code Textbox Keypress Event:
Private Sub txtbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox1.KeyPress
Try
If Val(txtbox1.text) < 10 Then
If Char.IsLetterOrDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
e.Handled = True
End If
Else
e.Handled = True
End If
Catch ex As Exception
ShowException(ex.Message, MESSAGEBOX_TITLE, ex)
End Try
End Sub
This code allow numbers only and you can enter only number between 1 to 10.
Very simple piece of code that works for me.
Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textbox1.KeyPress
If Asc(e.KeyChar) > 58 Then
e.KeyChar = ""
End If
End Sub
Here's what works for me. It allows backspace, del, as well as numbers from the top row of the keyboard and the number pad. It excludes the + and - signs.
Private Sub tbMQTTPort_KeyDown(sender As Object, e As KeyEventArgs) Handles tbMQTTPort.KeyDown
Dim kc As New KeyConverter
Dim Regex = New Regex("[^0-9]+")
e.Handled = Regex.IsMatch(kc.ConvertToInvariantString(e.Key).Replace("NumPad", ""))
End Sub

.NET fulltext autocomplete in a combobox. Any performance-positive way of overriding listitems?

I'm struggling to meet a demand from my supervisors. I really hope that someone could give some advice.
Basically there are places in our project where there is a big selection of options. The most concrete example is choosing a city in the world. The items are hundreds of thousands.
Using standard winforms controls and properties, one can search through a list fast.
The problem is that we're using a concatenation of city&district name for all the items. Essentially PREFIX autotomplete works but does not work as needed. The task is to filter and show items by any given string in any part of the item. Essentially a FULL TEXT search in the combobox.
Does anyone have an idea about switching autocomplete sources in runtime relatively qiuckly and handling the suggest/suggestappend event?
Also the project is in VB.NET, though any form of .NET advice will be extremely helpful.
Thanks!
UPDATE: The latest attempt using competent_tech's suggestion with some minor modifications.
Imports System.Data
Public Class Form1
Private _ErrorText As String
Private _CommandExecuted As Boolean
Private m_fOkToUpdateAutoComplete As Boolean
Private m_sLastSearchedFor As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Me.SetStatusText("Loading...")
Me._ErrorText = ""
Me.Cities.Clear()
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Me._CommandExecuted = True
Me.Ara_airportsTableAdapter.Fill(Me.Cities.ara_airports)
Catch ex As Exception
_ErrorText = ex.Message
End Try
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If Me._ErrorText = "" Then
Me.SetStatusText(Me.Cities.ara_airports.Count & " Records loaded")
Else
Me.SetStatusText(Me._ErrorText)
End If
Me.BindingSource.ResetBindings(False)
End Sub
Private Sub SetStatusText(ByVal sText As String)
Me.Text = sText
End Sub
Private Sub cboPort_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboPort.KeyDown
Try
If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down Then
m_fOkToUpdateAutoComplete = False
Else
m_fOkToUpdateAutoComplete = True
End If
Catch theException As Exception
' ...
End Try
End Sub
Private Sub cboPort_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPort.KeyUp
Try
If m_fOkToUpdateAutoComplete Then
With cboPort
If .Text.Length >= 2 Then
Dim cSuggestions As IList
Dim sError As String = ""
m_sLastSearchedFor = .Text
cSuggestions = GetSuggestions(m_sLastSearchedFor)
.DataSource = Nothing
If cSuggestions IsNot Nothing Then
.BindingContext = New BindingContext
.DisplayMember = "CName"
.ValueMember = "id"
.DataSource = New BindingSource(cSuggestions, Nothing)
System.Threading.Thread.Sleep(10)
System.Windows.Forms.Application.DoEvents()
.DroppedDown = True
.Text = m_sLastSearchedFor
If .Text.Length > 0 Then .SelectionStart = .Text.Length
End If
End If
End With
End If
Catch theException As Exception
' ...
End Try
End Sub
Private Function GetSuggestions(ByVal searchFor As String) As IList
BindingSource.Filter = "CName LIKE '%" & searchFor & "%'"
Return BindingSource.List
End Function
End Class
The way we address this with very large sets of data (full set of drug information) is:
1) Handle the combo's TextChanged event
2) Within this event, get the list of suggestions that match the user's current input from the database. We leverage the power of database searching to find matches anywhere within the string.
3) When the suggestions are retrieved, bind them to the combobox
4) Wait for a little bit (500ms) to let the UI catch up (we use a combination of System.Threading.Thread.Sleep and System.Windows.Format.Application.DoEvents()).
A couple of notes on this approach:
1) Nothing is bound to the list when the form is first opened
2) We wait until the user has entered at least 4 characters before we start searching to reduce the hit on the DB and improve the user experience (you don't want to show all of the matches for A, for example).
Update with code to show full solution:
Here are some additional notes and code to show the actual process.
The ComboBox should be configured with all of the properties set to their default values with the exception of:
AutoCompleteMode = SuggestAppend
PreferredDropDownSize = 0, 0
Here is the code that we use for our specific situation (searching first four chars) with a placeholder for retrieving and assigning the data:
Private m_fOkToUpdateAutoComplete As Boolean
Private m_sLastSearchedFor As String = ""
Private Sub cboName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboName.KeyDown
Try
' Catch up and down arrows, and don't change text box if these keys are pressed.
If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down Then
m_fOkToUpdateAutoComplete = False
Else
m_fOkToUpdateAutoComplete = True
End If
Catch theException As Exception
' Do something with the error
End Try
End Sub
Private Sub cboName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboName.TextChanged
Try
If m_fOkToUpdateAutoComplete Then
With cboName
If .Text.Length >= 4 Then
' Only do a search when the first 4 characters have changed
If Not .Text.Substring(0, 4).Equals(m_sLastSearchedFor, StringComparison.InvariantCultureIgnoreCase) Then
Dim cSuggestions As IEnumerable
Dim sError As String = ""
' Record the last 4 characters we searched for
m_sLastSearchedFor = .Text.Substring(0, 4)
' And search for those
cSuggestions = GetSomeSuggestions(m_sLastSearchedFor) ' Your code here
.DataSource = Nothing
If cSuggestions IsNot Nothing Then
' Because this can use the same data source as the list, ensure that
' the bindingcontexts are different so that the lists are not tied to each other
.BindingContext = New BindingContext
.DataSource = cSuggestions
' Let the UI process the results
System.Threading.Thread.Sleep(10)
System.Windows.Forms.Application.DoEvents()
End If
End If
Else
If Not String.IsNullOrEmpty(m_sLastSearchedFor) Then
' Clear the last searched for text
m_sLastSearchedFor = ""
cboName.DataSource = Nothing
End If
End If
End With
End If
Catch theException As Exception
' Do something with the error
End Try
End Sub