Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB2008 - Keeping a checkbox from being changed 1

Status
Not open for further replies.

cycledick

Technical User
Oct 6, 2005
141
US
I hate being a n00b. I’m such a VB novice that I have no idea what I’m doing and am simply reading the help file, trying something, reading some more, etc…

BE GENTLE…

I have a timer that is being activated by a button. When I click the button it starts. When I click it again, it stops. It then takes the elapsed time and saves the value (to be put in a database later, but baby steps). It keeps a running total for every time I start/stop the timer. That part works. I’m sure it’s not the best way, but I thought it was cool I was able to get that to work. Anyways….

I added a check box so that when the checkbox is checked, it saves the elapsed time value in a different field. That works too. Woo Hoo! (regular work hours vs. OT hours – it’s for keeping track of my time)

Now, I want to be able to make sure that if I try and click on the checkbox (whether it’s on or off) while the timer is “on” then it will give me an error message, NOT change the checkbox, and NOT interrupt the timer.

So far I’ve been checking to see if the timer is “off” by setting the value of the field to a bizarre date and time, 10/10/1000. Whenever I “stop” the timer, I reset all the values to 10/10/1000.

I have figured out how to make sure the timer is “off” by simply checking to see if the value of my “time” field is 10/10/1000. But, I’m having trouble figuring out how to see if the checkbox has changed without upsetting the process. I’ve seen some examples of how to determine the state of the checkbox, but it’s a little over my head. I’ve been trying to check to see if the checkbox is “on” and the value is NOT 10/10/1000 (that means the timer is on) then keep the checked value on and show a pop-up window. Example;

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = False And BeginTime <> #10/10/1000# Then
CheckBoxChecked()
End If
If CheckBox1.Checked = True And BeginTime <> #10/10/1000# Then
CheckBoxUnchecked()
End If
If BeginTime = #10/10/1000# Then
SetEverythingToBlank()
End If
End Sub

Private Sub CheckBoxUnchecked()
CheckBox1.Checked = False
MsgBox("The Timer Has Already Started")
End Sub
Private Sub CheckBoxChecked()
CheckBox1.Checked = True
MsgBox("The Timer Has Already Started")
End Sub

It does NOT like me trying to set the value back to the value it was when I clicked on the checkbox. I get a “System.StackOverflowException” when it hits the “checkbox1.checked = “ step in either the CheckBoxUnchecked or the CheckBoxChecked subs.

Does someone have a way of doing this that I might be able to understand? I appreciate any direction or advice anyone has.
 
Code:
	Private TimerRunning As Boolean = False

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		If TimerRunning Then
			Timer1.Stop()
			TimerRunning = False
			CheckBox1.Enabled = True
		Else
			CheckBox1.Enabled = False
			Timer1.Start()
			TimerRunning = True
		End If

	End Sub

This sets a From level variable [tt]TimerRunning[/tt] to True when the Timer is started and to False when it is stopped. The CheckBox is enabled ONLY if the Timer is NOT running.


Hope this helps.

[vampire][bat]
 
A slightly more complete answer:

Code:
Public Class Form1

	Private TimerRunning As Boolean = False
	Private StartTime As DateTime

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		If TimerRunning Then
			Timer1.Stop()
			Dim ElapsedTime As TimeSpan = DateTime.Now.Subtract(StartTime)
			Dim DisplayString As String = _
			 ElapsedTime.Hours.ToString("00") + ":" + ElapsedTime.Minutes.ToString("00") + ":" + ElapsedTime.Seconds.ToString("00")
			If CheckBox1.Checked Then
				Label1.Text = DisplayString
			Else
				Label2.Text = DisplayString
			End If
			TimerRunning = False
			CheckBox1.Enabled = True
		Else
			CheckBox1.Enabled = False
			StartTime = DateTime.Now
			Timer1.Start()
			TimerRunning = True
		End If

	End Sub

End Class

As you can see, for this example there is no need for any code for the Timer1.Tick event or the CheckBox.CheckChanged event.

Alternatively, instead of using TimerRunning, you could use the Text property of the Button, by alternating between "Start" and "Stop".


Hope this helps.

[vampire][bat]
 
Do you actually need the Timer?

Code:
Public Class Form1

	Private StartTime As DateTime

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Button1.Enabled = False	'Prevent user form clicking while we are processing
		If Button1.Text = "Stop" Then
			Dim ElapsedTime As TimeSpan = DateTime.Now.Subtract(StartTime)
			Dim DisplayString As String = _
			 ElapsedTime.Hours.ToString("00") + ":" + ElapsedTime.Minutes.ToString("00") + ":" + ElapsedTime.Seconds.ToString("00")
			If CheckBox1.Checked Then
				Label1.Text = DisplayString
			Else
				Label2.Text = DisplayString
			End If
			CheckBox1.Enabled = True
			Button1.Text = "Start"
		Else
			Button1.Text = "Stop"
			CheckBox1.Enabled = False
			StartTime = DateTime.Now
		End If
		Button1.Enabled = True

	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		Button1.Text = "Start"
		CheckBox1.Checked = False

	End Sub
