|
-
Aug 15th, 2006, 02:30 PM
#1
Thread Starter
Lively Member
[RESOLVED] Getting the middle number
My problem is how can I get the value of the middle number in a textbox?
For example:
1.
Number: 563921
The Middle Number is: 39
2.
Number: 63942
The Middle Number is: 394
I want the middle number to be squared (39^2) and displayed it in a textbox.
Note:
The middle number must be atleast 2 digit number. Meaning, if the length of the number is even number, the middle number has 2 digit while if the length is odd number then the middle number is 3 digit.
Last edited by Hack; Aug 25th, 2006 at 08:23 AM.
Reason: Added [RESOLVED] to thread title and green "resolved" checkmark
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 02:37 PM
#2
Re: Getting the middle number
VB Code:
Private Sub Command1_Click()
MsgBox GetMiddleNumber("123456789")
MsgBox GetMiddleNumber("12345678")
End Sub
Private Function GetMiddleNumber(sNum As String) As Long
If Len(sNum) Mod 2 = 0 Then
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
Else
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
End If
End Function
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Aug 15th, 2006, 02:38 PM
#3
Re: Getting the middle number
oops.. sorry:
VB Code:
Text1 = GetMiddleNumber("123456789") ^ 2
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Aug 15th, 2006, 02:56 PM
#4
Thread Starter
Lively Member
Re: Getting the middle number
What is the meaning of CLng?
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 03:00 PM
#5
Re: Getting the middle number
Convert to long...
Clng
Cint
CDate
etc.. its so it converts it to a number for calculations
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Aug 15th, 2006, 03:13 PM
#6
Thread Starter
Lively Member
Re: Getting the middle number
Can I use:
sNum = Val(txtKeyValue.Text)
Since it is the user who will input the value of the number?
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 03:15 PM
#7
Re: Getting the middle number
Yes. Val Or CInt/CLng or whatever type the sNum variable is.
-
Aug 15th, 2006, 03:19 PM
#8
Thread Starter
Lively Member
Re: Getting the middle number
Clng = Convert to Long
Cint = Convert to Integer
CDate = Convert to Date
??? = Convert to String
Cstr ???
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 03:23 PM
#9
Re: Getting the middle number
Yes. To check out all conversions, goto to Object Browser in your VB and input "Conversion" in the search box. It will show you the complete Conversion class (methods, functions). You also have all other classes there.
-
Aug 15th, 2006, 03:33 PM
#10
Thread Starter
Lively Member
Re: Getting the middle number
I used this code but nothing happens:
Private Function GetMiddleNumber(sNum As String) As Long
sNum = CStr(Val(txtKeyValue(0).Text))
If Len(sNum) Mod 2 = 0 Then
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
Else
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
End If
End Function
Private Sub cmdCompute_Click()
y = Val(txtLKeyValue.Text) - 1
For x = 0 To y
w = Len(txtKeyValue(x).Text)
If w <> Val(txtSKeyValue.Text) Then
MsgBox "Incorrect input"
txtKeyValue(x).SetFocus
Else
lblAnswer(0).Caption = GetMiddleNumber("123456789") ^ 2
End If
Next
End Sub
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 03:51 PM
#11
Thread Starter
Lively Member
Re: Getting the middle number
The code is working, hehehe! I forgot to make the lblAnswer(x) visible. Sorry!
Another Problem:
When I input 3 digit number, it only displays the square of the last 2 digit number. It should be the 3 digit.
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 04:16 PM
#12
Re: Getting the middle number
just modified Static's code:
VB Code:
Private Sub Command1_Click()
'since you are passing the value entered by user in text box
'check before hand if the number entered is single digit or not
'like this
'
'If Len(Text1.Text) = 1 then Msgbox "Number too Small"
MsgBox GetMiddleNumber("123456789")
MsgBox GetMiddleNumber("12345678")
MsgBox GetMiddleNumber("123")
MsgBox GetMiddleNumber("12")
End Sub
Private Function GetMiddleNumber(sNum As String) As Long
Select Case Len(sNum)
Case 2, 3: GetMiddleNumber = CLng(sNum)
Case Else
If Len(sNum) Mod 2 = 0 Then
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
Else
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
End If
End Select
End Function
-
Aug 15th, 2006, 04:37 PM
#13
Thread Starter
Lively Member
Re: Getting the middle number
Problem Solved!!! Thanks!
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Sep 17th, 2006, 08:46 AM
#14
Junior Member
Re: [RESOLVED] Getting the middle number
what is the use of this txtLKeyValue.Text and txtSKeyValue.Text ??
so it means you have
txtKeyValue arrays
lblAnswer arrays
txtLKeyValue.Text ??
txtSKeyValue.Text ??
-
Sep 17th, 2006, 08:54 AM
#15
Fanatic Member
Re: [RESOLVED] Getting the middle number
I think it's really better to use a control array rather that
text1.text upto how many textbox you want to do ..... but in a label no need to array it .....
-
Sep 17th, 2006, 10:08 AM
#16
Junior Member
Re: [RESOLVED] Getting the middle number
i tried the code in post # 10
i created an control array of
txtKeyValue textboxes
lblAnswer labels
and a cmdCompute command button
like this illustration below:
txtKeyValue lblAnswer
[====] [====]
[====] [====]
[====] [====]
[Enter]
now where do i put the txtLkeyValue and txtSkeyValue ??
and what does it do ??
can anyone answer??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|