Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is that my code isn't working at all. I'm trying to create a toggle button for a Hide/Show Password button on VB.NET.

What I have tried:

VB.NET
Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tokenBotVar As Integer
        tokenBotVar = tokenBotVar + 1
        If tokenBotVar = 0 Then
            tokenBot.UseSystemPasswordChar = False
        End If
        If tokenBotVar = 1 Then
            tokenBot.UseSystemPasswordChar = True
        End If
        If tokenBotVar = 2 Then
            tokenBotVar = 0
        End If
    End Sub
End Class
Posted
Updated 7-Jan-21 6:47am

Use a Boolean Data Type - Visual Basic | Microsoft Docs[^]. You then just need to test whether it is True or False, and flip it to the other state. Also learn to use If ... Else, not just If ... End If.

VB
Public Class Form2
    Dim tokenBotVar As Boolean = True
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If tokenBotVar = True Then
            tokenBot.UseSystemPasswordChar = True
        Else
            tokenBot.UseSystemPasswordChar = False
        End If
        tokenBotVar = Not tokenBotVar
    End Sub
End Class
 
Share this answer
 
v2
Comments
Member 15040038 7-Jan-21 12:21pm    
Thank you for your support. But it's still not working.
Richard MacCutchan 7-Jan-21 12:47pm    
See my updated solution.

And please, do not say "it's not working". That means absolutely nothing to anyone apart from you. If you have a problem then show the code and explain exactly what is going wrong.
Member 15040038 8-Jan-21 7:26am    
It worked! Thank you :)
You have defined tokenBotChar as a local variable. The value of the variable will not be persisted between calls to your method.

If you want it to persist between calls, make it a class-level field:
VB.NET
Public Class Form2
    Private tokenBotVar As Integer
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        tokenBotVar = tokenBotVar + 1
        If tokenBotVar = 0 Then
            tokenBot.UseSystemPasswordChar = False
        End If
        If tokenBotVar = 1 Then
            tokenBot.UseSystemPasswordChar = True
        End If
        If tokenBotVar = 2 Then
            tokenBotVar = 0
        End If
    End Sub
End Class
Alternatively, make the variable Static - this will make it a private field behind the scenes, with a compiler-generated name to ensure it does not conflict with static variables from other methods in the same class:
VB.NET
Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static tokenBotVar As Integer = 0
        tokenBotVar = tokenBotVar + 1
        If tokenBotVar = 0 Then
            tokenBot.UseSystemPasswordChar = False
        End If
        If tokenBotVar = 1 Then
            tokenBot.UseSystemPasswordChar = True
        End If
        If tokenBotVar = 2 Then
            tokenBotVar = 0
        End If
    End Sub
End Class
Static - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 15040038 7-Jan-21 12:21pm    
Thank you for your support. But it's still not working.
Richard Deeming 7-Jan-21 12:48pm    
"Not working" tells us precisely nothing.

Update your question to show us what you have tried, and explain exactly what the problem is.
Quote:
What am I doing wrong?

If you want to learn how to fix your code by yourself, the first step is to understand what your code is really doing, the tool of choice is the debugger.
VB
Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tokenBotVar As Integer ' This recreate every time the variable with value 0
        tokenBotVar = tokenBotVar + 1
        If tokenBotVar = 0 Then
            tokenBot.UseSystemPasswordChar = False
        End If
        If tokenBotVar = 1 Then
            tokenBot.UseSystemPasswordChar = True
        End If
        If tokenBotVar = 2 Then
            tokenBotVar = 0
        End If
    End Sub
End Class

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900