3 Replies - 7706 Views - Last Post: 28 December 2012 - 09:23 AM Rate Topic: -----

#1 nucleus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-December 12

defining all the if's when first loading a form

Posted 28 December 2012 - 01:12 AM

So, I'm creating a form with a label, a picture box and two buttons. This form will control the Spooler service.
The label will say "Running" when the service is running and the picture box will be green. The "Start" button will be disabled and the "Stop" button will be enabled. The label will say "Stopped" when the service is stopped, the picture box will be red, the "Start" button will be enabled and the "Stop" button disabled.

My code below generates errors though.

If Spooler.Status.Equals(ServiceControllerStatus.Running) Then Label1.Text = "Running" And PictureBox1.BackColor = Color.Green And Button1.Enabled = False And Button2.Enabled = True
If Spooler.Status.Equals(ServiceControllerStatus.Stopped) Then Label1.Text = "Stopped" And PictureBox1.BackColor = Color.Red And Button1.Enabled = True And Button2.Enabled = False


I guess this would be a good time to say that I'm just trying things for the first time with VB.NET.

Any suggestions on how to set all the "if's" when the form is first loaded?

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: defining all the if's when first loading a form

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: defining all the if's when first loading a form

Posted 28 December 2012 - 04:01 AM

Hi,

The problem is you are still thinking like a human :) You are thinking this:

Label1.Text = "Running" And PictureBox1.BackColor = Color.Green


means "set label one's text to "Running" AND set picture box one's back color to green", when actually it is a boolean expression.

Label1.Text = "Running"


in the context of the above code, is a boolean expression (so it is either true or false), and is saying 'Is label one's text equal to "Running"?".

Similarly,

PictureBox1.BackColor = Color.Green


is also a boolean expression, and is saying "Is picture box one's back color equal to green?".

The first bit of code above uses And (which is an operator that takes two boolean values as operands) to combine those two boolean expressions into one single, larger boolean expression, that is saying:

"Is label one's text equal to "Running", AND is picture box one's back color green?

If both sides of the And are true, the whole expression is true, otherwise the whole expression is false.

Now, that's obviously not what you want...

To actually assign/set the values of Text and BackColor, you need to write each assignment as a statement, rather than a boolean expression:

If Spooler.Status.Equals(ServiceControllerStatus.Running) Then
    Label1.Text = "Running" 'set label one's text equal to "Running"
    PictureBox1.BackColor = Color.Green 'set picture box one's back color equal to green
End If



Now, the computer will execute each statement inside the If...End If block (if the spoolers status is running, obviously), one line at a time, and because we aren't using And anymore, the compiler is clever enough to work out that you want to perform assignment (i.e. assign "Running" to label one's Text property), rather than create a boolean expression, as you were before.
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: defining all the if's when first loading a form

Posted 28 December 2012 - 04:05 AM

Also it will be a good time if you tell us what error message you are getting...
the syntax for if condition is:
If condition Then '....

So your conditions should be before the Then keyword. Or simply saying, we dont join two statement is vb using And, just you can say:
If Spooler.Status.Equals(ServiceControllerStatus.Running) Then 
Label1.Text = "Running" 
PictureBox1.BackColor = Color.Green 
Button1.Enabled = False 
Button2.Enabled = True
End If

Everyone on its line
Edit: CodingSup3rnatur@l-360 explains it better and ninjaed me..

This post has been edited by smohd: 28 December 2012 - 04:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 nucleus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-December 12

Re: defining all the if's when first loading a form

Posted 28 December 2012 - 09:23 AM

Thank you for the replies. They were very helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1