Sender and MessageBox oddities - vb.net

New poster here and a VB hack of sorts.
I have a windows form that contains a picture box and three textboxes.
The three textboxes (Red, Green, Blue) are used to enter a numeric value from 000 to 255 representing a color code.
The PictureBox will display the resulting color from the three TextBox values.
My validation routine ValidDecCode makes sure the values entered are within the allowable limits (000-255).
If a user enters a ‘2’ in the first position all is well. If a user then enters a ‘6’ in the second position it will not pass my validation test.
After entering the KeyDown event I save the sender.selectionstart and the sender.text values. After entering the ‘6’ the selectionstart will be a 1 and text contains data that has been validated, ‘2’.
Once the ‘6’ fails my validation test I display a MessageBox, reload my saved sender data, text and selectionstart. This returns the form back to its state prior to entering the ‘6’. The TextBox will show a ‘2’ and the cursor is placed after the ‘2’.
What I find strange is that if I comment out the MessageBox.Show() line the TextBox will show ‘26’ after failing validation. If I comment out the reload of my sender data and not the MessageBox.Show() line, the TextBox will show ‘26’.
I would like to know why commenting out the MessageBox.Show() line does not return the form to its state prior to entering invalid data and what can I do about it. What is it about calling the MessageBox that makes it work?
Private Sub RedBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles RedBox.KeyDown, GreenBox.KeyDown, BlueBox.KeyDown
Dim InChr As String
Dim InPos As Integer = sender.selectionstart
Dim InText As String = sender.text
If IsNumeric(Chr(e.KeyCode)) Then
InChr = Chr(e.KeyValue)
If Not ValidDecCode(InText + InChr, InPos) Then
MessageBox.Show("Invalid value")
sender.text = InText
sender.selectionstart = InPos
End If
Else
MessageBox.Show("Invalid value")
sender.Text = InText
sender.selectionstart = InPos
Exit Sub
End If
End Sub

Related

How to pass a value to DGV Cell when its formatted to C2 format

I'm struggling find a specific answer to this question, therefore asking it myself...
I have a DataGridView with columns which has the following formatting applied to it:
DGV.Columns(3).DefaultCellStyle.Format = "C2"
On the form there is a text box in which the user enters a number and that value is then entered in one of the cells in DGV:
For Each dr As DataGridViewRow In DGV.Rows
dr.Cells("ColumnHeader").Value = TextBox.Text
Next
Before the above code is executed, this event occurs on the TextBox to format its value:
Private Sub TextBox_Leave(sender As Object, e As EventArgs) Handles TextBox.Leave
TextBox.Text = FormatCurrency(TextBox.Text)
End Sub
The text within the TextBox is displaying correctly as a currency, but when the code to put that into a cell in the DGV executes it fails saying the value is in incorrect format.
Is DGV.Columns(3).DefaultCellStyle.Format = "C2" different format to FormatCurrency(TextBox.Text)?
That is all wrong. "C2" is a numeric format string, so it will only work on numbers. A String containing digit characters is not a number and a String containing currency text definitely isn't. You need to get the String from the TextBox, concert that to a number (probably Decimal for currency values) and then load that number into the grid. The grid converts that number, along with all the data, into text to display and will use your format string to do so:
dr.Cells("ColumnHeader").Value = CDec(TextBox.Text)
You would, presumably, already have validated the user input by this stage, so there's no possibility of CDec throwing an exception.
If the intent is to display the data formatted as currency in both the grid and the TextBox then you should get rid of your Leave event handler and handle the Validating and Validated events instead. The first will validate the input to ensure that it is numeric and the second will do the formatting:
Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs) Handles TextBox1.Validating
If Not Decimal.TryParse(TextBox1.Text, NumberStyles.Currency, Nothing, Nothing) Then
'Don't let the control lose focus when it contains invalid data.
e.Cancel = True
End If
End Sub
Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
TextBox1.Text = Decimal.Parse(TextBox1.Text, NumberStyles.Currency).ToString("C2")
End Sub
That code will allow the user to enter the currency symbol or not, but it will ensure that the format is currency with two decimal places once it loses focus. You would then need to allow for the formatting when copying to the grid:
dr.Cells("ColumnHeader").Value = Decimal.Parse(TextBox1.Text, NumberStyles.Currency)