End Class


Hope this helps.

[vampire][bat]
 
Ah, damn it. That's why I hate being a newbie. You did in about 24 lines of code what took me literally hundreds. It's almost like being a good software programming is a highly-paid skilled job. Oh wait, it is. No wonder I can't read the tutorial for a few hours then start pumping out apps. DAMN!

I was looking at that boolean thing before I posted because that's what was in the VB examples too. But, I just don't get it. What does "If TimerRunning Then" mean? Isn't "TimerRunning" a variable? I know it's set to false at the beginning, but when I use an IF statement, don't I have to tell it IF what?

The third example makes more sense to me because it's checking actual "things" rather than variables (I start to lose track). Ulitmately I'm going to want a bunch of buttons to show up with different customer's names on them so I can quickly click on whatever customer I want to track time on. I do a lot of 5 minutes here 5 minutes there type of stuff. I haven't gotten that far yet, but I was hoping I could do something like make the button stay "pressed" when it's on, so I know it's on. Or change color or something. I thought I'd have to figure out a way to just set a variable to on or off so I could check that. But, now I'd really like a little time to look over the examples you gave me too.

This stuff is hard!
 
[tt]If TimerRunning Then[/tt]


Sorry about that. I'm so used to using the shorthand syntax, I forgot to explain.

[tt]If[/tt] requires a condition that evaluates to either True or False (in other words a Boolean expression)

[tt]If MyHandle = "earthandfire" Then[/tt], is either True or False.

When using Boolean variables, they have a value which is either True or False and as such you don't need to use:

[tt]If MyBooleanVariable = True Then[/tt]

[tt]If MyBooleanVariable Then[/tt] is exactly the same.

Use whichever version of the syntax that you feel most comfortable with.


Hope this helps.

[vampire][bat]
 
>> but I was hoping I could do something like make the button stay "pressed" when it's on, so I know it's on. Or change color or something

Look at my third example - swapping the Text (caption) of the button between "Start" and "Stop". If the button says "Stop" - the process is being timed, if it says "Start" it is waiting to be timed.


Hope this helps.

[vampire][bat]
 
So, if

If MyBooleanVariable Then
is the same as
If MyBooleanVariable = True Then

does that mean
If NOT MyBooleanVariable Then
is the same as
If MyBooleanVariable = False Then
 
It certainly does.

It can also make for easier reading of the code if you carefully name your Boolean variables.

[tt]Private ok as Boolean = False 'initialise to False[/tt]

elsewhere in your code:

[tt]If ok Then
...
Else
...
End If
[/tt]

or

[tt]
If Not ok Then
...
Else
...
End If
[/tt]


Hope this helps.

[vampire][bat]
 
That does help.

Is there a particular reason one would use one form of the IF statement over the other? Or, is it just one of those things where there is more than one way to do it?

I'll mess around with that for a while and undoubtedly post again when I want to do the next thing that takes more than dragging and dropping an item on to a form.


Thanks a lot!
 
Readability?

[tt]If ok Then[/tt] is more readable and obvious in its meaning than [tt]If ok = True Then[/tt]

Much depends on how you name your Boolean variables (and personal preference, of course)

[vampire][bat]
 
Personal preference is certainly important. As is habit!

For example, I would almost invariably code If ok = True Then as this is what I did right when I started coding 10 years back. I got in a habit. I still find If ok Then harder to read!

I guess that makes me.....well, slightly less competant than I should be!

C
 
Many moons ago when I was teaching programming, I would demonstrate both methods and leave it up to the student to use whichever they felt most comfortable with.

Personally, when using a Boolean variable, I almost always use the short syntax - as you say habit.

I think, probably, the most important criteria to determine which method to use, is to decide which method you are happier with and which makes more sense. There is no pont in using the short syntax if everytime you read it, you have to stop to think what it means.

Craig0102 said:
I guess that makes me.....well, slightly less competant than I should be!

I disagree, competence is NOT an issue.

[vampire][bat]
 
I would have to say if it didn't make any difference, and since I am NOT a professional programmer and will NOT being doing this all the time, that I will do the "IF ok = True THEN". I spent literally about an hour going over and over the example in VB that said "If ok Then" trying to figure out what the heck it meant. Actually, the example was "If NOT ok THEN" which really didn't make any sense. Now that someone told me what it means it does makes sense, but I would be stopping every time to say it outloud to make sure I knew what it meant. I'll just go ahead and get my habit started now. =:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top