Disable Close Button without Disabling Icon [duplicate] - vb.net

This question already has answers here:
Visually remove/disable close button from title bar .NET
(13 answers)
Closed 8 years ago.
my problem is so simple but I can't seem to get it solved.
I just want to remove the Close Button from my form and don't remove the icon.
I used ControlBox = false but it removes the form's icon as well, I just want to keep it.
Is there anyway I can do it either by code or properties?

Add it under Public Class Form ... :
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim Param As CreateParams = MyBase.CreateParams
Param.ClassStyle = Param.ClassStyle Or &H200
Return Param
End Get
End Property
It should work perfectly!

If you want something full of capabilities, then you could use my SystemMenuManager By Elektro Class.
Just add all the code into a single Class and use it like in the example below:
Public Class Form1
Dim SystemMenu As New SystemMenuManager(Me)
Private Shadows Sub Load() Handles MyBase.Load
' Disables the 'Close' button and 'Close' menu-item.
SystemMenu.SetItemState(SystemMenuManager.Item.Close,
SystemMenuManager.ItemState.Disabled)
End Sub
End Class

Related

DatetimePicker and other Controls in MenuStrip

I know I can add a DateTimePicker to my MenuStrip with the following lines
Dim dp = New ToolStripControlHost(New DateTimePicker)
MenuStrip1.Items.Add(dp)
But I can't figure out how to add a DateTimePicker to the MenuStrip at designtime. What's the trick behind it? I have been trying and searching for like an hour and I am about to give up even though I know there has to be a way!
TL;DR
How do I add a DateTimePicker to my MenuStrip at design-time?
Alternatively we can add it to a ToolStrip instead.
You are close to a solution in using the ToolStripControlHost, but you will need to derive from that class as shown in the linked-to example. The frustrating thing with that example is that it does not decorate the derived class with the System.Windows.Forms.Design.ToolStripItemDesignerAvailabilityAttribute to make it available on the design surface.
The following is a minimalist implementation to get a working example. You may need to override the automatic sizing to suit your needs/wants for the control. The implementation overrides the Text property to prevent designer from assigning invalid text to the underlying DateTimerPicker control.
<System.Windows.Forms.Design.ToolStripItemDesignerAvailability(
System.Windows.Forms.Design.ToolStripItemDesignerAvailability.ToolStrip _
Or System.Windows.Forms.Design.ToolStripItemDesignerAvailability.StatusStrip _
Or System.Windows.Forms.Design.ToolStripItemDesignerAvailability.MenuStrip)> _
Public Class TSDatePicker : Inherits ToolStripControlHost
Public Sub New()
MyBase.New(New System.Windows.Forms.DateTimePicker())
End Sub
Public ReadOnly Property ExposedControl() As DateTimePicker
Get
Return CType(Control, DateTimePicker)
End Get
End Property
<Browsable(False), EditorBrowsable(EditorBrowsableState.Advanced), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Overrides Property Text As String
Get
Return ExposedControl.Text
End Get
Set(value As String)
' verify valid date
Dim dt As DateTime
If DateTime.TryParse(value, dt) Then
ExposedControl.Text = value
End If
End Set
End Property
End Class
Was going to add as a comment but I trust it justifies an answer.
The only way I have succeeded in this is to add one at design time (to the form) and set Visible to False and use the menu item to set Visible to True (may also need to set the position and/or bring it to the front).
You do need to manually handle setting Visible to False again.

Disabling the AxWebbrowser Context Menu VB.NET [duplicate]

This question already has an answer here:
How do you override the ContextMenu that appears when right clicking on winforms WebBrowser Control?
(1 answer)
Closed 6 years ago.
I'm using Axwebbrowser to display HTML page in my VB.NET application and i would like to know how disable its context menu ?
I use AxWebbrowser more than the original Webbrowser component because it handle the NewWindows 2 event that help me getting the popup url when a link is open in a new tab for example.
So i can't use the Webbrowser1.ContextMenuEnabled = False
Thanks for your answers
A simple method is to trap the WM_RBUTTONDOWN and WM_RBUTTONDBLCLK messages at the application level. You can add a message filter to your application using the statement Application.AddMessageFilter(instance of IMessageFilter). In the example below, the ImessageFilter interferace is implemented by the form containing the AXWebrowser control. As the filter is application wide, it is added/removed when the form activates/deactivates.
Public Class Form1
Implements IMessageFilter
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
Const WM_RBUTTONDOWN As Int32 = &H204
Const WM_RBUTTONDBLCLK As Int32 = &H206
' check that the message is WM_RBUTTONDOWN Or WM_RBUTTONDBLCLK
' verify AxWebBrowser1 is not null
' verify the target is AxWebBrowser1
Return (m.Msg = WM_RBUTTONDOWN OrElse m.Msg = WM_RBUTTONDBLCLK) AndAlso (AxWebBrowser1 IsNot Nothing) AndAlso (AxWebBrowser1 Is Control.FromChildHandle(m.HWnd))
End Function
Protected Overrides Sub OnActivated(e As EventArgs)
MyBase.OnActivated(e)
' Filter is application wide, activate filter with form
Application.AddMessageFilter(Me)
End Sub
Protected Overrides Sub OnDeactivate(e As EventArgs)
MyBase.OnDeactivate(e)
' Filter is application wide, remove when form not active
Application.RemoveMessageFilter(Me)
End Sub
End Class