How do I get the textbox to enable after a certain amount of text is inputted?

So my next question(i know i know ive had a lot of questions already but im learning and my teachers suck)
but I am trying to get the textbox to go to readonly after a certain amount of text has been entered. I know how to make it a read only textbox but only after Ive had one set of data entered. i need it to be readonly after 7 days of data has been entered
I've tried inputtextbox.enabled = false
'Validating if user input is a number or not
Dim output As Integer
If Not Integer.TryParse(InputTextbox.Text, output) Then
MessageBox.Show("ERROR! Data must be a number")
InputTextbox.Text = String.Empty
Else
UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
InputTextbox.Text = String.Empty
End If
InputTextbox.Enabled = False
I'm expecting it to disable after the user has entered 7 days worth of data but it only disables after one day of data is entered
Since the entries to UnitsTextbox are all done in code, this TextBox can be set to read only at design time.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim output As Integer
If Not Integer.TryParse(InputTextbox.Text, output) Then
MessageBox.Show("ERROR! Data must be a number")
Else
UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
End If
'Moved this line outside of the If because it happens either way
InputTextbox.Text = String.Empty
If UnitsTextbox.Lines.Length >= 7 Then
Button2.Enabled = False
End If
End Sub
Here's some simple psuedocode
Private Sub InvalidateTextbox(sender As TextBox, e As KeyEventArgs) Handles TextBox1.KeyUp, TextBox2.KeyUp
'FOR ANY TEXTBOX YOU WANT TO CONTROL WITH THIS SUB, ADD AN ADDITIONAL HANDLE.
If Strings.Len(sender.Text) > 7 Then
'^SIMPLE CONDITIONAL, CHECKING IF THE LENGTH IS MORE THAN SEVEN CHARACTERS, MODIFY THIS TO SUIT YOUR NEEDS.
sender.Enabled = False
'^IF THE CONDITIONAL IS TRUE, DEACTIVATE THE CONTROL, IF THAT IS WHAT YOU ARE LOOKING FOR.
sender.ReadOnly = true
'^IF YOU WANT READONLY,NOT ENABLED/DISABLED.
End If
End Sub
This code will execute every time a key is pressed while the text boxes are active. What is after "Handles" defines what events will trigger the sub.
sender becomes the textbox object that triggered the sub. e holds all the event arguments for the keyboard, so you can evaluate things like which key was pressed and other neat things.
There was some confusion on if you wanted enabled/disabled or readonly, both options included.

How do I validate user input?

So I'm having a problem with my code. I need to validate if the user input is a number or not in a textbox. Now I can get it to see whether or not it is a number and it displays the error message just fine but the problem is that the word still gets inputted in to the textbox when I want there to be only numbers
If tried using if not IsNumeric(Number) then
msgbox.show("ERROR! Data must be a number!")
'Getting user input
Dim Number As String = Me.InputTextbox.Text
UnitsTextbox.AppendText(Environment.NewLine & Number)
'Make the textbox delete the text once the button is clicked
InputTextbox.Text = String.Empty
If Not IsNumeric(Number) Then
MsgBox("ERROR! Data must be a number")
End If
I'm expecting it to accept numbers only
i have a text box for input and a textbox for the results and when the number comes up false I want it to not show in the results textbox
As per #Jens comments, only I changed it to .TryParse. IsNumeric is an old VB6 method that has been optimized in .Net to TryParse.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim output As Integer
If Not Integer.TryParse(InputTextbox.Text, output) Then
MessageBox.Show("ERROR! Data must be a number")
Else
UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
InputTextbox.Text = String.Empty
End If
End Sub

VB.NET 2010 Post-KeyPress Event

