Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox Number Validation 1

Status
Not open for further replies.

swk003

IS-IT--Management
Feb 10, 2004
86
GB
Private Sub txtF2Total_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtF2Total.Validating

I wish to validate a textbox (txtF2Total) to accept just 3 possible values: "0", "2" and "9". This was simple in Access however i am struggling. Can anybody help?

Thanks in advance
 
One way not to let give focus to the next control:

Private Sub txtF2Total_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtF2Total.Validating
e.Cancel = Not (txtF2Total.Text = "0" Or txtF2Total.Text = "2" Or txtF2Total.Text = "9")
End Sub
 
One other way, more graphical is:

Private Sub txtF2Total_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtF2Total.Validating
e.Cancel = Not (txtF2Total.Text = "0" Or txtF2Total.Text = "2" Or txtF2Total.Text = "9")

If e.Cancel = True Then
txtF2Total.BackColor = Color.Red
Else
txtF2Total.BackColor = Color.LightGray
End If
End Sub
 
Onew more way is to use the ErrorProvider Component for the toolbox. What to check is: Not (txtF2Total.Text = "0" Or txtF2Total.Text = "2" Or txtF2Total.Text = "9") and then you have to do: ErrorProvider1.setError(txtF2Total, "It is not 0, 2 or 9 !").
Else (in the validating event handler off cource) do something like: ErrorProvider1.setError(txtF2Total, Nothing).


-
There are lots other ways. Not posting again :)
 
Thanks TopGiver for the 3 fine examples. They all worked. I have gone with ErrorProvider example for now, and added focus(), undo() and clear(). Here's the code:

If Not (txtF2Total.Text = "0" Or txtF2Total.Text = "2" Or txtF2Total.Text = "9") Then
ErrorProvider1.SetError(txtF2Total, "It is not 0, 2 or 9 !")
txtF2Total.Focus()
txtF2Total.Undo()
txtF2Total.Clear()
Else : ErrorProvider1.SetError(txtF2Total, "")
End If
 
Else : ErrorProvider1.SetError(txtF2Total, "")

Better not use the "" but the keyword Nothing if there is not error.
 
Hi TopGiver

I have another problem which is related to the above but is down to my lack of clear understanding of boolean logic and whether to use an if....else statement, a case statement or a combination of them both. The conditional statement below fails and I am getting muddled with the boolean construct..can you point me in the right direction?

As discussed before, I wish to validate a textbox (txtF2Total) to accept just 3 possible values: "0", "2" and "9". I then want to validate a number of other textboxes on the same form to meet certain conditions dependent on the value displayed in txtF2Total. e.g.

if txtF2Total = '0' then all other textbxes values MUST be'0','2','3'or'9'
if txtF2Total = '2' then all other textbxes values MUST be '0','2' or '9'
if txtF2Total = '9' then all other textbxes values MUST be '0' or '9'

Here's the context so far:

Private Sub txtCentre_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCentre.Validating, txtInnerInferiorHELevel.Validating, txtInnerNasalHELevel.Validating, txtInnerNasalHELevel.Validating, txtInnerSuperiorHELevel.Validating, txtInnerTemporalHELevel.Validating, txtOuterInferiorHELevel.Validating, txtOuterNasalHELevel.Validating, txtOuterSuperiorHELevel.Validating, txtOuterTemporalHELevel.Validating


Dim myControl As Control
myControl = CType(sender, Control)

If (txtF2Total.Text = "0") Then
If Not(myControl.Text = "0" Or myControl.Text = "2" Or myControl.Text = "3" Or myControl.Text = "9") Then
ErrorProvider1.SetError(myControl, "is not 0, 2, 3 or 9 !")
myControl.Focus()
myControl.Refresh()
myControl.ResetText()

Else
ErrorProvider1.SetError(myControl, Nothing)
End If
If (txtF2Total.Text = "2") Then
If Not (myControl.Text = "0" Or myControl.Text = "2" Or myControl.Text = "9") Then
ErrorProvider1.SetError(myControl, "is not 0, 2 or 9 !")
myControl.Focus()
myControl.Refresh()
myControl.ResetText()
Else
ErrorProvider1.SetError(txtF2Total, Nothing)

End If
If (txtF2Total.Text = "9") Then
If Not (myControl.Text = "0" Or myControl.Text = "9") Then
ErrorProvider1.SetError(myControl, "is not 0 or 9 !")
myControl.Focus()
myControl.Refresh()
myControl.ResetText()
Else
ErrorProvider1.SetError(txtF2Total, Nothing)

