Enabled but unselectable menu item - vb.net

In WinForms application I need some "caption" in dynamically created ContextMenuStrip.
That caption is changable text composed in ContextMenuStrip_Opening event handler.
For that purpose I'm trying to use ToolStripControlHost with label in it, like this:
Dim labelItem As ToolStripControlHost = New ToolStripControlHost(New Label)
...
labelItem.BackColor = Color.Transparent
labelItem.ForeColor = Color.FromKnownColor(KnownColor.HotTrack)
labelItem.ToolTipText = "mytooltiptext"
mycontextmenu.Items.Add(labelItem)
That work almost OK, but...
I try to disable that "labelItem" to avoid clicks and keypresses and then it becomes gray automatically what is unwanted and also then tooltiptext is not showed.
If "labelItem" is enabled then color is OK, item cannot be selected with keys but can be with mouse and on mouse click it takes focus to itself. That is also unwanted but shows tooltiptext.
Is here a way in described situation to get "labelItem" to be enabled and able to show tooltiptext but be unselectable? In short... How to make an item like is described which would be in color (enabled) but would not accept mouse clicks and take a focus?

Don't disable the item. Set the disabled state image and then in the click event handler, just ignore the case for the item you don't want to be active.

Enabled and Disabled are predetermined definitions for for the appearance and behavior of a control. Disabled will always mean the control can't be clicked.
If you need alternate behavior, you'll need to write it yourself. I would suggest tracking two global variables in your form: whether or not your item should be active in a boolean and which object currently has focus in an object. Then use these to write your click event behavior. For example:
Public Class Form1
Public RunEvent As Boolean
Public HasFocus As Object
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RunEvent Then
'Do something
Else
HasFocus.Focus()
End If
End Sub
End Class

Related

Block focus/select in a ReadOnly textbox

Someone knows how to block the focus/select in a read-only textbox (ReadOnly = true), without using enabled = false?
Thanks!
Controls have a GotFocus Event. You can add an event handler for this event and give another control focus, for example by calling Select() on another control or by using SelectNextControl:
Private Sub MyTextBox_GotFocus(sender as Object, e as EventArgs) _
Handles MyTextBox.GotFocus
MyTextBox.Parent.SelectNextControl(MyTextBox, True, True, True, True)
End Sub
Alternately, you can create a custom control that inherits TextBox and set ControlStyles.Selectable to False.
Public Class NonSelectableTextBox Inherits TextBox
Public Sub New()
SetStyle(ControlStyles.Selectable, false)
End Sub
End Class
Setting ControlStyles.Selectable to false will make the TextBox mimic the behavior of other controls which have this bit set to False:
Label
Panel
GroupBox
PictureBox
ProgressBar
Splitter
LinkLabel (when there is no link present in the control)
I'm not sure I understand fully why you would want that. A read only text box allows selection to allow users to copy the text in there for other purposes. What I assume from your question is that you don't want the TextBox to accept input focus when a user is tabbing through controls, which I've seen to be a more common requirement.
You can achieve this via code:
TextBox1.TabStop = False
to ensure that tab doesn't direct focus to the readonly textbox. You can also achieve this in the designer using the same property as the screenshot shows.

Need To Close A Panel When Clicked Outside (Lost Focus)

I have an issue that I cannot seem to overcome.
In my application, I have a custom class that loads a form into a panel upon startup. Then when I click a button on my main form I show the panel as visible revealing the form to the user.
My problem is that I want to be able to hide the panel when a user clicks outside of it (back onto the main form).
so far I have tried Form_Deactivate, Form_Leave, Form_LostFocus, Panel_Leave and Panel_LostFocus events but nothing will seem to trigger an event consistently to hide the panel. The only thing that works is if the user clicks inside the form (on a listview control) once the form is visible and then clicks outside of the form.
Is there anyway I can ensure this event gets called everytime whether the user clicks the form or not?
So far my code looks something like:
Public Class cls_UserObjects
Private frm As frmUsers
Public pnl As Panel
Public Sub ShowUserPanel()
Try
frm = New frmUsers
frm.TopLevel = False
pnl.Controls.Add(frm)
frm.Show()
frm.Focus()
....
End Class
Then in my main form I call the code below to build the form into the panel:
class_Users.pnl = pnlUsers
class_Users.ShowUserPanel()
And pnlUsers.Visible = True to show it to the user
I just can't seem to close it. I understand that Panels don't support the LostFocus properly, however, I can't find away around this. Maybe it has something to do with how I am opening my form/panel but I was advised to use classes to open forms so I can have more control over the controls within my forms from outside calls.
Any help appreciated. Thanks
MouseLeave event works, the panel hides immediately once it leaves the panel.
Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Panel1.Visible = False
End Sub

