+InsaneNutter MVC Posted February 19, 2009 MVC Share Posted February 19, 2009 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 :) Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/ Share on other sites More sharing options...
0 XerXis Posted February 19, 2009 Share Posted February 19, 2009 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 Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590598002 Share on other sites More sharing options...
0 +InsaneNutter MVC Posted February 19, 2009 Author MVC Share Posted February 19, 2009 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 :) Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590598062 Share on other sites More sharing options...
0 XerXis Posted February 19, 2009 Share Posted February 19, 2009 your tutor is dead wrong, the vb syntax for conditional code hasn't changed since the old days of vb4 ;) Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590598072 Share on other sites More sharing options...
0 Bookieass Posted February 19, 2009 Share Posted February 19, 2009 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 = Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590598440 Share on other sites More sharing options...
0 Brian Veteran Posted February 19, 2009 Veteran Share Posted February 19, 2009 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 codePrivate 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. Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590598538 Share on other sites More sharing options...
0 Bookieass Posted February 19, 2009 Share Posted February 19, 2009 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... :) Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590599202 Share on other sites More sharing options...
0 Rohdekill Posted February 21, 2009 Share Posted February 21, 2009 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. Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590606560 Share on other sites More sharing options...
0 Eric Veteran Posted February 21, 2009 Veteran Share Posted February 21, 2009 lblText.Font = IIf(chkbold.Checked, _ New System.Drawing.Font("Tahoma, FontStyle.Bold), _ New System.Drawing.Font("Tahoma, FontStyle.Bold)) No ElseIf or EndIf :) Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590606572 Share on other sites More sharing options...
0 Rohdekill Posted February 21, 2009 Share Posted February 21, 2009 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. Link to comment https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/#findComment-590606580 Share on other sites More sharing options...
Question
+InsaneNutter MVC
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 :)
Link to comment
https://www.neowin.net/forum/topic/737698-vb-end-if-must-be-preceded-by-a-matching-if/Share on other sites
9 answers to this question
Recommended Posts