Solved

Simple Newbie VB questions

Posted on 2006-11-03
9
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Six Sigma Control Plans
Introduction to Processes

688 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