VB.NET Disable/Hide x button but WITHOUT disabling closing altogether - vb.net

I would like to remove/disable/hide the x button from my form but WITHOUT disabling closing altogether. So I want it so the program can't be closed by using the x button but ONLY by a certain key command (Through a menu item or similar). The only way I know of is to disable closing altogether which then means that the x button doesn't work but I cannot close it using the key command.
Are there any ways of disabling JUST the x button?
Thanks.

In the properties for the form, set the ControlBox property to False. Then, in the code, when you want to close the form, just call the form's Close method.
However, doing that will not stop the user from closing the window via standard OS methods (e.g. via the button on the task bar, via ALT+F4). In order to stop that, you would need to cancel the closing of the form in its FormClosing event. For instance:
Public Class Form1
Private _closeAllowed As Boolean = False
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
_closeAllowed = True
Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Not _closeAllowed Then
e.Cancel = True
End If
End Sub
End Class
However, even that won't stop the application from being terminated. For more thorough solutions, you may want to do some searches on best-practices for developing kiosk applications for Windows.

Related

Why does a form move trigger ResizeEnd?

I use the following code in my form:
Public Class Form1
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
MsgBox("Resized")
End Sub
End Class
When I move my form, it also seems to trigger MyBase.ResizeEnd. Why is that? A move of the panel doesn't change the size, so I don't understand why.
Why does a form move trigger ResizeEnd?
Because this is the documented behavior. From the documentation:
The ResizeEnd event is also generated after the user moves a form, typically by clicking and dragging on the caption bar.
If you want an event that doesn't get triggered when the form is moved, you should use either Resize or SizeChanged. The problem with those two events is that they will be triggered while the form is being resized by the user. To work around that, you may use it with both ResizeBegin and ResizeEnd with a couple of flags to signal when the user actually finishes resizing the form.
Here's a complete example:
Private _resizeBegin As Boolean
Private _sizeChanged As Boolean
Private Sub Form1_ResizeBegin(sender As Object, e As EventArgs) Handles MyBase.ResizeBegin
_resizeBegin = True
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
' This is to avoid registering this as a resize event if it was triggered
' by another action (e.g., when the form is first initialized).
If Not _resizeBegin Then Exit Sub
_sizeChanged = True
End Sub
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
_resizeBegin = False
If _sizeChanged Then
_sizeChanged = False
MessageBox.Show("The form has been resized.")
End If
End Sub
One thing to note is that both ResizeBegin and ResizeEnd are only triggered when the user manually resizes* the form. It does not, however, handle other situations like when the form is resized via code, when the form is maximized, or restored.
* or moves the form, which is the part that we're trying to avoid here.

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.

Single code for Tab Key functionality using Enter Key in Vb.Net for a Windows Application

I write this code for tab-like behavior when user presses ENTER on textboxes for every form, which works fine.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
However, I need to write the code once, perhaps as a sub or function in a module so that I do not have to write the same for every form. I have checked various forums including
Detecting Enter keypress on VB.NET and
Tab Key Functionality Using Enter Key in VB.Net
BUT all I get is either to write code for each textbox or for every individual form.
Has anyone tried out a single code for ALL or may be several selected forms of the application? If yes, please share with me. Writing for every form still works fine for me but i need to take advantage of OOP. Thanks
There are many ways to implement this, one of those is to create a custom textbox User Control
Adding a control to your project is very easy just right click in your project in the solution explorer ->Add->User Control
Give a name to your control ex. "tabedtextbox"
Add a textbox control to your User Control
Place the code in the keypress event of textbox1 of the UserControl
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
Compile once in order to update the whole project with the new user control.
Goto to your form and add your new User Control, you can locate it at the Toolbox panel.
Run your program and you will see the behavior of TAB when you press enter on each textbox.
If You wish to allow the user to press enter in a TextBox, instead of pressing a specific button,
you can use the acceptbutton property.
One good way to use it, is to change the property, on the Enter and Leave events of the TextBox. That you call Click event of the Button, when User press Enter inside the TextBox.
Try this code:
Private Sub tbProject_Enter(sender As Object, e As EventArgs) Handles tbProject.Enter
Me.AcceptButton = bSearch
End Sub
Private Sub tbProject_Leave(sender As Object, e As EventArgs) Handles tbProject.Leave
Me.AcceptButton = Nothing
End Sub
Private Sub bSearch_Click(sender As Object, e As EventArgs) Handles bSearch.Click
'.... actions to perfom
End Sub
Screen

Microsoft Office BeforeCloseHandler

I made a function in vb.net for desktop application that totally saved the documents before closed, I implemented it with the System.Threading.Timer.
I'm asking with favor on how you experienced it.
So, I made a timer to saved the documents before closed.
But, upon testing without the timer, just one call to the function or Handler, the documents are not saved before closed, because a dialog box appears asking to saved or not or cancel.
Is it really in need to have the timer? because we know a timer could delay the system even a single milliseconds.
I setup the timer with 100 milliseconds.
But, I don't want to use the timer, I want to saved the documents before closed for just one call of the function.
Is it really using the timer is the solution? or it can be done with just one call setup?
If it can be done in one function call, then I think I'm missing a code.
Thanks for your opinion and experienced.
You can handle the FormClosing event. In the handler you can perform actions before the form closes you can even cancel the closing event. Here's a an example:
Dim Saved As Boolean = False
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not Saved Then
Dim Result As DialogResult = MessageBox.Show("Do you want to save your text?", "Save Text?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Select Case Result
'Since pressing the No button will result in closing the form without
'saving the text we don't need to handle it here
Case Windows.Forms.DialogResult.Yes
SaveText_Click()
Case Windows.Forms.DialogResult.Cancel
e.Cancel = True
End Select
End If
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Saved = False
End Sub
'SaveText is a button for the user to manually save the text on demand.
'Since we don't need the sender or the eventargs, we can handle the click event without them.
'this way we can call this like any sub without having to worry about providing the right parameters.
Private Sub SaveText_Click() Handles SaveText.Click
'Add your procedure to save the text here
Saved = True
End Sub
If you don't want the option to close without saving just omit the messagebox and the select block and just call SaveText() and include the code to save your data in there.

Better way to kill a Form after the FormClosing event is overridden to hide rather than close?

I have a simple Windows Form that hosts property controls at runtime. When the user clicks Close [X] I want to keep the window and its contents alive rather than killing it by handling the FormClosing event, canceling the event and simply hiding the form.
That's fine but at close of the application I need to actually close the window. I implemented the below but it feels kludgey. Is there a simpler, more clever way to handle this situation? (The form's controller calls KillForm explicitly after it receives a closing event from the main window.)
Friend Class HostForm
Private _hideInsteadOfClosing As Boolean = True
Private Sub HostForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If _hideInsteadOfClosing Then
Me.Hide()
e.Cancel = True
End If
End Sub
Public Sub KillForm()
_hideInsteadOfClosing = False
Me.Close()
End Sub
End Class
You can examine the value of the CloseReason property of the event args object. If it is UserClosing, hide the form, otherwise close it. For all possible values of this property, check the CloseReason enumeration documentation.