Syed Shujaat Hussain

Hi everyone,

I am facing a very strange problem with Visual Studio 2005. Look at the following code:

Dim ActiveTextBox As TextBox = Nothing

Private Sub TextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBackPrint1.Enter

If CType(sender, TextBox) Is txtBackPrint1 Or CType(sender, TextBox) Is txtBackPrint2 Then

ActiveTextBox = sender

Else

ActiveTextBox = Nothing

End If

End Sub

To my wonders, this code executes BOTH the If and the Else part of the subroutine code. Can anyone help I have restarted the IDE but no change in behavior. I have replaced the "Or" with OrElse too, but that also produces the same results.

Thanks.



Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

JohnWein

What is the purpose of the IF statement You already know that the ActiveTextBox is txtBackPrint1. Turning Option Strict On will help you troubleshoot your code problems.



Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

Syed Shujaat Hussain

I have to keep track of the textbox that is currently in focus. You can see that ActiveTextBox is a class-level member, so it will be used somewhere in the code. Apart from the fact that I could have done it using the ActiveControl property of the form, the basic question remains as to "why should compiler ever execute both IF and ELSE parts of the group



Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

weirdbeardmt

I think what JohnWein means is that your event is only handling txtBox1.Enter - so you KNOW that's the textbox that is the sender.

I also agree about using Option Strict - you would notice some problems with your code.

As for the described behaviour - i.e., executing both parts of the statement, then I can't reproduce it (and never really expected to, either). Unless something particularly screwy is going on with your machine, I believe something else is afoot here.






Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

JohnWein

If . . . Else . . . End If works fine for me too. There are problems with the code.



Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

Syed Shujaat Hussain

Thanks all of you. Problem seems to be with that particular project. I copy/pasted the code in a new project and it works fine. And just to make things clear, the handler in my code was originally attached with multiple textboxes' Enter event. so i needed to resolve the sender name and act accordingly.





Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

spotty

My thought is that your problem is how you are using the IF statement, from looking at this I am assuming you are wanting to set the activetextbox variable if the sender is txtbackprint1 or txtbackprint2.

In which case the following should work

Code Snippet

Public Class Form1
Dim t As TextBox = Nothing


Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter
Dim x = CType(sender, TextBox)

If x.name = "Textbox1" Or x.name = "Textbox2" Then
t = sender
Else
t = Nothing
End If
End Sub
End Class

This will work as long as you are entering into a textbox called Textbox1 or Textbox2. It will run either the IF or the ELSE clause but not both.





Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

Swade

Are you absolutely sure it's running both If and Else statements, and not just an Edit/Continue trick. I've noticed issues where the Edit/Continue part of debugging acts up and it appears as though the debugger is stepping through parts it shouldn't (If and Else, Catch with no exception, etc...).

Change the code in the Else part to something that will cause an exception (ActiveTextBox = -1) and see what happens. If it runs both the If and the Else then it's an actual compiler error and not an Edit/Continue bug.






Re: Visual Basic General Visual Basic 2005 If-Else execution dilemma

Syed Shujaat Hussain

Thanks swades, as I said before, I have copy/pasted the code in a new project and it seems to be working fine. There is some problem with that specific project. And yes, I explicitly checked variable values using watches and data tips. It is executing both If and Else parts, assigning new values to the variable.

Thanks again.