• 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

    • AMD 25.6.3 driver brings FSR 4 support for GTA V and Monster Hunter Wilds by Pulasthi Ariyasinghe AMD's driver team is busy this June. The company just released its third graphics driver of the month, and this one is focused on expanding support for FidelityFX Super Resolution 4 (FSR 4) upscaling tech. A handful of bug fixes are included in this release, too. AMD Software: Adrenalin Edition 25.6.3 optional driver is finally bringing FSR 4 support for Monster Hunter: Wilds, the Capcom-developed action RPG that released earlier this year. The title is well known for its performance issues, so the FSR update should help RX 9000 series owners get more frames without losing too much graphics fidelity. Next, the driver also delivers FSR 4 for Grand Theft Auto V Enhanced. The expanded and enhanced version of the title featuring ray tracing and other features only hit the PC platform a few months ago, and now, latest-generation Radeon graphics users can use the upscaling tech to improve their frame rates. AMD has also fixed an issue with FSR 4 in this release that made the technology not enable properly on a bunch of games. They include DragonKin: The Banished, Blades of Fire, RoadCraft, The Alters, Star Wars Outlaws, and S.T.A.L.K.E.R. 2: Heart of Chornobyl. A Black Myth: WuKong crash on the RX 7650 GRE GPU and a The Elder Scrolls IV: Oblivion Remastered texture corruption issue on the RX 9070 XT have also been resolved in this release. Here are the known issues that AMD is still working on: Stutter may be observed while playing games with some VR headsets at 80Hz or 90Hz refresh rate on some AMD Radeon™ Graphics Products, such as the Radeon™ RX 7000 series. Users experiencing this issue are recommended to change the refresh rate as a temporary workaround. Intermittent system or application crashes may be observed while playing Cyberpunk 2077 on some AMD Radeon™ Graphics Products, such as the Radeon™ RX 7000 series and Radeon™ RX 9000 series. Intermittent application crash or driver timeout may be observed while playing Monster Hunter Wilds with Radeon™ Anti-Lag and Instant Replay enabled. Stutter may be observed while playing Call of Duty: Warzone Season 03 ‘Verdansk’ map on some AMD Graphics Products. Stutter and lower-than-expected performance may be observed while playing 4K resolution YouTube videos in Chrome. Users experiencing this issue are recommended to play videos in full screen as a temporary workaround. The new AMD Software: Adrenalin Edition 25.6.3 optional update is now available for download from the AMD Software app as well as AMD's own changelog page for the driver.
    • So, basically, Windows 11 is the 'new Windows 7 'cause everyone's ignoring it? Guess my Windows 10 machine and I will just live happily ever after in our secure, familiar bubble... until four months from now. Then we'll figure it out. Probably.
    • Are you sure you are not mis-understanding a use case for Windows 11 where it fails? Your experience does not negate another user's dislike of something. I.E. don't be dismissive just because you haven't experienced something they have. That makes you look bad.
    • How about linking network storage and or external storage and then Explorer becomes a game of waiting and waiting and waiting. Windows 10 was not nearly this bad at that task.
    • People who don't know how to use a computer to its fullest genuinely fascinate me. Nope, not at all. Either you get it or you don't. Not getting it is not a good reason to speak out.
  • Recent Achievements

    • One Year In
      timothytoots earned a badge
      One Year In
    • One Month Later
      CHUNWEI earned a badge
      One Month Later
    • Week One Done
      TIGOSS earned a badge
      Week One Done
    • First Post
      henryj earned a badge
      First Post
    • First Post
      CarolynHelen earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      475
    2. 2
      +FloatingFatMan
      195
    3. 3
      ATLien_0
      163
    4. 4
      Xenon
      79
    5. 5
      Som
      76
  • Tell a friend

    Love Neowin? Tell a friend!