No output for If Like statement? - vb.net

So im trying to get this code to work but its not returning any values for the If Like portion
of the code. I have no idea why this is happening.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'display color button click event procedure should display the color of the item
'whos item number is entered by the user.
'all item numbers contain exactly seven characters
'all items are available in four colors: blue, green, red, and white.
'the fourth character in the item number indicates the items color
'as follows: B or b indicates blue etc
'if the item number does not contain 7 charactors OR
'if the forth character is not one of the valid color characters,
'the procedure should display the appropriate message
If txtItem.Text Like "###[bBgGrRrwW]###" Then
If txtItem.Text.Contains("bB") Then
lblColor.Text = "Blue"
ElseIf txtItem.Text.Contains("gG") Then
lblColor.Text = "Green"
ElseIf txtItem.Text.Contains("rR") Then
lblColor.Text = "Red"
ElseIf txtItem.Text.Contains("Ww") Then
lblColor.Text = "White"
End If
ElseIf txtItem.Text IsNot "###[bBgGrRwW]###" Then
MessageBox.Show("Bad Job", "Color Project", MessageBoxButtons.OKCancel,
MessageBoxIcon.Information)
End If
End Sub

Where you have .Contains("bB"), you are asking it to check if the string contains bB, i.e. two characters. What you actually want to do is check if it contains B and ignore the case of the character. Although Contains does not have a case-insensitive option, IndexOf does: String.IndexOf Method (String, StringComparison), so you could use
If txtItem.Text.IndexOf("B", StringComparison.OrdinalIgnoreCase) >= 0 Then

Related

Select a specific row from ComboBox and add amount in other Column from same row in DataGridView VB.NET