..This is a reworking of a previously deleted question, I'm rewording to try to be more clear..
VB.NET 2010 Winform with a Datagridview.
User is able to type into an editable cell.
As they type, the "current" value of the cell should be used to search through an array of radiobuttons and select the button that matches the typed value.
Issue is that the user could type a value, but then type a DEL or BACKSPACE to alter the value. And the DEL/BACKSPACE of course doesn't have to be done at end of value, could be anywhere. Also a cells value isn't "finalized" until after this event is complete.
My original code looked like this...
Private Sub txtdgvDiaryEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'Test for numeric value or backspace
If IsNumeric(e.KeyChar.ToString()) _
Or e.KeyChar = ChrW(Keys.Back) Then
e.Handled = False 'if numeric
'******************************************************
If IsNumeric(e.KeyChar.ToString()) Then
If DirectCast(sender, TextBox).Text.Length = 0 Then
Call SelectRadioButtonForValue(e.KeyChar.ToString)
Else
Dim strvar As String
strvar = DirectCast(sender, TextBox).Text.ToString + e.KeyChar.ToString
Call SelectRadioButtonForValue(strvar.ToString)
End If
Else ...
But this doesn't pick up the value of the cell AFTER the DEL/BACKSPACE.
AND I can't use any kind of CELL VALUE CHANGES Because those don't fire till i'm actually LEAVING the cell.
I need to be able to pick up the actual cell value AFTER the Keypress had been processed but remain in EDIT MODE.
Thoughts?

Labels & Text wont update accordingly

I developed an application which is like a digital comprehension sheet activity.
The order of which this works is:
A user double clicks an underscored text area on a text box
A form opens which asks them to place an answer
The answer is validated, if true it goes back to the original form
The issue here is that for some odd reason my labels don't update on step 3, as- well as the text. I have a label named lbAnswerStatus which notifies the user if they have a correct answer, the text does not update, and the 'SelectedText' on Form 1 should be replaced with the answer (if correct)
Here is my code:
Form 1 (when textbox is clicked):
Form2.Answer(answers(counter))
answers(counter) represents the correct answer being passed on, to compare with users answer in form 2
Form 2:
If tbAnswer.Text = theanswer Then
Form1.answerStatus(True, theanswer)
Form 1:
Public Sub answerStatus(status, answer)
If status = true Then
Form2.Close()
rtb.SelectedText = answer
lbAnswerStatus.ForeColor = Color.Green
End If
End Sub
My first assumption was that the Rich text box's SelectedText wasn't changing because it didn't have focus on it however, the lbAnswerStatus color didn't change either, so I figured that there was issues with making modifications to the form.
I used a message box to test whether lbAnswerStatus.Text would pop up and it did, so it can read but not write.
I also attempted to change the text of the label and selectedtext of the textbox in step 1 and it worked fine.
Any ideas what may be causing this issue?
Thanks.
I guess that you want your Form2 (AnswerForm) presented as a modal dialog. Doing that, you can return a result. You didn't say what you want to happen to the answer status label or the answer form if the supplied answer is incorrect.
Just as an example of how it can be done, create a new Windows Forms project. On Form1, place a button (Button1) and a label ("lblAnswerStatus"). Add a new form named "AnswerForm" to the project, and add a TextBox ("TextBox1") and a button ("bnDone").
As code for AnswerForm:
Public Class AnswerForm
Private statusLabel As Label
Private answerText As String
Private Sub bnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnDone.Click
If TextBox1.Text.Equals(answerText, StringComparison.CurrentCultureIgnoreCase) Then
' Alternative 1: set the color here if you want to
statusLabel.ForeColor = Color.Green
' return a value indicating success
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
Else
' indicate error
statusLabel.ForeColor = Color.Red
End If
End Sub
Public Sub New(ByVal statusLabel As Label, ByVal answerText As String)
InitializeComponent()
Me.statusLabel = statusLabel
Me.answerText = answerText
End Sub
End Class
and as code for Form1:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lblAnswerStatus.ForeColor = Color.Blue
Using answerFrm As New AnswerForm(lblAnswerStatus, "X")
Dim dlgResult = answerFrm.ShowDialog()
' Alternative 2: use this if you want to change the color in this handler
If dlgResult = DialogResult.Yes Then
lblAnswerStatus.ForeColor = Color.Purple
End If
End Using
End Sub
End Class
If you click Button1 on Form1, a dialog box appears. Type into its TextBox1 and click bnDone. Because the instance of AnswerForm has a reference to lblAnswerStatus (supplied in its constructor), it is easy to update the latter, e.g. it turns red if you enter the wrong answer.