ContextMenu on Multiple Buttons to change text of button being right clicked - vb.net

I have multiple buttons on my VB.net form. I have a single ContextMenuStrip and I have assigned it to all the buttons. I can't seem to figure out how to set the button text to the menu selection based on which button I right clicked on.
Any ideas? Thanks in advance.
My code:
Private Sub PuTTYToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PuTTYToolStripMenuItem.Click
Dim btn As Button = CType(sender, Button)
btn.Text = "PuTTY"
End Sub

Related

VB.Net 2015 "Disable btn_MouseLeave on btn_MouseClick"

In VS 2015 Community, My form as a button.
Current Code is:
Private Sub CellStart_MouseHover(sender As Object, e As EventArgs) Handles CellStart.MouseHover
CellStart.BackgroundImage = My.Resources.graycell_hilight
End Sub
Private Sub CellStart_MouseLeave(sender As Object, e As EventArgs) Handles CellStart.MouseLeave
CellStart.BackgroundImage = My.Resources.graycell
End Sub
Private Sub CellStart_Click(sender As Object, e As EventArgs) Handles CellStart.Click
CellStart.BackgroundImage = My.Resources.graycell_select
End Sub
What I want is that if the button is in the post click state (Current BackgroundImage is graycell_select), for the MouseLeave and Mouse Hover to no longer be enabled. If the button becomes deselected I need them to be enabled again. I have tried if statements, assigning an integer +1 on click but nothing seems to override the leave and hover events. I am dreadfully new so any help or points to something similar is appreciated. Thanks in advance. This is in a winform.
Click your button and Find the GotFocus and LostFocus in the Event tab.
Click GotFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell_hilight
Click LostFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell
leave the click on what it has.
Dont forget to remove not just copy remove the codes in the MouseHover and MouseLeave

loop button to check which was clicked

In VB.net form, I have 20 buttons. They are named from btnLoc1 ~ btnLoc20. I do not want to code each button click event.
How to loop through each button to check which was clicked?
Do I need to implement timer tick to listen for button click event?
You can create a single event handler for all the Buttons. Select all the buttons in the designer, open the Properties window, click the Events button and then double-click the Click event. That will generate a Click event handler, just like when you double-click a Button in the designer, except this one will have multiple items in the Handles clause. You can then use the sender parameter to acces the Button that was clicked, e.g.
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click,
Button2.Click,
Button3.Click
Dim btn = DirectCast(sender, Button)
'Use btn here.
End Sub
The question then is, what do you want to do with that Button? If you want to do something different for each Button then you really should be creating separate event handlers. Alternatively, you might have a list of data and you want to use the item in that list that corresponds to the Button that was clicked. There are numerous ways to do that. One is to put the data in the Tag property of the Button itself and retrieve it from there. Another is to use concurrent indexes, e.g.
Dim buttons = Controls.OfType(Of Button)().ToArray()
Dim data = {"First", "Second", "Third"}
MessageBox.Show(data(buttons.IndexOf(btn)))
Obviously you need to ensure that the Button array and the data array do line up.
For being noticed if button was clicked - eventhandler is best choice.
But you can create only one eventhandler for all buttons
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = DirectCast(sender, Button)
MessageBox($"Button '{button.Name}' was clicked")
End Sub
Then in constructor
Public Sub New()
InitializeComponennts()
AddHandler Button1.Click, AddressOf Me.Button_Click
AddHandler Button2.Click, AddressOf Me.Button_Click
AddHandler Button3.Click, AddressOf Me.Button_Click
' and so on
End Sub
If you want to get information about how much each button was clicked, simply create dictionary and add click amount in one eventhandler
Private ButtonsClickAmount As New Dictionary(Of String, Integer)()
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = DirectCast(sender, Button)
If ButtonsClickAmount.ContainKey(button.Name) = True Then
ButtonsClickAmount(button.Name) += 1
Else
ButtonsClickAmount.Add(button.Name, 1)
End If
End Sub

Using the same event handler for multiple buttons

I have 15 buttons created through design.
I want them to have a background picture whenever I click on any of them, for example:
If I click on button11 then its background will be "Hello.jpg"
If click button12 then its background will be become "Hello.jpg"
Is there a method to write a code instead of writing code for individual button?
The code should detect which button I clicked and then change its background.
Private Sub e_11_Click(sender As Object, e As EventArgs) Handles e_11.Click
e_11.Image = Image.FromFile("E:\battleship\Explode.gif")
End Sub
Is there a way that handles every button click?
Yes, you can bind the same method to multiple controls:
Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles e_1.Click, e_2.Click, e_3.Click, ...
Dim myButton = DirectCast(sender, Button)
myButton.Image = Image.FromFile("E:\battleship\Explode.gif")
End Sub

Open Tabs Control

I'm using MDI container to run my bussiness application I created for my client.
Since using MDI means that when I open several forms they will still run in background all the time untill I close them manualy.
What I need is to make User Control or anything else that could preview all opened forms in Tab Form so my client can easily close all or some of opened forms without closing a form he is curently viewing.
For now I have used this code, so for now only first clicked item from menu appears as button, but not others clicked menu items.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim Button As New Button
Me.Panel5.Controls.Add(Button)
Button.Text = e.ClickedItem.Name
Button.Width = 50
Button.Height = 25
End Sub
Now I need to write a code to add more buttons bellow, also should add a code for adding buttons only when I click on SubMenu item (The one when is clicked new Form appear).
And also, I should now add a little Close button into previewed User-Button-Control.
From your comments, I understand that your ideas regarding adding buttons at runtime are not too clear and thus I am including a small code which hopefully will help you on this front. Start a new project and put a Panel (Panel5) and a Button (AddButtons) on it, and write this code:
Dim lastButtonIndex, lastLeft, lastTop As Integer
Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
Dim curButton As Button = DirectCast(sender, Button)
If (curButton.Name = "Button1") Then
'do Button1 stuff
End If
'etc.
End Sub
Private Sub addNewButton()
lastButtonIndex = lastButtonIndex + 1
lastLeft = lastLeft + 5
lastTop = lastTop + 5
Dim Button As New Button
With Button
.Name = "Button" + lastButtonIndex.ToString()
.Text = "Button" + lastButtonIndex.ToString()
.Width = 50
.Height = 25
.Left = lastLeft
.Top = lastTop
AddHandler .Click, AddressOf Button_Click
End With
Me.Panel5.Controls.Add(Button)
End Sub
Private Sub ButtonAddButtons_Click(sender As System.Object, e As System.EventArgs) Handles AddButtons.Click
addNewButton()
End Sub
This code will add a new button to the panel every time you click on AddButtons. All the buttons will have an associated Click Event (the same one for all of them): Button_Click. The way to know which button is the current one inside this method is via sender, as shown in the code (you can put as many conditions as buttons. The names are given sequentially starting from 1; but you can take any other property as reference, curButton is the given Button Control).
Bear in mind that one of the problems you have to take care of is the location of the buttons. The code above has a very simplistic X/Y values (Left/Top properties) auto-increase which, logically, will not deliver what you want.

Contextmenu position in vb.net

i have a datagridview. On right click it shows a contextmenu but it is always in the right upper corner. I want it so that the menu appears on the cell where user right clicks. It could be Cell 1 or two or whatever.
Thanks
Furqan
The easiest way to do this is to handle showing your context menu on your own (not using the context menu property on the grid view) on MouseDown for the data grid. Like this:
Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(CType(sender, Control), e.Location)
End If
End Sub