Prevent focus on Control

I'm working on a project where I have an input with validation.
Next to it there are a set of buttons which you can press. When you focus the input the buttons will be shown. But when you click on 1 of the buttons the focus of the input is gone and the validation will start and the buttons will be hidden.
What I want is that the input focus is not lost, at any time.
Is it possible to not activate/focus the buttons when you click on them or is it possible to force focus on the input without losing it?
I have tried this on the Control, but seems to be only working on a form:
Const WS_EX_NOACTIVATE As Integer = &H8000000
Const WS_EX_TOOLWINDOW As Integer = &H80
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim ret As CreateParams = MyBase.CreateParams
ret.ExStyle = ret.ExStyle Or WS_EX_NOACTIVATE Or WS_EX_TOOLWINDOW
Return ret
End Get
End Property
You can create your custom button and make it non-selectable:
Public Class MyButton
Inherits Button
Public Sub New()
SetStyle(ControlStyles.Selectable, False)
End Sub
End Class
If you don't want to create custom control, set CausesValidation = False for your normal button and it will get focus, but doesn't cause validation.
Set the TabStop property of the buttons to false.
By setting this to false you'll stop them receiving focus when the user tabs, but they'll still be clickable.

Alt + Tab is not showing the forms when Form.Owner is set

I have an application (VB.NET) with the name MainForm and other child forms without using MDI Container. The child forms are based on assign to the MainForm with Me.Owner = MainForm
When I press Alt + Tab, for switching between these form, the Windows is showing the MainForm only unless I remove Me.Owner = Nothing it's working as expected again.
I tried Call SetWindowLong on Onload function but not luck. I am still looking for the solution.
EDIT
Actually it's easy for reproduced, I created very simple project.
Simple Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Form2 As New Form2
Form2.Owner = Me ' Alt+Tab only Show Form1, not showing Form2.
Form2.Show()
End Sub
Disable Owner property is working fine again.
Please check my teamview recording. Actually it's original form without change anything.
#Royce Your solution still not working, it's throw Win32Exception from my side.
Try to change the ShowInTaskbar Property to True.
Ran your example on my machine (Windows 10) and both windows showed up in my Alt-Tab menu (screenshot).
In your original program, make sure that the FormBorderStyle property of either window is not set to one of the two ToolWindow options and that ShowInTaskbar is True.
Otherwise, you might try changing the CreateParams of your second form to exclude WS_EX_TOOLWINDOW extended style bit by doing something similar to this:
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle And (Not &H80)
Return cp
End Get
End Property

Unable To Get Public Property To Work In Custom Class

I have created a custom Class to which mimics a Textbox control but allows me to embed a button within this control. Everything works fine normally.
However my problem is that I am trying to setup a Public Property that allows me to turn this button visible or not (well create it actually), but I cannot seem to work out why my property never gets set to True - meaning that since I've implemented this piece of code, my button is no longer drawn anymore.
I have the following code:
Public Class CustomTextbox
Inherits TextBox
Private m_EnableSearch As Boolean
Private search_btn As Button
Public Sub New()
' This call is required by the designer.
Initialize()
End Sub
Private Sub Initialize()
search_btn = Nothing
'Draw the buttons
If EnableSearchButton = True Then CreateSearchButton() '<== This never equals True
End Sub
Private Sub CreateSearchButton()
search_btn = New Button()
...
...
End Sub
<Category("Appearance")> _
<Description("Enables the search button")> _
Public Property EnableSearchButton() As Boolean
Get
Return Me.m_EnableSearch
End Get
Set(value As Boolean)
Me.m_EnableSearch = value
Me.Invalidate()
End Set
End Property
End Class
If I take out the check for EnableSeachButton = True and just change it to CreateSearchButton, the button appears as expected. However, even though I can see and change the EnableSearchButton property in the design view, it never seems to equal true when I step through the code.
Any help appreciated, thanks!
Your question is a little confusing. Does EnableSearchButton actually ADD a button to the control or is it meant to toggle the Enabled state of said button? To get the button to respond to the Property and toggle the enabled state, you need to set the button's state:
Public Property EnableSearchButton() As Boolean
Get
Return Me.m_EnableSearch
End Get
Set(value As Boolean)
Me.m_EnableSearch = value
' to create the button when the prop is set:
If Value AndAlso search_btn IsNot Nothing Then
' create button
End If
If Value = False Then
' destroy button
End If
Me.Invalidate()
End Set
End Property
Since you appear to be creating the button at runtime, you will have to add a check for cases when it is nothing. Since the constructor only runs once and when created, there is no need to test the state of EnableSearchButton, just create it always.
If your Enabled property actually means something like AddButton to this TB, then just pass a Boolean in the ctor:
Public Sub New(boolAddButton As Boolean)
If boolAddButton Then CreateSearchButton()
End Sub
The way you have it, the backing field m_EnableSearch will always be true when the control is created. Any prop setting you do in the IDE happens after the control is created which is the only place you have it set up to create the button.