Disabling Close window option in windows 7 - vb.net

My application is in VB.net with 3.5 framework. I want to disable the close button ("[X]") on the main form of the application while doing some processing.
I have achieved the disabling by overriding the "ReadOnly Property CreateParams()" of the form. This is working fine, as both the control button on the form and close option on right click in the taskbar shows them as disabled.
This fulfills my needs on Windows XP but not on Windows 7. In Windows 7, right clicking on the application icon in the TaskBar shows a different menu...which has a new "Close window" option.
Close in the original menu still shows it as disabled (this old menu is hidden, but can be shown by holding Shift key and right click on the application icon in TaskBar). Now I need to disable this "Close window" option as well...and only for my application.
Is there a method for doing this programmatically?

Use:
Private Sub MyForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub
Edit:
Yes, you should check the e.CloseReason property. The sender's type is another parameter you can key off of to decide whether to cancel the close request or not.

Related

how to make the tabindex go to the close button in the form

I have been asked by my company to make my vb.net app friendly to those who use accessibility features, like keyboard, narrator, etc...
I have noticed that when using tab in the keyboard, the focus do not go to the close button in the form. Is there a way to make sure when some one is using the tab to go to the close button?
If you want to trigger the system dialog through the keyboard you can check for a TAB key press on the control in question and then use SendKeys to pop the dialog open.
Private Sub TextBox2_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox2.PreviewKeyDown
If e.KeyCode = 9 Then
SendKeys.Send("% ")
End If
End Sub

Permanently disable Windows Aero using vb.net

What I want to do is disable windows aero permanently for the user if he / she clicks a button.
Currently I have this code:
<System.RunTime.InteropServices.DLLImport("dwmapi.dll", PreserveSig:=False)>
Public Shared Sub DwmEnableComposition(bEnable As Boolean)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DwmEnableComposition(False)
End Sub
The current problem with this code is... It works, but whenever you exit your application Aero switches back on.
Is there a way to prevent this?
It sounds like in windows 8 aero transparency is disabled by default.
http://www.howtogeek.com/howto/windows-vista/disable-aero-on-windows-vista/
Also, from the link: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969510%28v=vs.85%29.aspx
Disabling DWM composition disables it for the entire desktop. DWM composition will be automatically enabled when all processes that have disabled composition have called DwmEnableComposition to enable it or have been terminated. The WM_DWMCOMPOSITIONCHANGED notification is sent whenever DWM composition is enabled or disabled.
So it sounds to me like you need to leave your program running. You could do something like minimize it to the task tray (as opposed to exiting it ). Who knows, maybe you can even just launch a separate executable that stays running and doesn't show up in the taskbar OR task tray. But you will have to leave the process running.
If you want Aero to be disabled "PERMANENTLY" for the user regardless of whether your app is running or not, then you'll need to identify where this setting lives in the registry and modify that setting.
If you meant that you only want to disable it for your application (meaning that the next time you launch the app, it will be either enabled or disabled based on how it was set when it was last closed), then you will need to store this in a user setting.
Create a new bool setting (user-scope) in the project settings and call it DWM. Then, in the button click event toggle it's value. Apply that value to the setting at runtime.
EDIT: The previous version would have resulted in an always false setting.
I've edited the If statements to an If-Else block.
<System.RunTime.InteropServices.DLLImport("dwmapi.dll", PreserveSig:=False)>
Public Shared Sub DwmEnableComposition(bEnable As Boolean)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If My.Settings.DWM = False THEN
My.Settings.DWM = True
Else
My.Settings.DWM = False
End If
My.Settings.Save()
DwmEnableComposition(My.Settings.DWM)
End Sub
In the onload or startup event of the form/app, run the code to set true or false
DwmEnableComposition(My.Settings.DWM)

Enable Tab Key in my Web Browser Control