I'm using VB.NET for a small project. I have a DataGridView with some rows and columns.
I want to send amounts inside a specific row & column. For this purpose I'm using a ComboBox to select the receiver, but I cant manage to select the specific row & column for the amount.
Here is a photo of the form:
[
I managed to add the receivers inside the ComboBox and to add an amount, but in all rows.
This is code (it auto generates the rows for now).
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rowId As Integer = Me.DataGridView1.Rows.Add()
Dim row As DataGridViewRow = Me.DataGridView1.Rows(rowId)
row.Cells("Column1").Value = "UNITED BANK"
row.Cells("Column2").Value = "1000"
row.Cells("Column3").Value = "ExampleInfo"
Dim rowId2 As Integer = Me.DataGridView1.Rows.Add()
Dim row2 As DataGridViewRow = Me.DataGridView1.Rows(rowId2)
row2.Cells("Column1").Value = "FREE BANK"
row2.Cells("Column2").Value = "2000"
row2.Cells("Column3").Value = "ExampleInfo"
Dim bolAdd As Boolean = False
Dim strValue As String
For Each myRow As DataGridViewRow In Me.DataGridView1.Rows
bolAdd = True
strValue = myRow.Cells("Column1").Value
For Each myItem As String In Me.ComboBox1.Items
If myItem = strValue Then bolAdd = False
Next
If bolAdd AndAlso Not (strValue Is Nothing) Then Me.ComboBox1.Items.Add(strValue)
Next
If Me.ComboBox1.SelectedIndex = -1 Then
Me.ComboBox1.SelectedIndex = 0
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each rowX As DataGridViewRow In Me.DataGridView1.Rows
rowX.Cells(1).Value = Val(rowX.Cells(1).Value) + Val(TextBox1.Text)
Next
End Sub
End Class
With this code, I'm able to add an amount, but to all rows.
I want to add an amount only to the row I selected in the ComboBox.
Your current code in the Button1_Click event is adding the amount from the text box to ALL the rows in the grid regardless of “what” value is selected in the combo box.
From what you describe, you want the amount from the text box to be added to rows that “match” the value in the combo box. The current code is never checking this, so the value from the text box is added to all the rows in the grid.
Therefore, the code needs to check to see if the value in Column1 "Receiver" “matches” the value in the combo box. If it DOES match, then add the value from the text box to the current value in Column2 "USD."
It is unclear if there may be more rows in the grid that have the same Column1 "Receiver" value and if you want to update those values also. I am assuming that there will only be ONE row in the grid that “matches” the value in the combo box. Therefore, once we find this value and add the amount, then we are done and the code will return without looping through the rest of the rows in the grid.
So is what you need to do is alter the code in the button click event to do this checking as described above.
There are a lot of checks we need to make. We need to check that the combo box has a value to compare to. Also, before we check ANY cell value, we need to make sure the cells value is not null BEFORE we try and call the cell's Value ToString method.
In addition, the cell MAY have a value, however, it may not be a number, so we have to check and make sure the value in the cell is an actual number and the same would apply to the text box.
So, walking through the code below would go something like…
If (Int32.TryParse(TextBox1.Text, addedValue)) … checks to see if
the text box amount is a valid number.
If (Not String.IsNullOrEmpty(targetComboValue)) … checks to make
sure the combo box value is not empty.
Then we start the loop through all the rows in the grid.
If (Not rowX.IsNewRow) … checks to see if the row is the “new” row
which we will ignore this “new” row.
If (rowX.Cells("Column1").Value IsNot Nothing) … checks to make
sure the “Receiver” cell is not null
If (rowX.Cells("Column1").Value.ToString() = targetComboValue) …
checks to see if the “Receiver” cells value matches the value in the
combo box.
If (rowX.Cells("Column2").Value IsNot Nothing) … check to make sure
the “USD” cell is not null.
And Finally,…
If (Int32.TryParse(rowX.Cells("Column2").Value.ToString(), currentValue)) … check to see if the value in the “USD” cell is a valid number.
If all these checks succeed then add the value from the text box to the current value in the “USD” cell.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim targetComboValue = ComboBox1.SelectedItem
Dim addedValue As Int32
Dim currentValue As Int32
If (Int32.TryParse(TextBox1.Text, addedValue)) Then
If (Not String.IsNullOrEmpty(targetComboValue)) Then
For Each rowX As DataGridViewRow In Me.DataGridView1.Rows
If (Not rowX.IsNewRow) Then
If (rowX.Cells("Column1").Value IsNot Nothing) Then
If (rowX.Cells("Column1").Value.ToString() = targetComboValue) Then
If (rowX.Cells("Column2").Value IsNot Nothing) Then
If (Int32.TryParse(rowX.Cells("Column2").Value.ToString(), currentValue)) Then
rowX.Cells("Column2").Value = currentValue + addedValue
Return
End If
End If
End If
End If
End If
Next
End If
End If
End Sub
I hope this makes sense.
You can edit the desired row just by using a syntax as follow:
Me.DataGridView1.Rows(1).Cells(2).Value = "your string"
So if you want to select corresponding to your combo index :
Dim myId As Integer = Me.ComboBox1.SelectedIndex
Me.DataGridView1.Rows(myId).Cells(2).Value = "your string"
But this assuming that the count of combo and DataGridView are same.
If not you should think about to add an id Column in your DataGridView ...
If you mean something else please explain, I will edit.

Storing string then displaying it with a label box

I am trying to store the string from an input box and then display it with dashes in the lblbox for a hangman game for a class.
These are the tasks that I am struggling with:
edit the program to allow a Secret Word of any length.
the program will allow the ‘guesser’ to guess 2 times the length of the word. As an example, the word ‘code’ will allow 8 total guesses.
As the user guesses at letters contained in the word the program will:
Count the number of attempts the user has completed.
Replace the appropriate dash (-) with the correct letter, if the correct letter has been guessed.
When all the letters have been guessed correctly all the dashes (-) should be replaced with the appropriate letters, and a message box should appear stating “Great Job playing Hangman.!”
If the user is unable to guess the correct word in the amount of guesses allowed; the dashes (-) should be replaced with GAME OVER! and a message box should appear stating “Sorry the correct word was________”
2 bonus points will be awarded for displaying all incorrect letters guess in a 3rd label control.
4 more additional bonus points will be awarded for not allowing, or counting a user who guesses the same incorrect letter twice.
Here is my code:
Dim strSecretWord As String
Dim strLetterGuessed As String
Dim blnDashReplaced As Boolean
Dim intNumberOfRemainingGuesses As Integer = 10
Dim intNumofGuesses As Integer = 0
lblSecretWord.Text = ""
lblNumberOfAttempts.Text = ""
'start game and have 1st user input a 5 letter word that 2nd player needs to guess
strSecretWord = InputBox("Please input a 5 letter word for user to guess:", "Please input secret word.").ToUpper
'displays five dashes for the secret word
lblSecretWord.Text = lblSecretWord.Text & "-----"
'guessing player recieves inputbox to make letter guesses
MessageBox.Show("The length of the word is 5 letters, you will be given 10 guesses", "10 guesses", MessageBoxButtons.OK)
MessageBox.Show("Player who gets to guess, BE READY!", "Good Luck Guessing", MessageBoxButtons.OK)
'Counts number of attempts player gets (10) and replaces dashes with guessed letter if correct
'If guessed letter was incorrect, user loses a turn
For intNumberofGuesses = 1 To 10
strLetterGuessed = InputBox("Please guess a letter:", "Letter Guess").ToUpper
'Uses an IntIndex counter of 0 to 4 to execute 5 times (5 dashes)
'Also uses the value of intIndex to check each of the 5 locations of the strSecretWord
For intIndex As Integer = 0 To 4
'if the user has guessed a correct letter then remove a dash and insert the correct letter guessed
If strSecretWord.Substring(intIndex, 1) = strLetterGuessed Then
lblSecretWord.Text = lblSecretWord.Text.Remove(intIndex, 1)
lblSecretWord.Text = lblSecretWord.Text.Insert(intIndex, strLetterGuessed)
blnDashReplaced = True
End If
Next intIndex
'If the user guessed a correct letter on their last guess the blnDashReplaced is set and the true condition of the If statement is executed
If blnDashReplaced = True Then
'if there are no more dashes, and the game has been solved.
If lblSecretWord.Text.Contains("-") = False Then
MessageBox.Show("Great Job playign Hangman!", "Game Over", MessageBoxButtons.OK)
lblRemainingNumberOfAttempts.Text = ""
lblNumberOfAttempts.Text = ""
Exit Sub
Else
blnDashReplaced = False
End If
Else
End If
lblNumberOfAttempts.Text = intNumberofGuesses
intNumberOfRemainingGuesses = intNumberOfRemainingGuesses - 1
lblRemainingNumberOfAttempts.Text = intNumberOfRemainingGuesses
Next
lblSecretWord.Text = "GAME OVER!"
MessageBox.Show("Better luck next time. Sorry the correct word was " & strSecretWord & ".", "You Lost", MessageBoxButtons.OK)
lblRemainingNumberOfAttempts.Text = ""
lblNumberOfAttempts.Text = ""
I added a list box to keep the guessed letters. Other comments and explanations in line.
Public Class Form3
'Move this to a class level variable so it can be seen by
'all the methods in the class
Private strSecretWord As String
Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
Dim strLetterGuessed As String
Dim blnDashReplaced As Boolean
Dim intNumberOfRemainingGuesses As Integer
Dim intNumofGuesses As Integer = 0
'Display correct number of dashes
Dim numberOfDashes As Integer = strSecretWord.Length
'Create a string with correct number of dashes
'This uses and overload of the String constructor that takes a Char and an integer
'as arguments and returns a string with that character repeated that number
'of times. The lower case c following "-" indicates that - is a Char.
Dim TotalNumofGuesses = numberOfDashes * 2
lblRemainingNumberOfAttempts.Text = TotalNumofGuesses.ToString
intNumberOfRemainingGuesses = TotalNumofGuesses
Dim dashString As String = New String("-"c, numberOfDashes)
'displays the dashes
lblSecretWord.Text = dashString
'guessing player recieves inputbox to make letter guesses
'You can use an Interpolated string to display variables in line surrounded by { }.
'In older versions of VB String.Format() will yield the same result.
MessageBox.Show($"The length of the word is {numberOfDashes} letters, you will be given {TotalNumofGuesses} guesses", $"{TotalNumofGuesses} guesses", MessageBoxButtons.OK)
MessageBox.Show("Player who gets to guess, BE READY!", "Good Luck Guessing", MessageBoxButtons.OK)
'Counts number of attempts player gets and replaces dashes with guessed letter if correct
'If guessed letter was incorrect, user loses a turn
For counter = 1 To TotalNumofGuesses
strLetterGuessed = InputBox("Please guess a letter:", "Letter Guess").ToUpper
'If lstLettersGuessed.Contains(strLetterGuessed) Then
If lbxLettersGuessed.Items.Contains(strLetterGuessed) Then
MessageBox.Show($"{strLetterGuessed} has already been guessed.", "Try Again")
'need to do this so they are not cheated out of a guess
TotalNumofGuesses += 1
Continue For 'Moves to the next iteration of the For
End If
lbxLettersGuessed.Items.Add(strLetterGuessed)
'lstLettersGuessed.Add(strLetterGuessed)
'Uses an IntIndex counter of 0 to 4 to execute 5 times (5 dashes)
'Also uses the value of intIndex to check each of the 5 locations of the strSecretWord
For intIndex As Integer = 0 To numberOfDashes - 1
'if the user has guessed a correct letter then remove a dash and insert the correct letter guessed
If strSecretWord.Substring(intIndex, 1) = strLetterGuessed Then
lblSecretWord.Text = lblSecretWord.Text.Remove(intIndex, 1)
lblSecretWord.Text = lblSecretWord.Text.Insert(intIndex, strLetterGuessed)
blnDashReplaced = True
End If
Next intIndex
'If the user guessed a correct letter on their last guess the blnDashReplaced is set and the true condition of the If statement is executed
If blnDashReplaced = True Then
'if there are no more dashes, and the game has been solved.
If lblSecretWord.Text.Contains("-") = False Then
MessageBox.Show("Great Job playing Hangman!", "Game Over", MessageBoxButtons.OK)
'Do this at start of game, player wants to see final score
'lblRemainingNumberOfAttempts.Text = ""
'lblNumberOfAttempts.Text = ""
Exit Sub
Else
blnDashReplaced = False
End If
End If
'This is a shorter way of incrementing a variable
intNumofGuesses += 1
'Can't put an integer into a Text property, it needs a string
lblNumberOfAttempts.Text = intNumofGuesses.ToString
'This is a shorter way of decrementing a variable
intNumberOfRemainingGuesses -= 1
'Can't put an integer into a Text property, it needs a string
lblRemainingNumberOfAttempts.Text = intNumberOfRemainingGuesses.ToString
Next
lblSecretWord.Text = "GAME OVER!"
MessageBox.Show("Better luck next time. Sorry the correct word was " & strSecretWord & ".", "You Lost", MessageBoxButtons.OK)
'Do this at start of game
'lblRemainingNumberOfAttempts.Text = ""
'lblNumberOfAttempts.Text = ""
End Sub
Private Sub btnSetUp_Click(sender As Object, e As EventArgs) Handles btnSetUp.Click
lblSecretWord.Text = ""
lblNumberOfAttempts.Text = "0"
lblRemainingNumberOfAttempts.Text = "0"
lbxLettersGuessed.Items.Clear()
'start game and have 1st user input a 5 letter word that 2nd player needs to guess
strSecretWord = InputBox("Please input a word for user to guess:", "Please input secret word.").ToUpper
End Sub
End Class

How to search for a word that contains uppercase and lowercase letters in a TextBox and then replace it with another word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a TextBox that includes more words
Example:
I'm pRoFeSSoinaL coder
I want to search for the word pRoFeSSoinaL (that contains uppercase and lowercase letters) and then replace it.
OK let me try and address your problem again based on your image:
And on your comment:
...but i need search about word have More than a change of letters example (execute, ExEcute, eXecute, execUtE........ETC) i need find word No matter how large and small the letters are see this proof
Again, if I have this right, you would like to find all occurrences of a word regardless of it's case. In effect you would like for your application to work a bit like Notepad.
I've put together some code that will use the following methods:
String.ToLower:
Returns a copy of this string converted to lowercase.
TextBox.Select:
Selects a range of text in the text box.
String.Remove:
Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.
String.Insert:
Returns a new string in which a specified string is inserted at a specified index position in this instance.
String.Substring:
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
I have three Button controls that I've put a bit a code behind. One will find the next occurrence of a word, another will replace the next occurrence of a word and the last one will replace all occurrences of a word.
Now it's not my job to design your application but for ease I have also popped on three TextBox controls. This is how my form looks:
To find the next occurrence of professional this is the code I used:
Private Sub btnFindNext_Click(sender As Object, e As EventArgs) Handles btnFindNext.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
'select next occurrence
txtTextToSearch.Select(position, txtWordToFind.Text.Length)
End If
End Sub
This is a screenshot of how it would look when you click the button:
Clicking the button again will select the next occurrence of the word. It will keep toggling between the two in my example.
To replace the next occurrence of professional this is the code I used:
Private Sub btnReplaceNext_Click(sender As Object, e As EventArgs) Handles btnReplaceNext.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
'remove old word
txtTextToSearch.Text = txtTextToSearch.Text.Remove(position, txtWordToFind.Text.Length())
'insert new word
txtTextToSearch.Text = txtTextToSearch.Text.Insert(position, txtReplaceWith.Text)
End If
End Sub
This is a screenshot of how it would look when you click the button:
Clicking the button again will replace the next occurrence of the word.
To replace all occurrences of the word professional this is the code I used:
Private Sub btnReplaceAll_Click(sender As Object, e As EventArgs) Handles btnReplaceAll.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
While position > 0 AndAlso position < txtTextToSearch.Text.Length
'remove old word
txtTextToSearch.Text = txtTextToSearch.Text.Remove(position, txtWordToFind.Text.Length())
'insert new word
txtTextToSearch.Text = txtTextToSearch.Text.Insert(position, txtReplaceWith.Text)
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
End While
End If
End Sub
This is a screenshot of how it would look when you click the button:
My code will need adapting I'm sure but it should give you something to go on. As for the level of detail I have gone to, may not benefit you but may benefit others that visit this question. Hope this helps.
Judging from your screenshot (http://i.imgur.com/77wdZoI.png) you appear to be after case-insensitive string matching.
The simplest way is to use the String.IndexOf(String, StringComparison) overload to perform case-insensitive comparison:
Private Function FindText(ByVal Source As String, ByVal Text As String) As Integer
Return Source.IndexOf(Text, StringComparison.OrdinalIgnoreCase)
End Function
Usage example:
Dim TextToFind As String = "professional"
Dim TextPos As Integer = FindText(TextBox1.Text, TextToFind)
If TextPos > -1 Then 'Match found.
TextBox1.SelectionStart = TextPos
TextBox1.SelectionLength = TextToFind.Length
End If

Invalid Cast Expectation Was Unhanded (when checking the contents of a text box)

This code is from a subroutine that checks if a text box entry fits the criteria specified (an integer between 1 and 100).
The first IF statement should check if it is not a numerical entry. If it is not numerical then the contents of the text box should be set blank so that a number can be entered.
The second IF statement should check if the number is larger than 100. If it is then the contents of the text box should be set blank so that an appropriate number can be entered.
The Third IF statement should check if the number is smaller than 1. If it is then the contents of the text box should be set blank so that an appropriate number can be entered.
Finally the contents of the box should be set as the variable.
I initially programmed the first IF statement on its own and it worked. But upon adding the others my program would crash when I typed anything into the text box and the error was as stated in my title. I have looked at multiple solutions and have found nothing for almost 2 days that fixed the problem.
Any suggestions would be appreciated.
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles NumQTextBoxInput.TextChanged
'Check if input is numeric
If Not IsNumeric(NumQTextBoxInput.Text) Then NumQTextBoxInput.Text = ""
If (NumQTextBoxInput.Text > 100) Then
NumQTextBoxInput.Text = ""
End If
If (NumQTextBoxInput.Text < 1) Then
NumQTextBoxInput.Text = ""
End If
ArchwayComputingExamCreator.GlobalVariables.NumOfQuestions = NumQTextBoxInput.Text
'Setting the variable to the contense
End Sub
You should always use the appropriate parse function when accepting text for numbers.
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles NumQTextBoxInput.TextChanged
Dim Value as integer
If Not Integer.TryParse(NumQTextBoxInput.text, Value) OrElse Value < 1 OrElse Value > 100 Then NumQTextBoxInput.Text = ""
... no idea if the archway bit is really what you wanted so left that out ....
End Sub
In this operation:
If Not IsNumeric(NumQTextBoxInput.Text) Then NumQTextBoxInput.Text = ""
Any time the input is not numeric, you set it to a value which is still not numeric. So any numeric comparison will fail:
If (NumQTextBoxInput.Text > 100)
Maybe you meant to set the value to some numeric default?:
If Not IsNumeric(NumQTextBoxInput.Text) Then NumQTextBoxInput.Text = "0"
Or just exit the method entirely when it's not numeric?:
If Not IsNumeric(NumQTextBoxInput.Text) Then
NumQTextBoxInput.Text = ""
Return
End If
Or perhaps something else? However you modify your logic, the point is that you can't perform numeric comparisons on non-numeric strings.

How to insert letter "N" and have followed by 4 digits. The 4 digits is entered by the user in to the text box

NOTE: I tried using concat() method, join method and even tried to
insert just text(which turned out editable) but none of them worked as
I wanted. I was thinking if substring method will work but I only have
basic idea of how substring works. I want the letter "N" to be
inserted into the text box when form loads and when the user enters
4digits(OrderNo2.text) which should be joined so that it can be saved
together when I clicked on save button.
Please help guys. Thanks
Private Sub btnAddOrder_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAddOrder.Click
isNewRow = True
Dim newRow As DataRow = dsOrders.Tables("Orders").NewRow
Try
If txtOrderNo2.Text.Length = 5 Then
newRow.Item("OrderNo") = txtOrderNo2.Text
If cbo_Product.SelectedIndex <> -1 Then
newRow.Item("Product") = cbo_Product.Text
newRow.Item("Price") = txtPrice2.Text
If txtQuantity.Text <> "" Then
newRow.Item("Quantity") = txtQuantity.Text
newRow.Item("CustomerNo") = txtCustomerNo2.Text
dsOrders.Tables("Orders").Rows.Add(newRow)
'determine row index of new row
rowIndex = dsOrders.Tables("Orders").Rows.Count - 1
'save changes back to the database
daOrders.Update(dsOrders, "Orders")
Else
MessageBox.Show("Please enter the quantity", "Missing Quantity", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Please choose the product", "Missing Price", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Order Number must be 5 digits long!", "Missing Order Number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Used a MaskedTextBox instead of a regular TextBox. You can then set the Mask property to "N0000", which will only allow the user to enter 4 digits following the 'N', and will not let the user edit the 'N'. The Text property will give you the displayed text, including the 'N'.
Try just allowing only digits in the textbox with a max lenght of 4.
Then if the user clicks on the submit button just get the value of the input and place the N infront of it.
You will always have a length of 4 + your character("N") is 5.
That way you should be able to fix it.
You can also try using a regex. to see if it contains the N and numbers from 0-9.
only if it starts with the character N followed by 4 numberic characters it will be a valid input.
This would be your regex then.
^(N?[0-9]{4})$
Hope this can be of any use to you