VB.NET hide NotifiyIcon ContextMenuStrip when click somewhere else - vb.net

My Form include Notify icon with ContexMenu. ContextMenu is automatically closed when click on any item (AutoClose=True). But if I click anywhere else (away from context menu), ContextMenu remains visible. How to hide it in this case?
Found one answer, that hiding context menu when click somewhere else is standard behavior of context menu. In my case it doesnt work :).
I tried to use ContextMenuStrip Leave or LostFocus event to hide but this didnt work.
To show context menu:
Private Sub NotifyIcon1_DoubleClick(sender As Object, e As EventArgs)
Handles NotifyIcon1.DoubleClick
NotifyIcon1.ContextMenuStrip.Show(MousePosition)
End Sub
To hide context menu I tried:
Private Sub ContextMenuStrip_Leave(sender As Object, e As EventArgs)
Handles ContextMenuStrip.Leave
NotifyIcon1.ContextMenuStrip.Hide()
End Sub
Private Sub ContextMenuStrip_LostFocus(sender As Object, e As EventArgs)
Handles ContextMenuStrip.LostFocus
NotifyIcon1.ContextMenuStrip.Hide()
End Sub
UPDATE: This works:
Private Sub ContextMenuStrip_MouseLeave(sender As Object, e As EventArgs)
Handles ContextMenuStrip.MouseLeave
NotifyIcon1.ContextMenuStrip.Hide()
End Sub
Could be used as temp solution, but original question still open.

Related

How do I add items to a listbox inside a user control?

I have a user control (XListBox) with a listbox (ListBox1) and a button on it. The user control is on a form (Form1) which also has a button on it. The code is shown below. If click the button inside the user control the text 'Add Item from inside XListBox' appears in the Listbox no problem. If I click the button on the form however, the string 'Add item to XListBox from form' does not get added to the listbox although the Debug.Print does show it in the output window. It appears that I am missing something but I have no idea what. Any help would be appreciated. Thanks in advance.
Public Class Form1
Dim lstActions As New XListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstActions.AddItem("Add item to XListBox from form")
End Sub
End Class
Public Class XListBox ' user control
Private Sub XListBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add("Add Item from inside XListBox")
End Sub
Public Sub AddItem(sString As String)
ListBox1.Items.Add(sString)
Debug.Print(sString)
End Sub
End Class
Why do you have this in your code there?
Dim lstActions As New XListBox
Surely you added the user control to your form in the designer, like you would any other control. How do you usually access a control that you added to the form in the designer? Basically, you are ignoring the one you added in the designer, i.e. the one you can see, and you have created another one that you never add to the form, so you can't see it, and that's the one you're adding the items to. Don't. Get rid of that field and use the one you added in the designer.

How to make one-click buttons?

I have several buttons in which all of them have come codes. I want the user to only can make click the buttons once. How can I do it without having a long disable buttons code?
Dim oneClick As Integer
Private Sub btnPro_Click(sender As Object, e As EventArgs) Handles btnPro.Click
oneClick += 1
If oneClick = 1 Then
Dim ucP As New ucPro
fillMenu(ucP)
End If
End Sub
This is the code I come up with.
EDIT : The point is that I want to click a button once, but the others can be clicked. For example, I have a form with 6 buttons, every button has some codes. If I click Button1, it'll do such code only once, therefore if I click it again, it will not do that code, because it already did it. Then, if I click Button2, it'll do the code once. And what about if I click Button1 again? well, it'll be able to do that code because I clicked to another button.
Sorry for the way I explain myself. I hope you get it.
Keep track of last clicked button and you can use one handler for all involved buttons
Private _lastClickedButton As Button = Nothing
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button = DirectCast(sender, Button)
If button Is _lastClickedButton Then Exit Sub
_lastClickedButton = button
' Now based on button instance you can execute corresponding logic/method
End Sub
Based on your rather poor explanation, it sounds like what you actually mean is that you only want each button to act once in a row, i.e. once you click a button you need to click another button before the first button does anything again. In that case, the proper way to handle this is to not use Button controls.
Instead of using Button controls and handling their Click events, you should be using RadioButton controls and handling their CheckedChanged events. You can set the Appearance property to Button and they will look just like regular Buttons. When checked, they will appear depressed and that will indicate to the user that they can't be used again.
Here's an example of a form using such controls:
And here's what the appropriate code might look like:
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
'Do something.
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton1.Checked Then
'Do something else.
End If
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton1.Checked Then
'Do yet something else.
End If
End Sub
Here's what the form might look like when a RadioButton has been checked:
As you can see, it's nice and clear to the user which one they cannot use.
EDIT:
If you are really determined to use Button controls then you absolutely should be disabling them as that is the standard way that feedback has been provided to the user for decades. It's very simple to disable a Button if and only if it is the last one clicked. Here's an example:
Private Sub AllButtons_Click(sender As Object, e As EventArgs) Handles Button3.Click,
Button2.Click,
Button1.Click
For Each btn In Controls.OfType(Of Button)
btn.Enabled = (btn IsNot sender)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do something.
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Do something else.
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Do yet something else.
End Sub
Note that you can handle multiple events with a single method and multiple methods can handle a single event, both of which are demonstrated here. All four of those methods were generated automatically by the designer, so I just had to add the body code. Note that the first method assumes that all Button controls on the form will be treated this way. If that's not the case then would need a slight adjustment but nothing too major.

