Solved

Simple Newbie VB questions

Posted on 2006-11-03
9
222 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:ManasiD
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:taycuong76
Comment Utility
* 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
Comment Utility
neither work
0
 
LVL 3

Accepted Solution

by:
taycuong76 earned 500 total points
Comment Utility
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
Comment Utility
Sorry for the delay. Good answer. Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now