stackexchange12
stackexchange12

Reputation: 652

Prevent symbols using Char.IsSymbol() Method excluding mathematical operators

I'm hoping to find a way to prevent symbols in my vb.net windows forms application using a datagridview with the Char.IsSymbol() method, however I'm hoping to still allow the user to input mathmatical operators into the datagridview. Is there a way to prevent all symbols except mathmatical operators? Here is what I have so far, which prevents all symbols, math operators, subscripts and superscripts. However I'm hoping there is a simple way of preventing symbols while allowing math operators. Thanks for any help or suggestions you may offer.

If(columnindex = 0) Then ' checking value for column 1 only
Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value

If Char.IsSymbol(cellString)
MessageBox.Show("Special Characters Not Allowed")
End If
End If

Upvotes: 0

Views: 337

Answers (1)

Andrew Morton
Andrew Morton

Reputation: 25057

If cellString is a single character, then you can make a string of the allowed symbols and check if cellString is in that string before checking if it is another symbol:

Dim cellString = "+"c ' for illustration
Dim allowedSymbols = "+-*/^"

If (allowedSymbols.IndexOf(cellString) < 0) AndAlso (Char.IsSymbol(cellString)) Then
    MessageBox.Show("Special Characters Not Allowed")
End If

If cellString may be more than one character then you just need to iterate over it and check each character.

Upvotes: 1

Related Questions