I have a web browser control in a custom task pane control (User Control) and I open it as a sidebar as soon as my Outlook opens (I have created it as an Outlook Addin using Visual Studio 2013). The web browser control has a login form inside it and I would like to place focus on an input control in my web browser control as soon as Outlook opens. I have tried several tricks like placing focus on the custom user control and then placing focus on the input control after the form has loaded but it doesn't place focus on it. Also I have been trying to allow the use of Tab and Delete keys to work inside the web browser control so that I can tab to other controls and play with it like we would do it on a normal browser window. Kindly let me know how I can achieve this as I am out of ideas.
Cheers.
Try to use the Excel WebBrowser Control instead of the System.Windows.Forms WebBrowser; it handles special key forwarding as TAB, DEL, CTRL+V, etc.
For that change the WebBrowser contructor from
new System.Windows.Forms.WebBrowser();
to
new Microsoft.Office.Tools.Excel.Controls.WebBrowser();
You would need to add references to your project:
Project/Add Referernce/Extensions select
Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities
Doc: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx
You can only do that if you install a Windows hook (SetWindowsHookExW(WH_GETMESSAGE, YourHookProc, 0, GetCurrentThreadId()) and in the hook proc detect that messages are supposed to go to your browser, and redirect them accordingly.
It works partially this way but not 100%. First I had to set the TabStop property to True for my webbrowser control and then just set the focus once the document was loaded and that was it. The tab, delete, backspace keys all ran properly.
Private Sub CustomTaskPane_Load(sender As Object, e As EventArgs) Handles Me.Load
TestWebBrowser.Size = New Drawing.Size(Me.Width, Me.Height)
Me.Focus()
TestWebBrowser.Navigate(url)
WaitForPageLoad()
TestWebBrowser.TabIndex = 0
TestWebBrowser.TabStop = True
End Sub
Private Sub TestWebBrowser_DocumentCompleted(sender As Object, e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles TestWebBrowser.DocumentCompleted
' Now set the focus on the Web Browser Control
TestWebBrowser.Focus()
End Sub

close form when application loses focus

My form is displayed as TopMost on my application. The problem I have is that whenever I minimize my application or it loses focus, the form remains displaying. I want to be able to minimize my application or move to another and also hide or close my form. Once the application regains the focus, then unhide or open the form again.
Here is what I worked out on the form's closing event:
Private Sub frmNavigation_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Static Minimize As Boolean
If Minimize = True Then
e.Cancel = True
Me.Hide()
End If
End Sub
I tried using the same code in the applications WindowDeactivate event but nothing happens.
You do not show how you create the instance of your frmNavigation. I am assuming that you are using the Show Method, so just use the version of Show that you pass in the top level window. That will assign the owner of the form, it will then stay on top of your Main Form and minimize and restore with it also. If this doesn't work please show how you are creating and showing your form.
frmNavigation.Show(Me)
I was able to find an answer to the question. MSDN had an article on this very issue.
it can be found here: http://support.microsoft.com/kb/186908#appliesto

Winforms: Closing a program to system tray

'This is the event that is fired as the application is closing, whether it
'be from a close button in the application or from the user
'clicking the X in the upper right hand corner
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) Handles Form1.FormClosing
'What we will do here is trap the closing of the application and send the application
'to the system tray (or so it will appear, we will just make it invisible, re-showing
'it will be up to you and your notify icon)
'First minimize the form
Me.WindowState = FormWindowState.Minimized
'Now make it invisible (make it look like it went into the system tray)
Me.Visible = False
End Sub
Hello again Stackoverflow!
Im trying to make an application that when you press X, the program gets put in system tray. But i have like no idea how i'm suppost to do that, so did a search on google and found this code. Only VB2010 (what i use) doesn't like the fourth line. Can anybody give me a quick tutorial on this, and make this work in VB 2010?
By the way, i will most likely use VB only tonight, just to make one application. So im not thinking of learing the whole language.
It looks like you found code over here Dream.In.Code: Minimize To System Tray
Did you "keep" reading the rest of the messages?
You need to add:
e.Cancel = True
to your FormClosing event or else the program just ends. Also, you need to add the NotifyIcon component and a ContextMenuStrip.