Disable password TextBox Caps-Lock is On warning - vb.net

I am creating a login thing and I have this problem that every time I click on this "Show Password" Button and the Caps-Lock is activated, a Warning pops up and won't leave (at least I think it won't, which for the end-user would be even worse)
I would like to get rid of this warning completely.
Before redirecting my question to this question:
How to disable system's caps-lock notification on Textbox
I have already tried that.
Private Sub ShowPassword_MouseDown(sender As Object, e As MouseEventArgs) Handles ShowPassword.MouseDown
If txt_Password.Text <> "" Then
txt_Password.UseSystemPasswordChar = False
End If
End Sub
Private Sub ShowPassword_MouseUp(sender As Object, e As MouseEventArgs) Handles ShowPassword.MouseUp
If txt_Password.Text <> "" Then
txt_Password.UseSystemPasswordChar = True
End If
End Sub
The warning should disappear.
Hope this question can help other people other than just me.
Edit: Thanks Jimi. :D

When you do this:
[TextBox].UseSystemPasswordChar = [Bool Value]
the handle of the control is recreated each time (.Net source code)
The baloon tooltip will be shown for the new control handle, as soon as the TextBox.Text value changes: the tooltips will pile up.
A simple workaround - since disabling the warning may not be a good choice here - is to set the PasswordChar property to Char.MinValue or Nothing in the MouseDown handler of your Show Password Button, then set it back to the previous value on MouseUp. This won't recreate the handle (.Net Source code) and the balloon tooltip can be disabled pressing the Caps-Lock key.
Set UseSystemPasswordChar to False in the Designer.
In Form.Load or txt_Password.Enter: txt_Password.PasswordChar = ChrW(&H25CF)
Private Sub ShowPassword_MouseDown(sender As Object, e As MouseEventArgs) Handles ShowPassword.MouseDown
txt_Password.PasswordChar = Char.MinValue
End Sub
Private Sub ShowPassword_MouseUp(sender As Object, e As MouseEventArgs) Handles ShowPassword.MouseUp
txt_Password.PasswordChar = ChrW(&H25CF)
End Sub

Related

How do i detect Alt + Tab Key and then close the form?

I want to close my form when Alt + Tab is pressed. However, The form is somehow not registering the key combination.
i have tried to use the Me.KeyUp event to detect the keys
Private Sub Menu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyData
Case (Keys.Alt + Keys.Tab)
Close()
End Select
End Sub
How can it be done ?
Try this: As in my comment Alt+Tab would not work. So try something else like Alt+Q. Put this code under the Form's keydown event.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If (e.KeyCode = Keys.Q AndAlso e.Modifiers = Keys.Alt) Then
Me.Close()
End If
End Sub
You need to set the form's Set KeyPreview to True for it to respond to the key events, but since Alt + Tab is a special windows combination the key events will not be fired.
Try in this way
Dim myval As Integer 'this is global variable declare
Private Sub Menu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyData
Case (Keys.Alt)
myval = 1
Case (Keys.Tab)
If myval = 1 Then
Me.Close()
End If
End Select
End Sub
May be my syntax in not correct, please edit it if anything is notok.
I managed to work around this problem using this method-
That's usually the ESC key. One key press. A common one. Maybe you want to handle the Form's deactivate event, instead. – Jimi
I used the forms deactivate event to close it, since Alt+Tab is basically deactivating the form!
(A workaround because Alt+Tab as keystrokes were not detected by any means, as mentioned by
the last two answers.)
My final code looks like this -
Private Sub Menu_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
If loaded = true Then
Close()
End If
End Sub
I had to check if loaded (a boolean i declared to be set to true when everything is loaded)
to be true because my form opens one other form while loading, which inadvertently deactivated the main form and closed it!
Thank you so much for helping me everyone!!
Edit: I needed to clarify that the second form is always behind my main form so it is never really clicked on. I did this as a workaround to have aero blur in my application without a bug i experienced while applying it to the main form - it is too light, so text is unreadable. So i set another form to show aero blur and always stay behind my main form, whose opacity I have set to 0.88. This gives me a lot of control over the look of the blur.

