1

When I add this code in my project then the logic is not giving expected output

If e.KeyCode = Keys.Enter And cbmPassportCountry.Text.Trim() <> "" Then
    SendKeys.Send("{tab}")
Else
    If btnSave.Enabled = True Then
        btnSave.Focus()
    End If
End If

If I use this logic then my code gives expected output

Private Sub cbmPassportCountry_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbmPassportCountry.KeyDown
    'If e.KeyCode = Keys.Enter And cbmPassportCountry.Text.Trim() <> "" Then
    If e.KeyCode = Keys.Enter Then
        If btnSave.Enabled = True Then
            btnSave.Focus()
        Else
            SendKeys.Send("{tab}")
        End If
    End If
End Sub

But I want to implement that And Logic in my code which will work like my next logic

McMillan Cheng
  • 382
  • 1
  • 6
  • 20
Tazbirul Haque
  • 233
  • 1
  • 3
  • 15
  • 1
    you say it doesn't work what does that mean? – Mederic Nov 15 '17 at 06:00
  • @Mederic When i add that logic instead of nested if logic then my code is not working. But when Comment out my AND logic and use nested logic then this code works fine – Tazbirul Haque Nov 15 '17 at 06:01
  • you still didn't understand: doesn't work means nothing! **do you get an error? or a warning? or is the code not executed?** – Mederic Nov 15 '17 at 06:08
  • @Mederic My code is running but i am not getting the expected output such as i want to enable the tab if the condition meets otherwise the else condition will work. – Tazbirul Haque Nov 15 '17 at 06:10
  • Could you please explain how you want this to work/what result you're expecting? Currently it is not clear what exactly you are trying to do. – Visual Vincent Nov 15 '17 at 06:19
  • I'm not sure if I got your problem. Did you try to use 'AndAlso' instead of 'And'? If this doesn't work, split it into two If-commands. – muffi Nov 15 '17 at 06:19
  • @VisualVincent I want my And logic work like my second logic. Basically i want to know why my AND logic is not working Because i don't want to use multiple if logics in my code – Tazbirul Haque Nov 15 '17 at 06:21
  • @muffi : In this case `AndAlso` won't make a difference. It will only skip evaluating the right side if the left is `False`. Even if both sides are evaluated by `And`, if the left is `False` it won't continue anyway so the outcome of both will be the same. – Visual Vincent Nov 15 '17 at 06:22
  • @muffi I haven't used AndAlso in my code but when i split my code into multiple if conditions then my code works fine. But that adds a lots of line to my project – Tazbirul Haque Nov 15 '17 at 06:23
  • @Visual Vincent if `AndAlso` makes no difference in this case, why is your answer exactly this and Tazbirul's code is working? – muffi Nov 15 '17 at 06:53
  • @muffi : Because I'm using nested `If`s. The reason I use `AndAlso` is because I always use that in my `If`-statements. – Visual Vincent Nov 15 '17 at 07:04
  • @Visual Vincent I don't catch so far why your answer is correct and my answer not - both using `AndAlso`. – muffi Nov 15 '17 at 07:07
  • @muffi : Because switching to `AndAlso` in his initial code doesn't make a difference. Using nested `If`s is the solution. I could've used `And` in my code as well, but that would've been unnecessary. – Visual Vincent Nov 15 '17 at 07:11
  • @muffi : The only reason I used `AndAlso` is because I consider it bad practice to use `And`. It only affects speed in this case, not the outcome. – Visual Vincent Nov 15 '17 at 07:17
  • 1
    @Visual Vincent 100% agree to your last comment. – muffi Nov 15 '17 at 07:18

1 Answers1

2

If you want your first block to work like the second, but checking that cbmPassportCountry.Text isn't empty, why don't you just add that to your second statement?

If e.KeyCode = Keys.Enter AndAlso cbmPassportCountry.Text.Trim() <> "" Then
    If btnSave.Enabled = True Then
        btnSave.Focus()
    Else
        SendKeys.Send("{tab}")
    End If
End If

You cannot put this in one If-statement without having to make additional, unnecessary checks.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • 1
    Yap. now i am getting the expected output. Thanks a lot for your help. I was totally unaware about this AndAlso keyword. I was using And in my logic and that was not giving the expecte doutput but AndAlso is worrking fine – Tazbirul Haque Nov 15 '17 at 06:29
  • 3
    @TazbirulHaque : `AndAlso` doesn't make a difference in this case. It is better to use it, which is why I switched to it, but it doesn't affect the outcome. What changed is that you **must** use nested `If`s, otherwise you'd have to check `e.KeyCode` **twice** which is completely unnecessary. – Visual Vincent Nov 15 '17 at 06:32
  • I am new in vb.net that's why i am not familiar with this logics.Thanks again for your cordial help. – Tazbirul Haque Nov 15 '17 at 06:33
  • @TazbirulHaque : No problem. I didn't have the time to post the link earlier, see this for more info on the difference between `AndAlso` and `And`: https://stackoverflow.com/questions/8409467/orelse-and-or-and-andalso-and-and-when-to-use – Visual Vincent Nov 15 '17 at 06:46
  • You are really awesome. I am really grateful to you. I hope that you will lead me in future with your awesome solutions.But i don't know is it possible to follow you or any other way to communicate with you. If there is any possible way then please let me know!! Thank you once again. – Tazbirul Haque Nov 15 '17 at 06:56