vb.net How to get working scrollbars when moving nested form controls inside a parent panel

I have a container (a panel) which can contain multiple Form controls.
(Form.TopLevel = False)
When the user moves the Forms around I would like to display scrollbars when a form is out of the panel bounds.
When I register the Form.Move event, I can set the AutoScrollPosition. This works unless the user uses the scrollbars.
The problem is that the form.move event is also fired when the scrollbars are used. The result is that the scrollbars don't work. (And I have currently no idea how to find out whether the form has been moved by the mouse or by the scrollbar)
So the question is: How can I make the scrollbars of the panel appear/work when a form (or multiple) forms of the panel exceed the boundaries? I think there must be a simpler way than to handle the move event..
Note:
The panel is placed inside a Infragistics DockableControlPane. (Managed by an UltraDockManager)
(So there are multiple panels which contain at least one form per panel)
The reason is that the "panels" should appear as tabs, can be moved around using the DockManager and display their "sub" forms (Which also can be moved around on their panel).
Any idea would be great
It looks like the LocationChanged event could be used. Example with only one form:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Dim f As New Form
f.TopLevel = False
AddHandler f.LocationChanged, AddressOf Form_LocationChanged
Panel1.Controls.Add(f)
f.Show()
Call Form_LocationChanged(f, EventArgs.Empty)
End Sub
Private Sub Form_LocationChanged(sender As Object, e As EventArgs)
With DirectCast(sender, Form)
Panel1.AutoScrollMinSize = New Size(.Bounds.Right, .Bounds.Bottom)
End With
End Sub
Using an MDI form seems to be more appropriate though for something like this.

Disable mouse scroll wheel in combobox VB.NET

Does anyone know of a way to disable the mouse scroll wheel when a control such as a combobox or listbox has focus? For my purposes, combobox is all I need the answer for.
I have a combobox set to trigger a SQL query on SelectedIndexChanged, and accidentally scrolling the wheel while the combobox has focus causes about six SQL queries to fire off simultaneously.
I've found a mix response, put this code in the MouseWheel event:
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
That's all. You don't need to create a new class, if you have your project in an advanced state.
The ComboBox control doesn't let you easily override behavior of the MouseWheel event. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
Friend Class MyComboBox
Inherits ComboBox
Protected Overrides Sub OnMouseWheel(ByVal e As MouseEventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
End Class
Beware that this also disables the wheel in the dropdown list.
If you subclass the control it's possible (apologies for the C#)
public class NoScrollCombo : ComboBox
{
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
protected override void WndProc(ref Message m)
{
if (m.HWnd != this.Handle)
{
return;
}
if (m.Msg == 0x020A) // WM_MOUSEWHEEL
{
return;
}
base.WndProc(ref m);
}
}
One such option would be to add a handler to the comboBox, and within that comboBox, resolve the situation. I'm not sure how your code is set up, but I'm assuming if you knew when the event was happening, you could set up some kind of conditional to prevent the queries from happening
'''Insert this statement where your form loads
AddHandler comboBoxBeingWatched.MouseWheel, AddressOf buttonHandler
Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
'''Code to stop the event from happening
End Sub
In this way, you'd be able to maintain the user being able to scroll in the comboBox, but also be able to prevent the queries from happening
Combining all the answers on this thread, the best solution if you don't want to create a custom control is to handle the mousewheel event. The below will also allow the list to be scrolled if it is dropped down.
Assuming your combobox is called combobox1:
If Not ComboBox1.DroppedDown Then
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End If
I had the exact same issue, but found that simply changing the focus of the control after the query executed to another control such as the "Query" button itself worked better than perfect. It also allowed me to still scroll the control until the SelectedIndex actually changed and was only one line of code.
Just put this in the mousewheel event or in a single handler for all the controls this applies to, maybe call it wheelsnubber.
DirectCast(e, HandledMouseEventArgs).Handled = True

Visually remove/disable close button from title bar .NET

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