• 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

    • Microsoft quietly burying a massive Windows 7 hardware driver feature as Windows 11 kills it by Sayan Sen Last month Microsoft announced a big update for Windows hardware drivers. The company declared that it was killing Windows Device metadata and the Windows Metadata and Internet Services (WMIS). For those wondering what it is, device metadata, as the name suggests, is the collection of additional, user-facing information that an original equipment manufacturer (OEM) provides about a hardware device. The feature was introduced with Windows 7 and can include stuff like icons, logos, descriptive texts, among other things, that help the Windows UI display details about such devices in places like Task Manager or Device Manager. This was a huge deal back in the day when Windows 7 debuted. The company called the feature "Device Stage" and Microsoft described it as a "new visual interface" that essentially worked like a "multi-function version of Autoplay where it displays all the applications, services, and information related to your device." It is often considered synonymous with the Windows "Devices and Printers" Control Panel applet. Neowin did an in-depth overview of the feature when it first launched which you can find in its dedicated article here. The Windows OS was able to obtain the device experience metadata from the WMIS, but now that the feature is being deprecated, Microsoft has begun removing information about Device Stage from its official support documents. Neowin noticed while browsing that a support article regarding automatic Windows hardware drivers was updated for Windows 11 and 10 sometime last year after the release of Windows 11 24H2. Previously, this article was geared for Windows 7 and was much longer. It also contained information about Device Stage, which, as mentioned above, was a headlining feature on Windows 7. In the said article, the section "If Windows can't find information about your device in Device Stage" has been deleted. You can find the archived version of the support page here. Aside from shortening the amount of information on the page, Microsoft has also added some more details on it. The company has now tried to define what the Microsoft Basic Display Adapter is, how updating drivers through Device Manager works, as well as a thorough and detailed troubleshooting section for common hardware driver errors on Windows, including one for USB-C. You can find all the new details on the updated support page here on Microsoft's website.
    • Sounds creepy to say the least. Don't need nor want AI having access to my history. They're claiming it to be an "offline" model now, but how can we guarantee they don't go behind our backs and change that?
    • Exactly! Without those fundamentals you've mentioned, Democracy is literally just Demonstration of Crazy, nothing to be proud of in such system.
    • Still I see almost no ads in mobile Edge unlike Chrome. So their browser is much better at blocking ads than Chrome and it is a fact. It even blocks ads on YouTube and you can add simple custom block filters. Also, Edge still support manifest v2 on desktop, so I'll look for another browser when I start seeing ads again.
    • Considering they consistently release a new version every year, I think it makes perfect sense. There is a minor pain point at the time of the change, so as long as they only change it once, we get over it and end up in a better place. The issue with MS' naming scheme is that they change their mind so frequently. 3.1, 95, 98, ME, 2000, XP, Vista, 7, 8, 8.1 10, there is no consistency. That was the issue, not the choice of naming the OS after the year. Minor correction: Windows 7 was 6.1. Even Windows 10 was version 6.4 at launch, but they retroactively changed it to 10.0 early on, along with an announcement that they would no longer track the kerel version and OS version separately...then proceeded to call Windows 11 version 10.0.2, so yeah, MS sucks at naming.
  • Recent Achievements

    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      481
    2. 2
      +FloatingFatMan
      263
    3. 3
      snowy owl
      238
    4. 4
      ATLien_0
      230
    5. 5
      Edouard
      176
  • Tell a friend

    Love Neowin? Tell a friend!