End If

Else
ErrorProvider1.SetError(myControl, Nothing)
End If

Else
ErrorProvider1.SetError(myControl, Nothing)
End If
'Problems with this statement????????
 
Hello,
I'll reply to you with a solution as soon as possible.

For the time being I thought to tell you something that will reduce the code. The text box "txtF2Total" must contain 3 values, 0, 2 or 9. Delete it and add a combo or listbox instead of the txtF2Total. The error will be if the .selectedIndex = -1. The items will be added in design time clicking the items(collection), one per line.

That's for now. I know the solution but i'm sorry i can't post it now. (quite late here)
 
ok this seems like a more elegant way to approach this.. I have added a cbo as follows:

Private Sub cboF2TotalHE_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cboF2TotalHE.Validating

If (cboF2TotalHE.SelectedIndex = -1) Then
ErrorProvider1.SetError(cboF2TotalHE, "It is not 0, 2, 3, 4 or 5 !")
cboF2TotalHE.Focus()
Else
ErrorProvider1.SetError(cboF2TotalHE, Nothing)
End If
End Sub

Now, shall I swap all the other txtboxes for cbo's? If so how easy is it to wire up the conditions as discussed b4?
 
You should replace the textboxes with combos or listboxes because you actually let the user select 0, 2 or 9. In other case you show him an error message that only these values are accepted. So show him what to choose; no textboxes.

It is simple. Just add them and replace the "txt" to "cbo" as you have done it correctly. The combos should be dropdownlist so he can choose only (not edit). The condition is for everything: .SelectedIndex = -1 means has not selected anything.


So:

Code:
    [COLOR=blue]Private Sub FillComboBox(ByVal comboName As System.Windows.Forms.ComboBox, ByVal items As String)
        Dim _items() As String = items.Split(",")
        Dim i As Integer

        comboName.Items.Clear()

        For i = 0 To _items.Length - 1
            comboName.Items.Add(_items(i))
        Next
    End Sub[/color]


EXAMPLE: The combo cboF2TotalHE has "1" (index 0), "3" (index 1), "4" (index 2) and "9" (index 3).
If you select the "1" (which is at index 0 *ZERO BASED*) you want the cboSomething to have the items a,b,c and d.

