Help With syntax

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Help With syntax

 
0
  #1
Feb 19th, 2008
i have a problem making the code for this logic


i have 5textbox ,1 label and a command button on a form and
need to do this:

if the value of the 1st textbox is greater than the 2nd,3rd,4th,5th
then label1.caption = to the value of the 1st text box
if the value of the 2nd textbox is greater than the 1st,3rd,4th,5th
then label1.caption = to the value of the 1st text box
and so on.. until the 5th

'''''''''''''''''''''''''''''''''''''''''''''''''''
i tried making code like

if txt1 > txt2 then
label1.caption = txt1
end if

if txt1 > txt3 then
label1.caption = txt1
end if

and so on...

but having a problem when it come to comparing the

"2nd to the 1st"
"3rd to the 1st"
and so on .....


it still have a problem on selecting the highest value on the 5 textboxes


pls help me............. getting dizzy thinking of the solution
Last edited by hawisme000; Feb 19th, 2008 at 10:53 am.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Help With syntax

 
0
  #2
Feb 19th, 2008
Make sure that you define these variables integer based.

it might be doing a text comparison because of data type.

Store the variables into a temp var that is defined as integer and then run them through your if statements.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. if txt1 > txt2 or txt1 > txt3 or txt1 > txt4 or txt1 > txt5 then
  2.  
  3. label1.caption = txt1
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Help With syntax

 
0
  #3
Feb 19th, 2008
why use or??
if the value 3 2 4 5 6 then the result is 3 cause the first state is pass (3 > 2).
when use or operand, if one state was passed then another state not checked by prog again.
use and operand.
  1. If Val(Text1.Text) > Val(Text2.Text) And Val(Text1.Text) > Val(Text3.Text) And Val(Text1.Text) > Val(Text4.Text) And Val(Text1.Text) > Val(Text5.Text) Then
  2. Label1.Caption = Text1.Text
  3. End If
Last edited by Jx_Man; Feb 19th, 2008 at 12:03 pm.
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 538
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Help With syntax

 
0
  #4
Feb 19th, 2008
why you are using 5 separate boxes? to solve this problem more efficiently try control arrays. this will be more easier for you. check out this sample code, it is implemented on control arrays. one advantage of this code is you do not need to encounter each & every textboxes to find out the maximum value among all values. you do not need to write those long if conditions also. this code is dynamic. it is capable to find out the max. value without bothering how many textboxes u used in your form. just put a single textbox and make no. of copies of it that you want. this will create the control arrays of the textbox. now put a label and fire this code. you will get your answer.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim ctlControl As Object
  2. Dim x() As Integer, c As Integer, i As Integer, max As Integer
  3.  
  4. c = 0
  5. On Error Resume Next
  6.  
  7. For Each ctlControl In Me.Controls
  8. If TypeOf ctlControl Is TextBox Then
  9. c = c + 1
  10. DoEvents
  11. End If
  12. Next ctlControl
  13.  
  14. ReDim Preserve x(c)
  15.  
  16. max = Text1.Item(0)
  17. For i = LBound(x) To UBound(x) - 1 Step 1
  18. If max < Val(Text1.Item(i)) Then
  19. max = Val(Text1.Item(i))
  20. Else
  21. max = max
  22. End If
  23. Next i
  24.  
  25. Label1.Caption = max

ok by implementing this logic i've not meant that others methods are not workable. no i'm not saying that at all. but this logic is more practical.

try this and get me your feedback.

ok......
good luck

regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Help With syntax

 
0
  #5
Feb 19th, 2008
i post it cause previous post use or operand.
but you have a great code. nice one
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 538
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Help With syntax

 
0
  #6
Feb 19th, 2008
ok by implementing this logic i've not meant that others methods are not workable. no i'm not saying that at all.
you saw that i didn't underestimate any one coz i respect all of u guys.

have a nice day

regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Re: Help With syntax

 
0
  #7
Feb 20th, 2008
SORRY FOR THE VERY LATE REPLY EVERY ONE, JUst got home from school,

sir shouvi

im having a proj about my subject --> OS (Operating System)
and he want me to make a program about "Priority First"

like .. this (just sharing the story why i came up with this prob )

------------CPUTIME-------------------PRIORITY TIME
P1-------------#----------------------------------#
P2-------------#----------------------------------#
P3-------------#----------------------------------#
P4-------------#----------------------------------#
P5-------------#----------------------------------#


where # is the input number
--------------------------------------------------------------------
as you can see from above there is 5 input for Priority time , 1 for each P#

thats y i came up with the idea of using 5txtbox(but i dndt think of using array ,thx 4 dat)


im currently making it again, wit the use of the syntax u gave (ty all!)

''
il reply again of what came up

TY OL!!!
Last edited by hawisme000; Feb 20th, 2008 at 10:06 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Re: Help With syntax

 
0
  #8
Feb 20th, 2008
sir shouvi!!! ur code is Great!!!

i does solve my prob, ty soo much,

but before i end this post , on your code,
there are part of it i cant understan (the code's meaning and use)
it my 1st time encountering this part of the code

-Me.Controls (does this point for the objects in the form to?? like ,Dim ctlControl As Object)
-DoEvents (umm.. where is the event? umm..where it start?)
-ReDim Preserve x(c) (does it preserve all values put on dim C?? cnt understand d logic)

-For i = LBound(x) To UBound(x) - 1 Step 1
(also how to read this in logic)

pls?? wanna learn this part really,

jx_man ty for the help and ideas too,,
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 538
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Help With syntax

 
0
  #9
Feb 20th, 2008
Originally Posted by hawisme000 View Post
sir shouvi!!! ur code is Great!!!

i does solve my prob, ty soo much,

but before i end this post , on your code,
there are part of it i cant understan (the code's meaning and use)
it my 1st time encountering this part of the code

-Me.Controls (does this point for the objects in the form to?? like ,Dim ctlControl As Object)
-DoEvents (umm.. where is the event? umm..where it start?)
-ReDim Preserve x(c) (does it preserve all values put on dim C?? cnt understand d logic)

-For i = LBound(x) To UBound(x) - 1 Step 1
(also how to read this in logic)

pls?? wanna learn this part really,

jx_man ty for the help and ideas too,,
ok
i am solving one by one. this is going to be a long post. plz hv patience when u read it.

your first question :- Me.Controls (does this point for the objects in the form to?? like ,Dim ctlControl As Object)

Answer : quiet so but not the same as ctlcontrol. here ctlcontrol is just an instance of the object type. as the code is dynamic (capable to encounter every textboxes without bothering how many r there) how could it do that?
notice this line : for each ctlcontrol in me.controls
this line is telling the compiler to iterate through each objects represented as controls in the current form and if the currently scanned object is a textbox then just increament the value of c by 1. here c is declared to store the total no. of textboxes present in the current form. so in this way you will get how many tb you have in ur form and it doesn't require you to maintain code for each textboxes.

your second question : - DoEvents

Answer : this is a system defined procedure. this is not indicating to any physical events that may exists on any of ur forms. DoEvents is a predefined sub-routine in vb6 which yield execution of an ongoing process of the compiler so the operating system can perform other operations that are waiting in the process queue.

your third question : ReDim Preserve x(c) (does it preserve all values put on dim C??

Answer : see i've declared an array there ----->dim x() as integer, here x is an integer array but have you noticed the "()"? look there is no dimension size mentioned. as we all know an array must be initialized when the same is declared. now in the code u r trying to iterate through each text box and counting how many of them r there. but before running the for each..next statement how could the program will know how many text boxes are there? it can't be and x() is declared to store the values of each text box one by one. so, in such case when we do not know the dimension size from advance we can declare an array with null no of elements means it can hold n number of values. now after running the for each..next statement when we found total no. of text boxes then what we will do, we just re-initialize the array with the appropriate dimension size we just got in order to make sure that our program is not accessing un-neccessery memory. in vb you use Redim to re-declare a variable and that's what exactly i'm doing here Redim Preserve x(c)
now this line is just re-declaring the array x with a proper dimension size computed dynamically which is stored in the variable c. for example, u have a 5 text boxes. so c will contain 5 and when this line is fired x will be re-declared & re-initialized with dimension 5 and it will look like as x(5). the preserve command is used to store the new dimension size.

your fourth question : -For i = LBound(x) To UBound(x) - 1 Step 1

Answer : See the logic involved in this code is very simple. after counting each instance of the text boxes i'm running a for loop to store value of each tb into current element of the integer array x. that is implemented by using Text1.item(index), for example x(0)=text1.item(0) , it will store value of the first tb in sequence into the first element of array x and so on. now after taking values we need to take a value and compare it with rest of each one to find the max. value. now to iterate through an array you must start from element 0 and the process must stop when u reach the last element which is total dimnesion size-1. the ubound() returns the highest element no./index/subscript of an array. in this case it is 5 but we need to iterate from 0 to 4. otherwise system will fetch "subscript out of range" error. so how can we find the last/final element to scan? for this this is used ubound(x)-1 , which returns 4 i.e. total dm size (5) - 1 =4. similarly lbound returns the first index of the array which is always be 0. so u can mention 0 in case of lbound(x) also.

hope these explanations are enough to clear ur doudts.
if u want more then just do some googling.......

ok......
bye

regards
Shouvik
Last edited by choudhuryshouvi; Feb 20th, 2008 at 2:33 pm.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Re: Help With syntax

 
0
  #10
Feb 20th, 2008
thank you soo much for the effort in explaining it,
cause of if im olready finishing mu proj,
thank you sir! (ill memorize what you have explain more)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1480 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC