Mouse Click / Tab

like I said, to stop the user from missing data. The data needs to be entered in a certain order, because I have events that are fired on the textbox leave events (checks the value against a range) - therefore I don't want the user to be able to "click out" of a textbox, I want it so they can only tab through the textboxes.
 
Sorry, yeah I'm using the event _Validated

However, if the value is validated, it doesn't stop them then clicking in for example the 10th textbox, without filling in the previous 8.

I NEED to keep the correct order. The easiest way of this is the tab order. So I need to stop mouse clicks into the textboxes (but allow the mouse on the toolbar options)

No offense, but I'm not going to go into the reasons as to why I need to do this, it's a certain spec I've been given towards the project, I understand it but it's too hard to explain to people who aren't aware of what the system does.

All I need to know is can I stop the user clicking in the textboxes, yes or no :D If yes then how :)

Thanks
 
How about starting with all segments disabled, and then enable the appropriate segments in sequence as the data is entered ?
 
What about this piece of code? TextBox1 will always be in focus if the validation fails even if the user clicks mouse on another TextBox. (If the user enters nothing then the validation fails)

The following piece of code forces the user to enter something in TextBox1
VB.NET:
Public Class Form1

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If String.IsNullOrEmpty(TextBox1.Text.Trim()) Then
            TextBox1.Focus()
        End If
    End Sub

End Class
 
OK I can see how that works. However I have 20 textboxes to go through in the correct order.

For example purposes, lets start at textbox1. If that's got nothing typed in, then the user can't click another textbox. If I enter a value, they can click any of the other 19 textboxes. At this point I need to make sure textbox2 takes focus and doesn't allow the user to click any of the other boxes, inc. textbox1

This is why I thought just using tab would be so much easier. Was hoping there was a simple way to stop a textbox accepting a mouse click, just like you can set a textbox to only allow numbers to be typed.
 
actually that works if I adjust the code so that;

VB.NET:
 Private Sub txtBagNo_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtBagNo.Validating
        If String.IsNullOrEmpty(txtBagNo.Text.Trim()) Then
            txtBagNo.Focus()
        Else : Me.txtAppearance.Focus()
        End If
    End Sub

- as long as I change the Else : statement to the next textbox I want the user to go to, then it works.

Thanks for the help Ajeesh - much appriciated! :D rep coming your way!
 
Instead of else statement i think you may use Validated event. Also if validation fails you can prevent the execution of validated event by assigning e.Cancel = True.

A quick demonstration.

VB.NET:
Public Class Form1
    Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
        TextBox2.Focus()
    End Sub

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If String.IsNullOrEmpty(TextBox1.Text.Trim()) Then
            e.Cancel = True
            TextBox1.Focus()
        End If
    End Sub

    Private Sub TextBox2_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Validated
        TextBox3.Focus()
    End Sub

    Private Sub TextBox2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
        If String.IsNullOrEmpty(TextBox2.Text.Trim()) Then
            e.Cancel = True
            TextBox2.Focus()
        End If
    End Sub

    Private Sub TextBox3_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.Validated
        TextBox4.Focus()
    End Sub

    Private Sub TextBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox3.Validating
        If String.IsNullOrEmpty(TextBox3.Text.Trim()) Then
            e.Cancel = True
            TextBox3.Focus()
        End If
    End Sub

    Private Sub TextBox4_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.Validated
        Me.Text = "Validation succeded!!" ' Or do something else?
    End Sub

    Private Sub TextBox4_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox4.Validating
        If String.IsNullOrEmpty(TextBox4.Text.Trim()) Then
            e.Cancel = True
            TextBox4.Focus()
        End If
    End Sub
End Class
 
The code can also be shortened like below, the GetNextControl method uses the tab order:
VB.NET:
Private Sub controls_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating, TextBox2.Validating, TextBox3.Validating
    If String.IsNullOrEmpty(DirectCast(sender, Control).Text.Trim()) Then e.Cancel = True
End Sub

Private Sub controls_Validated(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles TextBox1.Validated, TextBox2.Validated, TextBox3.Validated
    Dim c As Control = Me.GetNextControl(sender, True)
    If c IsNot Nothing Then c.Focus()
End Sub
 
Nice tip JohnH, didn't think of GetNextControl() method.

Also Arg81 has got about 20 textboxes and may be using different validation methods so I think may have to use different event handlers.
 
That's nice John, thanks :D


Ajeesh is right, I have 20 textboxes, and for 5 of them, I use the _Validated event to check the typed value falls within a certain "range", and depending on the value, it will do one of 2 things (basically it's my validation tool!!)

Maybe I can move this code into another event, but the only one I really got it working in was _Validated.

VB.NET:
 Private Sub txtMoistNIR_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMoistNIR.Validated
        strMoistMin = cdec(Me.txtMinNIR.Text)
        strMoistMax = cdec(Me.txtMaxNIR.Text)
        Me.MositureRejectActions() [COLOR="SeaGreen"]'create the Action and Reject range values[/COLOR]

        Dim intValue As Decimal = CDec(Me.txtMoistSar.Text)
        If intValue <= decRejectLow Or intValue >= decRejectHigh Then
            'Me.RejectIt()
            MessageBox.Show("this was rejected")
        ElseIf (intValue >= decActionLowMin AndAlso intValue <= decActionLowMax) Or (intValue >= decActionHighMin AndAlso intValue <= decActionHighMax) Then
            'Me.ActionIt()
            MessageBox.Show("this was actioned")
        End If

 End Sub

 Private Sub MositureRejectActions()
        decRejectLow = strMoistMin - 1.01
        decActionLowMin = strMoistMin - 1.0
        decActionLowMax = strMoistMin - 0.01
        decActionHighMin = strMoistMax + 0.01
        decActionHighMax = strMoistMax + 0.5
        decRejectHigh = strMoistMax + 0.51
 End Sub

That won't make a lot of sense to people who read it, but I have a value, then create 2 reject and 2 action values from it, and if the value the user types is within any of the ranges, it will either do nothing (and move onto the next textbox), reject the product, or require action on the product.....
 
Last edited:
Back
Top