VB.Net 2015 "Disable btn_MouseLeave on btn_MouseClick"

In VS 2015 Community, My form as a button.
Current Code is:
Private Sub CellStart_MouseHover(sender As Object, e As EventArgs) Handles CellStart.MouseHover
CellStart.BackgroundImage = My.Resources.graycell_hilight
End Sub
Private Sub CellStart_MouseLeave(sender As Object, e As EventArgs) Handles CellStart.MouseLeave
CellStart.BackgroundImage = My.Resources.graycell
End Sub
Private Sub CellStart_Click(sender As Object, e As EventArgs) Handles CellStart.Click
CellStart.BackgroundImage = My.Resources.graycell_select
End Sub
What I want is that if the button is in the post click state (Current BackgroundImage is graycell_select), for the MouseLeave and Mouse Hover to no longer be enabled. If the button becomes deselected I need them to be enabled again. I have tried if statements, assigning an integer +1 on click but nothing seems to override the leave and hover events. I am dreadfully new so any help or points to something similar is appreciated. Thanks in advance. This is in a winform.
Click your button and Find the GotFocus and LostFocus in the Event tab.
Click GotFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell_hilight
Click LostFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell
leave the click on what it has.
Dont forget to remove not just copy remove the codes in the MouseHover and MouseLeave

My Form is not registering Enter key presses

I am writing an educational program. I have a button which I want to be "clicked" when the user presses enter. I have set the form's accept button property to true and the KeyPreview property is also set to true. I have tried creating a custom Sub to replicate AcceptButton functionality, this does not work either, the sub runs on all keys other than when the enter key is pressed.
Private Sub TextBoxAnswer_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBoxAnswer.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Enter Then
CheckQ()
End If
End Sub
I have spent the last 3 days searching for an answer and have tried everything on google I could find. I have also just tried setting the textbox MultiLine property to true and the enter key doesn't even create a new line, just to clarify I have set the Multiline property back to false now as it should be.
The AcceptButton property should be set = to the button you want "clicked" when enter is pressed, not a boolean value. Me.AcceptButton = buttonSubmit
Did you add a handler for the form's Keydown event to go along with setting the KeyPreview property?
Private Sub form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles form1.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Enter Then
CheckQ()
e.Handled = true
End If
End Sub
Your code as you have it written for the textbox keydown event should work as well. I tried it in a test app and didn't have any issues. Do you hit a breakpoint if you place it on the CheckQ() method call?
Any one of these methods should accomplish what you are trying to do.

NumericUpDown.value is not saved in the User Settings

I have a NumericUpDown Control on a form. In the Application Settings / Properties Binding, for the value parameter, i can't select my USER setting called : Heures (Integer / User).
I tried to save the value by this way :
Private Sub NumericUpDownHeures_Leave(sender As System.Object, e As System.EventArgs) Handles NumericUpDownHeures.Leave
My.Settings.Heures = NumericUpDownHeures.Value
My.Settings.Save()
End Sub
But it's not saved.
No problem for other settings (String / User). But i don't understand why the settings (Integer / User) are not saved.
Please help, Thanks.
As you are putting "NumericUpDown1.Value" you have to set the value at My.Settings.Heures to decimal.
In Form1_Load add:
NumericUpDownHeures.Value = My.Settings.Heures
and add to the event listener for your button or other widget:
My.Settings.Heures = NumericUpDownHeures.Value
I would guess the issue is that the Leave event is not being fired as you expect it to be, especially if the user just clicks the up/down arrows. I suspect that it is only fired when the user actually clicks into the value area, then leaves. You could verify this by debugging to see if your code is ever hit or by showing a simple msgbox from that event.
I think that you will have better luck if you hook the LostFocus or ValueChanged event.
I want to add to this as well for anyone looking at this in the future.
Save your settings as shown already by putting
My.Settings.Heures = NumericUpDownHeures.Value into your ValueChanged event, and then doing reverse in the form load event.
The problem is, this value changed event fires before the form load when you first initialize, so it will keep defaulting to whatever value you have set in the designer because you're overwriting the setting value with the designer value.
To get around this, you need a private/public boolean at the top of your code that is only set to true once your form has loaded (set to true at the bottom of your form_load event), then you can add the condition to the ValueChanged event checking if the form is loaded yet or not. If it is, then change the setting value, if not, then don't.
An example:
Private IsFormLoaded As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
NumericUpDown1.Value = My.Settings.SavedNumValue
IsFormLoaded = True
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If IsFormLoaded = False Then Exit Sub
My.Settings.SavedNumValue = NumericUpDown1.Value
End Sub
OR
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If IsFormLoaded Then
My.Settings.SavedNumValue = NumericUpDown1.Value
End If
End Sub

