English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

In my program there r 6 text boxes for subject scores and 6 labels to display grades for the scores.

eg txtMaths for Maths score and lblMaths for maths grade etc

In the program I only want the user to be able to input numbers between 0 and 100 inclusive.Else an error message appears.I have the following code that works fairly well

Private Sub txtMaths_Change()

On Error Resume Next
If CInt(txtMaths)<0 Then txtMaths = " "
If CInt(txtMaths)>100 Then txtMaths =" "

Select Case CInt(txtmaths)
Case 0 To 39:lblMaths="D"
etc
End select
End Sub.

My questions are
1.How do I get the error sg to appear
2. How EXACTLY can I get this code repeated for the otrher subjects without typing it for each subject? .
What about a control array and a loop ? will that work?
Can you give me the code to do this.

2007-03-14 16:17:54 · 3 answers · asked by candy 1 in Computers & Internet Programming & Design

3 answers

Private Sub txtMaths_Change()
On Error GoTo txtMaths_ERR
...
Main Sub code
End Select
EXIT SUB
txtMaths_ERR:
msgbox title:="Error number: "&err.number, prompt:=err.description
End Sub

You can use the same error code for each routine. I like to name my error code with the name PROCEDURENAME_ERR so I know exactly what procedure it is in. The EXIT SUB line right above the error handler line label makes sure that the ERROR code ONLY executes WHEN AN ERROR OCCURS. This code is using VB-6, since you didn't specify a different version of the language.

2007-03-14 16:51:44 · answer #1 · answered by Richard H 7 · 0 0

You can use the form keypress event to detect what key was pressed. On the forms properties enable the KeyPreview

Next you can make a routing that will let you select what type of formatting you want by using a letter code in the TAG property of each text box you wish to format. "N" for numeric
"A" for Alpha nothing for no formatting.

You will need to create a module level variable to capture the tag value which the text box receives focus.

You can create a multi control event handler which fires on the Got Focus event of each textbox

Private myTag As String


Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles TextBox1.GotFocus, TextBox2.GotFocus

Dim tb As TextBox
tb = sender
myTag = tb.Tag


End Sub

Next creat a keypress event handler for Form1

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Dim ch As Char
ch = e.KeyChar

If myTag = "N" Then
Select Case ch
Case "0" To "9"
'do nothing
Case Else
e.KeyChar = "" 'cancel key press
e.Handled = True
End Select

End If

End Sub

The keypress event is where all the work is done to format the keypresses as they are typed. It will be up to you to expand upon the select case function but you should get the idea with what I've provided.

Basically capture the e.KeyChar and test it based on the captured TAG value. If it matches a valid key press then do nothing otherwise you can modify it or cancel it by setting the e.KeyChar to ""

2007-03-14 17:15:27 · answer #2 · answered by MarkG 7 · 0 0

rc.exe is the source compiler. that's merely invoked while you're turning out to be an application with source records. make useful there is not any longer something decrease than source records on your report tree. make useful the seen Studio setting up is sparkling (and legal, if it isn't the freeware convey version).

2016-10-02 03:46:01 · answer #3 · answered by ? 4 · 0 0

fedest.com, questions and answers