Sticky Button

NssB

Member
Joined
May 11, 2007
Messages
13
Programming Experience
Beginner
Hi all,

Looking for help with a VB.NET form issue. Have a button which runs a Do Loop until a variable is set by the click event of another button. However, the Button which starts the logging routine remains depressed until I click elsewhere on the form.....more of an annoyance issue, which requires a click anywhere on the form before you can click the 'Stop Logging' button(or two clicks on that button, if you like.)

Code:

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] GlassButton1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] startButton.Click[/SIZE]
[SIZE=2][COLOR=#008000] 'run startlogging subroutine[/COLOR][/SIZE]
[SIZE=2] loggingState = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] radioDetailed.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]     logDetailed()[/SIZE]
[SIZE=2][COLOR=#0000ff] Else[/COLOR][/SIZE]
[SIZE=2]     logBasic()[/SIZE]
[SIZE=2][COLOR=#0000ff] End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] logDetailed()[/SIZE]
[SIZE=2][COLOR=#008000]'start logging detailed stuff[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] loggingState <> [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]    'slow the loop down[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] timeOut [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = Now.AddMilliseconds(200)[/SIZE]
[SIZE=2][COLOR=#0000ff]    Do[/COLOR][/SIZE]
[SIZE=2]        Application.DoEvents()[/SIZE]
[SIZE=2][COLOR=#0000ff]    Loop[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Until[/COLOR][/SIZE][SIZE=2] Now > timeOut[/SIZE]
[SIZE=2][COLOR=#008000]'read in the text file line by line[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] oRead [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.IO.StreamReader[/SIZE]
[SIZE=2]oRead = IO.File.OpenText([/SIZE][SIZE=2][COLOR=#800000]"C:\sample.txt"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] oRead.Peek <> -1[/SIZE]
[SIZE=2]    logText.Text += oRead.ReadLine() & vbCrLf[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] GlassButton2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] stopButton.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Set the logging state to false to stop the logging routine[/COLOR][/SIZE]
[SIZE=2]loggingState = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
that's because you've got loop running inside a loop that ties up the UI thread while it's running.

what you need to do is put the log detailed code onto it's own thread, a timer control would be perfect for this

add a timer to the form and set it's miliseconds property to 200 and put the LogDetailed code into the timer's tick event

now all you need to do is start/stop the timer knowing the timer's tick event will fire every 200 miliseconds

edit:
an easy way to start/stop the timer would be to:
VB.NET:
Private LogDetailed As Boolean = False
Private LogBasic As Boolean = False

PrivateSub GlassButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
 'run startlogging subroutine
 If radioDetailed.Checked = True Then
     LogDetailed = Not LogDetailed
     tmrLogDetailed.Enabled = LogDetailed
 Else
     logBasic()
 EndIf
EndSub

PrivateSub GlassButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopButton.Click
'Set the logging state to false to stop the logging routine
  LogDetailed = False
  tmrLogDetailed.Enabled = LogDetailed
EndSub
 
Back
Top