• 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

    • Cjam 2.0 by Razvan Serea Cjam is a lightweight and fast MP3 editor for Windows that lets you cut, join, and edit MP3 files without re-encoding. This means your audio quality remains untouched, and edits happen instantly. Cjam is ideal for quick, lossless edits—whether you're trimming music, combining tracks, or preparing audio for learning tools or podcasts. It features batch processing, scripting support, cue and playlist file handling, and a simple interface. Cjam is perfect for anyone who needs efficient MP3 editing without the complexity of full audio suites. Cjam requires a PC running Windows 10 or later and Microsoft .NET 6.0 or later. Key features for Cjam: No Re-encoding: Edit MP3 files without losing quality. Cut and Join MP3: Easily cut, trim, and combine MP3 tracks. Batch Processing: Edit multiple files at once for faster workflows. Scriptable Interface: Automate tasks with a custom command language. Cue and Playlist Support: Handle CUE and playlist files for seamless audio management. Fast and Lightweight: Quick processing with minimal system resources. Lossless Audio Editing: Ensure your edits don't affect audio quality. Simple User Interface: Clean, intuitive design for easy navigation. File Format Support: Works with MP3, Cjam-specific file formats (CJAMC, CJAMJ, CJAM). Cjam 2.0.0.0 fixes: Fixed a bug where the folder list was not updating on the vmp3 screen Download: Cjam 2.0 | 1.3 MB (Freeware) Links: Cjam Home Page | Cjam Manual | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Honestly, i either don't care, mind, use, or had any problem with those. 11 is really good for me, has a lot of little refinements, looks great, if i wanted to tweak/fix it's behavior i would with a range of tools, and wouldn't come back to the 2010's looking Win 10 even at gunpoint.
    • You dont want a expensive hp laptop either managed to get 3 new zbooks and they all died on me.
    • Nothing inherently wrong, but why buy 2021 tech in 2025? Assuming you were aiming for 6-10 years of usage, better to buy something current.
    • I have begun using Windows 11 native passkey feature. i have four passkeys set up so far. Three work fine, One has never worked. I have removed it and recreated it several times, still no luck. Anyway, on the websites in which it works, it works geat. No more two 2FA.
  • Recent Achievements

    • Dedicated
      Cole Multipass earned a badge
      Dedicated
    • Week One Done
      Alexander 001 earned a badge
      Week One Done
    • Week One Done
      icecreamconesleeves earned a badge
      Week One Done
    • One Year In
      PAC0 earned a badge
      One Year In
    • One Month Later
      PAC0 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      570
    2. 2
      +FloatingFatMan
      187
    3. 3
      ATLien_0
      185
    4. 4
      Skyfrog
      113
    5. 5
      Som
      109
  • Tell a friend

    Love Neowin? Tell a friend!