-
Jul 27th, 2016, 03:32 AM
#1
Thread Starter
Junior Member
[RESOLVED] Execute While loop when button pressed
Dear all,
I am trying to code with visual basic 2010.
I have 2 button START and STOP . if start button pressed while loop should executed with printing "hello" first time and "welcome"in second time . This loop should executed evey 5s. If stop button pressed while loop should end print " stop button pressed on same rich textbox
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_click.Click
Timer1.Interval = 1000 '3 seconds
Timer1.Enabled = True
RichTextBox1.Text = "hello"
RichTextBox1.Text = "\n wait\n"
End Sub
Private Sub Button_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_stop.Click
Timer1.Enabled = False
RichTextBox1.Text = "Button stop pressed"
End Sub
End Class
-
Jul 27th, 2016, 04:55 AM
#2
Thread Starter
Junior Member
Re: Execute While loop when button pressed
Code:
Option Explicit On
Public Class Form1
Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
Dim stopclick As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_click.Click
While True
Application.DoEvents()
sleep_program()
RichTextBox1.Text = "hello"
sleep_program()
RichTextBox1.AppendText(RichTextBox1.Text)
' Perform the while statements
If stopclick Then
stopclick = False
Exit While
End If
End While
End Sub
Private Sub Button_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_stop.Click
stopclick = True
RichTextBox1.Text = "Button stop pressed"
End Sub
Sub sleep_program()
Threading.Thread.Sleep(3000)
End Sub
End Class
Modified code as below. But i could not able to acheieve below thing
I have two button start and stop.
start button to start while loop and stop button to stop the while loop
when button has pressed richtextbox has to print "hello" -> delay(1000)-> print "welcome" ->delay(1000)-> print "hello" ->delay(1000)-> print "welcome" -> and so on.If button stop pressed it should stop printing "welcome" and "hello"
-
Jul 27th, 2016, 10:01 AM
#3
Re: Execute While loop when button pressed
Why not use a timer and just use the buttons to change the state of what the timer code is doing.
Code:
Public Class Form1
Private StepState As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_click.Click
Timer1.Interval = 1000 '1 second
Timer1.Enabled = True
StepState = False 'always start with hello
RichTextBox1.Text = "Hello"
End Sub
Private Sub Button_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_stop.Click
Timer1.Enabled = False
RichTextBox1.Text = "Button stop pressed"
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If Timer1.Enabled = True 'Might get one tick event after timer disabled, so verify it is still enabled
StepState = Not StepState
If StepState Then
RichTextBox1.Text = "Welcome"
Else
RichTextBox1.Text = "Hello"
End If
End If
End Sub
End Class
-
Jul 27th, 2016, 10:34 AM
#4
Re: Execute While loop when button pressed
I almost totally agree with passel. The one situation where using a loop would make sense here is if this was for a class (where it might be a requirement) or if those sleep_program() calls didn't really sleep. For all other cases, a timer would be the way to go.
My usual boring signature: Nothing
Tags for this Thread
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
|