Where do I control the behavior of the "X" close button in the upper right of a winform?

I'm venturing into making my VB.NET application a little better to use by making some of the forms modeless.
I think I've figured out how to use dlg.Show() and dlg.Hide() instead of calling dlg.ShowDialog(). I have an instance of my modeless dialog in my main application form:
Public theModelessDialog As New dlgModeless
To fire up the modeless dialog I call
theModelessDialog.Show()
and within the OK and Cancel button handlers in dlgModeless I have
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Hide()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Hide()
End Sub
and that seems to work fine.
The "X" button in the upper right is getting me, though. When I close the form with that button, then try to reopen the form, I get
ObjectDisposedException was unhandled. Cannot access a disposed object.
I feel like I'm most of the way there but I can't figure out how to do either of the following:
Hide that "X" button
Catch the event so I don't dispose of the object (just treat it like I hit Cancel)
Any ideas?
The class of this dialog is System.Windows.Forms.Form.
Catch the FormClosing event and, if the reason is UserClosing, set Cancel on the event to true.
Something like the following:
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _
Handles Form1.FormClosing
if e.CloseReason = CloseReason.UserClosing then
e.Cancel = true
Me.Hide()
end if
End Sub
Use Me.Close() to hide the form. To open it, use the following snippet:
If theModelessDialog.IsDisposed Then
theModelessDialog = New dlgModeless
End If
dlgModeless.Show()
If this is saving data, then you'll need to figure some way of storing it (perhaps in a static variable/s in the form). This is the proper way to do what you are trying to achieve though.
You'll also have to forgive me if my VB is off, it's been a while.
the formclosing event allows me to do a managed exit of the form so I have included a question to confirm to exit. I also have a form flag bterminate to force the cancel where i want it to and therefore not ask the question. Thanks your suggestion helped me as well :)
Dim msgboxresponse As MsgBoxResult
If e.CloseReason = CloseReason.UserClosing Then
If Not Me.bTerminate Then
msgboxresponse = MsgBox("Are you sure you want to cancel adding?", _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text)
If msgboxresponse <> MsgBoxResult.Yes Then
e.Cancel = True
Return
End If
End If
End If
#John was Hiding the form in his code and the answers above provide a solution to that case. Often, though, you are not planning to use the form again, so you really do want the form to be Disposed. All Close related activities will be in one place if you Handle the FormClosing event using Me.FormClosing by adding it to anyCancel/Close/Exit code that you already have. e.g. in #John's case:
Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Cancel_Button.Click, Me.FormClosing
....More code
Me.Dispose
End Sub
Note the use of the Me.Dispose instead of any existing Me.Close. If you leave the Me.Close you'll create an infinite loop. See this for the subtle differences between Close and Dispose.
Agree with handling the FormClosing event. Or change the properties on the form to hide the system X control.
I've tried everything and it didn't work
if you just want to close, without showing an messagebox, you will just need:
Private Sub FORM1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
>e.Cancel = False
>FORM2.Show() (if you want to show another form)
End Sub
Hope this helps you...!