Vb.net : How can i Click 2 buttons at the same time?

i'm just a beginner here, please help me to solve my problem, and actually i didn't have a code yet for this.
i have a 2 flow out panel and have a buttons. the Menu button in the upper side and the Menu button in the bottom side. how can i click them at the same time?
No you cannot do that, but there is a way to achieve your problem,
Do something like this,
This is only a suggestion..
Private Sub btnMenuUpper_Click(sender As Object, e As EventArgs) Handles btnMenuUpper.Click
'Code Here
End Sub
That's the first button and that will be activate also the second button.
Private Sub btnMenuBottom_Click(sender As Object, e As EventArgs) Handles btnMenuBottom.Click
'Code Here
End Sub
And just call the btnMenuBottom to btnMenuUpper.
Private Sub btnMenuUpper_Click(sender As Object, e As EventArgs) Handles btnMenuUpper.Click
'Code
btnMenuBottom_Click(sender, e)
End Sub
This should work
Private Sub btn1_click(sender as object, e as EventArgs) handles btn1.click
btn2.performclick
end sub

How do i pass keyUp/keyDown events to panel in visual basic?

I have created a game in visual basic, and have decided to expand by adding multiple rooms on the game. Each room will be a panel that shows when the user enters a certain area. To start, I created a class that inherits panel properties, then added it to my main Form. I then added all the items to the panel controls. My issue is that i cannot control the player when it is in the panel controls. I have tried all sorts, but couldn't get it to work.
I did some reading and found that panel does not come with keyUp() and keyDown() events. However, I would like to know if there is a way round this.
Thanks in advance!
Code of mainRoom() class (my panel):
Public Class mainRoom
Inherits Panel
Sub New()
With Me
.SetBounds(0, 0, Form1.Width, Form1.Height)
.SendToBack()
End With
End Sub
Public Sub mainRoom_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
e.Graphics.FillRectangle(Brushes.DarkOrange, block)
End Sub
End Class
In main form class:
Sub setMap()
main = New mainRoom()
Me.Controls.Add(main)
End Sub
You can set the KeyPreview=True on the main form, and then handle the key up/down events from the form:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
'key down code.
e.Handled = True
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
'key up code.
e.Handled = True
End Sub
Note: if you don't want other controls' key up/down event handlers to fire after your form event handlers do, then set e.Handled=True as I have in the above example.
See MSDN for more info on KeyPreview.
BEWARE - when moving (cut/paste) existing controls via the IDE from a form into a new panel control, the event handlers are not carried across as they become children of the parent control (the new panel). So this happens:
Private Sub Button1_Click(sender As Object, e As EventArgs)
Add the event handlers back and don't spend an age googling why keypress events suddenly stop firing in panels ...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

mouseenter makes button visible but button click event then doesn't fire

I have made a button (btnImageRemove) get shown and hidden based on if the mouse is inside the component, but when i then click that button the event doesnt fire.
Private Sub btnImageRemove_Click(sender As Object, e As EventArgs) Handles btnImageRemove.Click
lblAvatar.Image = Nothing
lblAvatar.Tag = Nothing
End Sub
Private Sub lblAvatar_MouseLeave(sender As Object, e As EventArgs) Handles lblAvatar.MouseLeave
btnImageRemove.Visible = False
End Sub
Private Sub lblAvatar_MouseEnter(sender As Object, e As EventArgs) Handles lblAvatar.MouseEnter
btnImageRemove.Visible = True
btnImageRemove.BringToFront()
End Sub
Private Sub btnImageRemove_MouseClick(sender As Object, e As MouseEventArgs) Handles btnImageRemove.MouseClick
lblAvatar.Image = Nothing
lblAvatar.Tag = Nothing
End Sub
I have tried all the click and mouseclick events on the button, and all components that take its area, but i cannot get an event to fire for that button
Narrowed down cause a little more:
I have narrowed down to if i add anything to show / hide the button at runtime, during another event, the button doens't fire a click event, nor turn orange (default dotnetbar button action)