i have a code which is in the if-then-else statement...
then i want to use the select case statement...
and i dont know well on this...
hope you can help me...
the code is

Private Sub cmdGo_Click()

Dim postA As Single
Dim postB As Single


If Combo1 = 1 Then
For postA = 0 To 4970 Step 0.1
Shape1.Left = postA
Next
For postB = 11175 To 5550 Step -0.1
Shape2.Left = postB
Next
For postB = 5550 To 11175 Step 0.1
Shape2.Left = postB
Next


ElseIf Combo1 = 2 Then
For postA = 0 To 4970 Step 0.1
Shape1.Left = postA
Next
For postB = 11175 To 5550 Step -0.1
Shape2.Left = postB
Next
For postA = 4970 To 0 Step -0.1
Shape1.Left = postA
Next


ElseIf Combo1 = 3 Then
For postA = 0 To 4970 Step 0.1
Shape1.Left = postA
Next
For postB = 11175 To 5550 Step -0.1
Shape2.Left = postB
Next
For postA = 4970 To 0 Step -0.1
Shape1.Left = postA
Next
For postB = 5550 To 11175 Step 0.1
Shape2.Left = postB
Next


ElseIf Combo1 = 4 Then
For postA = 0 To 4970 Step 0.1
Shape1.Left = postA
Next
For postB = 11175 To 5550 Step -0.1
Shape2.Left = postB
Next
For postA = 4970 To 0 Step -0.1
Shape1.Left = postA
Next
For postB = 4970 To 0 Step -0.1
Shape2.Left = postB
Next


ElseIf Combo1 = 5 Then
For postA = 0 To 4970 Step 0.1
Shape1.Left = postA
Next
For postB = 11175 To 5550 Step -0.1
Shape2.Left = postB
Next
For postA = 5550 To 11175 Step 0.1
Shape1.Left = postA
Next
For postB = 5550 To 11175 Step 0.1
Shape2.Left = postB
Next


ElseIf Combo1 = 6 Then
For postA = 0 To 4970 Step 0.1
Shape1.Left = postA
Next
For postB = 11175 To 5550 Step -0.1
Shape2.Left = postB
Next
For postA = 5550 To 11175 Step 0.1
Shape1.Left = postA
Next
For postB = 5550 To 11175 Step 0.1
Shape2.Left = postB
Next


ElseIf Combo1 = 7 Then
For postA = 0 To 4970 Step 0.1
Shape1.Left = postA
Next
For postB = 11175 To 5550 Step -0.1
Shape2.Left = postB
Next
For postA = 5550 To 11175 Step 0.1
Shape1.Left = postA
Next
For postB = 5550 To 11175 Step 0.1
Shape2.Left = postB
Next


End If
End Sub

Im trying to make a program for our Physics which is collision and collision has 7 types. In every type there is an animation for two objects. That’s it!!!
Hope you can help me…
Thanks and have more powers to you all!!

Dani AI

Generated

Good start — and are right that Select Case is a good way to pick which collision type to run. The part that often trips people up is the animation method: putting long For...Next loops in a Click handler will run tens of thousands of iterations and block the UI so the motion looks choppy or invisible. A more robust approach is to build a short sequence of "steps" (which shape moves to which target) and drive those steps with a Timer. That way the UI stays responsive and motion is time-based, not loop-count based.

High-level approach: for each collision type create a list/array of steps (each step = which shape, target Left). Start a Timer, and on each Tick move the active shape toward its current step target by a distance computed from a desired speed (twips-per-second) and the Timer interval. When the target is reached, advance to the next step. This avoids busy loops, prevents huge iteration counts (steps = Abs(target-start)/Abs(step)), and yields consistent timing across machines.

Example Timer-driven pattern (VB6-style):

Dim seqShape() As Integer
Dim seqTarget() As Single
Dim seqCount As Integer
Dim seqIndex As Integer
Dim speedTwipsPerSec As Single

Private Sub cmdGo_Click()
    ' build an example sequence (replace with the steps for each collision type)
    seqCount = 3
    ReDim seqShape(1 To seqCount)
    ReDim seqTarget(1 To seqCount)
    seqShape(1) = 1: seqTarget(1) = 2000
    seqShape(2) = 2: seqTarget(2) = 8000
    seqShape(3) = 1: seqTarget(3) = 1000
    seqIndex = 1
    speedTwipsPerSec = 4000
    Timer1.Interval = 30
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Dim cur As Single, target As Single, stepSize As Single
    stepSize = speedTwipsPerSec * (Timer1.Interval / 1000)
    If seqIndex > seqCount Then Timer1.Enabled = False: Exit Sub

    If seqShape(seqIndex) = 1 Then
        cur = Shape1.Left: target = seqTarget(seqIndex)
        If Abs(target - cur) <= stepSize Then Shape1.Left = target: seqIndex = seqIndex + 1 Else Shape1.Left = cur + Sgn(target - cur) * stepSize
    Else
        cur = Shape2.Left: target = seqTarget(seqIndex)
        If Abs(target - cur) <= stepSize Then Shape2.Left = target: seqIndex = seqIndex + 1 Else Shape2.Left = cur + Sgn(target - cur) * stepSize
    End If
End Sub

Quick tips: use ComboBox.ListIndex to select the sequence (safer than parsing Text), use Option Explicit and typed variables, and use Screen.TwipsPerPixelX if you need to work in pixels instead of twips. If a quick hack is needed, DoEvents inside the loop will force UI redraws, but Timer is the correct, scalable fix.

Recommended Answers

All 3 Replies

Try this one:

Dim combotext as integer

combotext=val(Combo1.text)

Select case combotext

Case 1
....code here

Case 2
....code here

Case 3
....code here

Case 4
....code here

Case 5
....code here

Case 6
....code here

Case 7
....code here

End Select

thanks for the reply...
sorry for the disturbance...
i don't really know how to start the select case statement...
but i gonna used this code...
thank you very much...

Just like Ryan said just enter the code for each CASE.

In this case (yuk, yuk, yuk) I don't think your saving anything by using Select Case.

Here's the first 3 CASES in code, I'm sure you can handle it from there.

Private Sub cmdGo_Click()

Dim postA As Single
Dim postB As Single

    Select Case combotext
    Case 1
    
        For postA = 0 To 4970 Step 0.1
            Shape1.Left = postA
        Next
        For postB = 11175 To 5550 Step -0.1
            Shape2.Left = postB
        Next
        For postB = 5550 To 11175 Step 0.1
            Shape2.Left = postB
        Next
    
    Case 2
        For postA = 0 To 4970 Step 0.1
            Shape1.Left = postA
        Next
        For postB = 11175 To 5550 Step -0.1
            Shape2.Left = postB
        Next
        For postA = 4970 To 0 Step -0.1
            Shape1.Left = postA
        Next
    
    Case 3
        For postA = 0 To 4970 Step 0.1
            Shape1.Left = postA
        Next
        For postB = 11175 To 5550 Step -0.1
            Shape2.Left = postB
        Next
        For postA = 4970 To 0 Step -0.1
            Shape1.Left = postA
        Next
        For postB = 5550 To 11175 Step 0.1
            Shape2.Left = postB
        Next
        
        ' and so on and so forth until you get to 7
    
    
    End Select

End Sub

BTW...are you using the actual text in the ComboBox or are you using the item.index? You might be safer using the index.

Happy coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.