Code:
    Private Sub [b]cboF2TotalHE_SelectedIndexChanged[/b](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboF2TotalHE.SelectedIndexChanged
        Select Case cboF2TotalHE.SelectedIndex
            Case 0    'selected text = "1"
                [b]FillComboBox(cboSomething, "a,b,c,d")[/b]
            Case 1    'selected text = "3"
                ' etc
            Case 2    'selected text = "4"
                ' etc
            Case 3    'selected text = "9"
                ' etc
        End Select
    End Sub

    Private Sub [b]cboF2TotalHE_Validating[/b](ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cboF2TotalHE.Validating
        If (cboF2TotalHE.SelectedIndex = -1) Then
            ErrorProvider1.SetError(cboF2TotalHE, "You have to select something")
            cboF2TotalHE.Focus()
        Else
            ErrorProvider1.SetError(cboF2TotalHE, Nothing)
        End If
    End Sub


** NOTICE that the items you want the other combos to fill MUST be comma seperated, like "2,45,667,2244".


-
Waiting for your post.
 
aha..you are a genius. It works! how silly of me to be messing around with boolean logic. 1 question, I tried to change the cboF2TotalHE_SelectedIndexChanged event so that it could update all the other cbo's on the form (1) by hardwiring all the cbo's in as below commented code (which works fine) or (2) with the myControl variable, alas this changes the cboF2TotalHE cbo as well (see below). Is there a quick way i can fix this???


Private Sub cboF2TotalHE_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboF2TotalHE.SelectedIndexChanged, cboCentreHElevel.SelectedIndexChanged

Dim myControl As Control
myControl = CType(sender, Control)

Select Case cboF2TotalHE.SelectedIndex
Case 0 'selected text = "1"
'FillComboBox(cboCentre, "2,9")
'FillComboBox(cboInnerSuperiorHElevel, "2,9")
'FillComboBox(cboInnerTemporalHElevel, "2,9")
FillComboBox(myControl, "2,9")

Case 1 'selected text = "3"
' etc
Case 2 'selected text = "4"
' etc
Case 3 'selected text = "9"
' etc
End Select
End Sub
 
I'd suggest you leave it as it is with the FillComboBox called as many time as you need. That you use this sub makes your code cleaner and very easy to do some changes in less than a minute.

Although if you want to pass the "2,9" in all controls (combos) you can do something like:

' assume that the combos are on the form; the form contains them. See the "Me" keyword.

Dim ctl as control
For each ctl in Me.Controls 'control collection
if typeof ctl is combobox then ' take only the combos
FillComboBox(ctype(ctl,combobox), "2,9") 'ctype to make the ctl a combobox, because the sub accepts a combobox and not a control
end if
next
 
Advice taken. Thanks again for your help in getting me up and running with this.
 
Me again

Once the user has clicked the save button I am trying to refresh the forms combobox controls (so that the text property is displayed again), however I can't seem to find the correct code to do this... the only success I have had is by using the cboF2TotalHE.SelectedIndex = -1 code, but this just leaves the cbo as blank!! Do I need to clear the dataset at all? Have trawled the FAQ but no luck! see below for context and the commented code that I have unsuccessfully tried. Can you help at all?



Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

SaveChanges()

MsgBox("Ok", MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, "Changes Saved you hope")
'''''''''''''''''''''''''''''''''''''
'Added from Sub btnDataNew_Click
'Finish the current edit.
'''''''''''''''''''''''''''''''''''''

m_CurrencyManager.EndCurrentEdit()
''''''''''''''''''''''''''''''''''''''''''''
' Add the new record. This automatically
' causes the CurrencyManager to reposition
' so it displays the new position.
'''''''''''''''''''''''''''''''''''''''''''''
m_CurrencyManager.AddNew()

'Below attempts have all failed
'refresh form???????????????????
'Me.Refresh()
cboF2TotalHE.Refresh()
'cboF2TotalHE.SelectedIndex = -1
' Me.cboF2TotalHE = ""
'cboF2TotalHE.
'Me.cboF2TotalHE.Name = "cboF2TotalHE"
'cboF2TotalHE.SelectedIndex = Nothing
'Me.dsLogging.Clear()
' cboF2TotalHE.SelectedValue = "cboF2TotalHE"
'Me.cboF2TotalHE.Text = ""
'Me.cboF2TotalHE.SelectedValue = 0
'Me.cboF2TotalHE.SelectedIndex = ""
'FillComboBox(cboF2TotalHE, "")
'FillComboBox(cboCentre, "cboCentre")
'cboF2TotalHE.Items.Clear()


' Set the focus to the FileName field.
txtFileName.Focus()

End Sub
 
Hi,

I did not understand this: "(so that the text property is displayed again)". What i got but not sure is that after saving you'd like to "reset" everything so the user can make his selections again and then click save, etc..


About the errors:

'Me.cboF2TotalHE.Name = "cboF2TotalHE". The name is readonly. You specify it at design time and use it to reference the controls: <ConctolName>.<Something>[=SomethingElse]

'Me.cboF2TotalHE.SelectedIndex = "". The result is Integer. If you set the dropdownstyle property to dropdownlist then the user can only select something;he cannot edit (not behaving like textbox). At this case you can have selected nothing by: cboF2TotalHE.SelectedIndex=-1
Generally arrays are zerobased. So the index of the first item is 0, the 2nd's is 1, ... the xth's is x-1. The number of items is x (in .NET is .Length property).

'FillComboBox(cboCentre, "cboCentre"). The 2nd string argument should be a string with items, delimited by "," like "a,b,c,d,....,o". This sub will do:
cboCentre.items.add("a")
cboCentre.items.add("b")
cboCentre.items.add("c")
...
cboCentre.items.add("o")
There should not be an error if you write "SomethingWithoutComma". The generated array in the sub will by _items(0)="SomethingWithoutComma".
 
Sorry my description was a bit vague and you are right...I would like to reset all the controls so that the user can make selections again, click save and then do the same process again etc.. I am sure that this was relatively easy in Access with the following code e.g. Me!cboF2TotalHE = ""

Having said that i did try cboF2TotalHE.ResetText()and this gave the same result as cboF2TotalHE.SelectedIndex=-1. Therefore is it possible to reset the cbo control to display the designtime readonly name??

 
Write a sub (private or public) like:

Sub ResetTheForm()
cboSomething.items.clear 'etc
txtSomething.Test=String.Empty 'etc
txtSomething.focus
' ...
End Sub

In the design time the cbos display their name so as to know which is what. Else you should click the control and see its name by the properties tab. If you want to do something like that just do:

The dropdownstyle should be "dropdown" and set the text property to the string you want.

There is the tooltip or labels to display more info.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top