I have a set of controls on my form and i want to enable/disable some of them. what is the best way?
Hint: I don't want to change all controls available in my form.
If your meaning from "enable/disable" is "preventing user from changing them", then you can do this:
THE_NAME_OF_CONTROL.Enabled = False 'Disable a control with THE_NAME_OF_CONTROL Name
And
THE_NAME_OF_CONTROL.Enabled = True 'Enable a control with THE_NAME_OF_CONTROL Name
Or you can put all of your controls in a "Group Box" and disable/enable whole group box.
If you want change controls outside of form, then create a public property or method which do it, instead of making controls public
Public Class MyForm
Inherits Form
Private _MyCheckBoxControl As CheckBox
Private _MyTextBoxControl As TextBox
Private _IsGroupOfControlsEnabled As Boolean
Public Property IsGroupOfControlsEnabled As Boolean
Get
Return _IsGroupOfControlsEnabled As Boolean
End Get
Set (value As Boolean)
_IsGroupOfControlsEnabled = value
'Update controls
_MyCheckBoxControl.Enabled = _IsGroupOfControlsEnabled
_MyTextBoxControl.Enabled = _IsGroupOfControlsEnabled
End Set
End 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.
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
I have a form which is set to TopMost. It is basically a gaming tool which runs in-game and only accepts key bindings. Now, I need to disable all click events in the form and making "Click-through" so that the users will not be interrupted when they accidentally click or right-click the form. If I will not use a click-through form, clicking events will focus the form which shows the taskbar and makes the game slow. I'll just use a key binding to enable/disable that click-through feature. How do you set this in a Windows form using VB.Net?
Override CreateParams() in your Form and set the WS_EX_TRANSPARENT flag:
Public Class Form1
Private Const WS_EX_TRANSPARENT As Integer = &H20
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
Return cp
End Get
End Property
End Class
This will give the "Click-through" functionality that you're looking for.
I have been asked to remove or disable the close button from our VB .NET 2005 MDI application. There are no native properties on a form that allow you to grey out the close button so the user cannot close it, and I do not remember seeing anything in the form class that will allow me to do this.
Is there perhaps an API call or some magical property to set or function to call in .NET 2005 or later to do this?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information:
I need to maintain the minimize/maximize functionality
I need to maintain the original title bar because the form's drawing methods are already very complex.
Based on the latest information you added to your question, skip to the end of my answer.
This is what you need to set to false: Form.ControlBox Property
BUT, you will lose the minimize and maximize buttons as well as the application menu (top left).
As an alternative, override OnClose and set Cancel to true (C# example):
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.ApplicationExitCall)
{
e.Cancel = true;
}
base.OnFormClosing(e);
}
If neither of these solutions are acceptable, and you must disable just the close button, you can go the pinvoke/createparams route:
How to disable close button from window form using .NET application
This is the VB version of jdm's code:
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim myCp As CreateParams = MyBase.CreateParams
myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
Return myCp
End Get
End Property
You can disable the close button and the close menu item in the system menu by changing the "class style" of the window. Add the following code to your form:
const int CS_NOCLOSE = 0x200;
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_NOCLOSE;
return cp;
}
}
This will not just stop the window from getting closed, but it will actually grey out the button. It is C# but I think it should be easy to translate it to VB.
Here is a simple way to remove the close button:
1. Select the Form
2. Now go to Properties.
3. Find ControlBox and change the value to False.
This will remove all the Control Buttons (e.g. Minimize, Maximize, Exit) and also the icon also that is in the to left corner before the title.
You should be able to override the OnClose event of the form. This is common when an application minimizes to the System Tray when "closed".
When you press the X box on the form.
The Form1_Closing is done first, then the Form1_Closed is done.
The e.Cancel = True in the Form1_Closing - prevents Form1_Closed from being called therefore, leaving your form still active.
Prevent to close the form, but hide it:
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.WindowState = FormWindowState.Minimized
Me.Visible=false
e.Cancel = True
End Sub
You can set the ControlBox property to False, but the whole title bar will be gone but the title itself...
What jmweb said here is OK as well. The X close button won't go if you cancel the event on form closing. But doing so, you need to release the processes the form needs and then closing the form.
Me.Dispose()
Me.Close()
This worked for me using Menu Strip.
Select (or click) the form itself
Click on events in the property window (the little lightning bolt icon).
Look for Form.Closing and double click it.
Then type: e.cancel=true
Making a Form without a Titlebar in Visual Basic.
Go to Form Properties and set both ControlBox and ShowIcon to false.
Then, clear all the text from the form's text property.
go to properties and select from bored style as none
Just select the required form and in the properties section, set controlBox = false
That just worked for me :)
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Beep()
e.Cancel = True
End Sub