Solved

Simple Newbie VB questions

Posted on 2006-11-03
9
225 Views
Last Modified: 2010-04-16
Let me know if I need to brak these up into multiple question...

I am a newbie trying to finish off a project.

Question #1
I am looking for a way to draw lines on a mschart  in VB. When I type a value into a textbox I need a horizontal line to be drawn on the Y axis. If I type in a 2 I need (0,2) to be graphed, and so on.

Question #2
How do I ensure that the data that can be inserted in a textbox are numerical values only.

0
Comment
Question by:zyanj
9 Comments
 
LVL 3

Expert Comment

by:taycuong76
ID: 17872473
1)

2) Assuming that you have a textbox named Text1:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> asc("-") Then KeyAscii = 0
End Sub
0
 
LVL 1

Expert Comment

by:sagaragrawal
ID: 17897264
For the 2nd question :-

If your textbox name is Text1, then you can use below code.

Private Sub Text1_Validate(Cancel As Boolean)
       
    If Not IsNumeric(Text1.Text) Then
        MsgBox "Enter a numeric value please.", vbExclamation, "Alert"
        Cancel = True
    End If
   
End Sub

This will validate the contents in Text1 textbox. It will not allow the user to move off the textbox, untill the user enters a numeric value. Everytime the user enters a non-numeric value, it will show an alert through the message box.

I hope this will do the needful.

Enjoy !!!
0
 

Author Comment

by:zyanj
ID: 17915942
taycuong76 ....

How do i apply this to a textbox that is apart of an array.

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> asc("-") Then KeyAscii = 0
End Sub

Private Sub Text1_Change(Index As Integer)

CODE>>>CODE

End Sub
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Expert Comment

by:ManasiD
ID: 17920578
for your textbox that is a part of an array:

suppose if there are 3 textbox and if you want to put the condition only for one text box (which is of index 1) then:

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    If Index = 1 Then
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then
           KeyAscii = 0
        End If
    End If
End Sub

suppose if the condition is to be put for all textbox in the control array then there is no need for the index condition.

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then
           KeyAscii = 0
        End If
End Sub
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17935258
* You declare a n array of textbox and just add an If...then.... with Index in the Sub:

If Index = ..... Then
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then
           KeyAscii = 0
        End If
End If

* To apply the "numeric textbox" to all of your textbox in a form, you need to use a class module and do something with "With Event" to reference to the class module.
0
 

Author Comment

by:zyanj
ID: 18029879
neither work
0
 
LVL 3

Accepted Solution

by:
taycuong76 earned 500 total points
ID: 18052004
Ok, let me show you in details:

1) Open a new project VB
2) Insert 1 textbox (automatically named as Text1)
3) Select it on the form, press Control + C to copy it to clipboard, then press Control + V to paste it to form. A message box will appear, choose Yes. This will create an array of Text1(2), you can add more by the same way but 2 is enough for this example.
4) Now, paste the following code to the code's form:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
 If Index = 1 Then
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then
           KeyAscii = 0
        End If
    End If
End Sub

This must work, and in the Textbox(1) you only input numbers, Textbox (2) you can input what you can (normal mode).
Tell me if it behave strangely.
0
 

Author Comment

by:zyanj
ID: 18294302
Sorry for the delay. Good answer. Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question