• 0

[VB] 'End If' must be preceded by a matching 'If'


Question

Hey, I?m wondering if anyone could tell me what I?m doing wrong.

I have the following if statement:

  Quote
Private Sub chkbold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbold.CheckedChanged

If chkbold.Checked = True Then lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

Else If chkbold.Checked = True False lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

End If

End Sub

I keep getting the errors:

ElseIf must be preceeded by a matching if or else if

End if must be preceded by a matching if.

Ive looked at example if statements and can?t work out what ive done wrong.

Thanks to anyone who can:)elp :)

9 answers to this question

Recommended Posts

  • 0

you can't use the short syntax:

if something then dosomething

in vb if you are using an elseif, it's elseif btw and not else if

the correct syntax is:

if something then

dosomething

elseif something then

dosomethingelse

else

dosomethingentirelyelse

end if

your code should be

Private Sub chkbold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbold.CheckedChanged
If chkbold.Checked Then 
lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)
Else 
lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)
End If
End Sub

  • 0

Thanks so much, I?ve just spent the last hour reading up on this and got nowhere,

My tutor told me it?s probably not compatible with Visual Studio 2008 (were working from tutorials based on 2005)

Thanks ag:)n :)

  • 0
  InsaneNutter said:
Hey, I?m wondering if anyone could tell me what I?m doing wrong.

I have the following if statement:

I keep getting the errors:

ElseIf must be preceeded by a matching if or else if

End if must be preceded by a matching if.

Ive looked at example if statements and can?t work out what ive done wrong.

Thanks to anyone who can:)elp :)

I see error in your code

Private Sub chkbold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbold.CheckedChanged

If chkbold.Checked = True Then lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

Else If chkbold.Checked =

  • 0
  XerXis said:
you can't use the short syntax:

if something then dosomething

in vb if you are using an elseif, it's elseif btw and not else if

the correct syntax is:

if something then

dosomething

elseif something then

dosomethingelse

else

dosomethingentirelyelse

end if

your code should be

Private Sub chkbold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbold.CheckedChanged
If chkbold.Checked Then 
lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)
Else 
lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)
End If
End Sub

This is correct.

  bookieass said:
I see error in your code

Private Sub chkbold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbold.CheckedChanged

If chkbold.Checked = True Then lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

Else If chkbold.Checked = True False lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

End If

End Sub

your code should look more like this

Private Sub chkbold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbold.CheckedChanged

If chkbold.Checked = True Then

lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

Else

If chkbold.Checked = False lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

End If

End If

End Sub

your code uses, two if loops so hence it should end both If statements........... hence

End If

End If

Hope it helps......... :)

And yes more importantly your tutor says crap things :(

And then you give out crap code... Why are you opening an if inside of an else and not giving it a condition. If chkbold.Checked = False lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

End If ?

He was using an elseif, which is poor style when you only have two choices, but you don't need a second if. Refer back to the first solution. He is correct.

  • 0
  Brian said:
This is correct.

And then you give out crap code... Why are you opening an if inside of an else and not giving it a condition. If chkbold.Checked = False lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

End If ?

He was using an elseif, which is poor style when you only have two choices, but you don't need a second if. Refer back to the first solution. He is correct.

oopss!!! did i write it two times.... well, that was not intended..... the point i wanted to highlight that the second condition had both true and false.... so i pointed in read.....

editing it right now... :)

  • 0

Save typing...

lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold) <- default text

If chkbold.Checked = True Then lblText.Font = New System.Drawing.Font("Tahoma, FontStyle.Bold)

End Sub

If you utilize a "then" within the "if" line, the "end if" is assumed when the line ends.

  • 0
  GreyWolfSC said:
lblText.Font = IIf(chkbold.Checked, _

New System.Drawing.Font("Tahoma, FontStyle.Bold), _

New System.Drawing.Font("Tahoma, FontStyle.Bold))

Even better. I just didn't want hime to freak out trying to follow along.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • OK? I am still good with my older iPad and more interested in upgrading my newer PC and laptop. I can still watch videos and read ebooks just fine on my tablet. There doesn't seem to be much to be gained from buying a new tablet beyond it's shinny. Maybe the cracked displays are piling up so people are looking to upgrade? LOL
    • iPhone 17 series rumored to be unveiled on September 9 by Hamid Ganji Every September holds special significance for Apple enthusiasts: it's when the company unveils its latest iPhone models. As the launch of the iPhone 17 series draws closer, speculation is mounting about the exact date Apple will unveil the new devices. The German website iPhone-Ticker might have the answer. The outlet claims internal information from German mobile phone providers shows that Apple will pull back the curtain on the iPhone 17 series on Tuesday, September 9. This means Apple's event for the latest iPhones is 35 days away. The September 9 timeframe for unveiling the iPhone 17 series actually aligns with last year's release date for the iPhone 16 series. For comparison, the iPhone 15 models were released on September 12, and the iPhone 14 debuted on September 7. Apple never releases phones on September 11, and the reason is quite apparent. Apple's release and preorder pattern for the iPhone 17 series also won't be different this year. After debuting the iPhones on Tuesday, September 9, preorders begin on Friday, September 12. Deliveries also start on September 19. The US, China, Europe, and Japan are the most important markets for Apple, so customers in these areas can expect to get their hands on the iPhone 17 series sooner than anybody else. But the iPhone 17 series might not debut alone at the upcoming September event. Apple Watch Series 11, the new Apple Watch Ultra, and the next-generation AirPods Pro are other products we expect to make a debut alongside the iPhone 17 models. All the previous rumors surrounding the iPhone 17 series converge on one key point: a significantly redesigned back, featuring changes such as a repositioned camera module and a relocated Apple logo. An alleged iPhone 17 Pro unit was recently spotted in public, seemingly confirming the rumors about its redesigned back panel. Apple is also rumored to replace the Plus variant in the iPhone 17 series with a so-called Air or Slim model.
    • A new detection method reveals a potentially habitable super-Earth. The breakthrough boosts the search for life-supporting planets. “Are we alone?” This enduring question has fascinated humanity for generations. The discovery in 1995 of the first exoplanet orbiting a star similar to our Sun marked a pivotal moment in addressing that mystery. Since then, exoplanet research has become a central focus of modern astronomy, offering critical insights into how planets form, evolve, and possibly support life. A major objective in this field is finding Earth-like life, which hinges on locating planets with Earth-like conditions in the habitable zones of Sun-like stars. In their search for Earth-like worlds, an international team led by the Yunnan Observatories of the Chinese Academy of Sciences (CAS), in collaboration with other institutions, made a significant breakthrough using a method called Transit Timing Variation (TTV). https://scitechdaily.com/earth-2-0-breakthrough-discovery-reveals-potentially-habitable-super-earth/
    • >The latest tvOS 26 even hints that the new Apple TV could come with a built-in camera. Sigh... No one is asking for that.
  • Recent Achievements

    • One Month Later
      Celilo earned a badge
      One Month Later
    • Dedicated
      Joey Solo earned a badge
      Dedicated
    • One Year In
      BergerMeister earned a badge
      One Year In
    • One Month Later
      ENAQUE earned a badge
      One Month Later
    • Week One Done
      ENAQUE earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      769
    2. 2
      ATLien_0
      201
    3. 3
      +FloatingFatMan
      165
    4. 4
      wakjak
      123
    5. 5
      Xenon
      117
  • Tell a friend

    Love Neowin? Tell a friend!