Can't make click event of button created in code - vb.net

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Panel1.Controls.Clear()
Dim patiekalai = New Button()
Panel1.Controls.Add(patiekalai)
patiekalai.Location = New Point(0, 0)
patiekalai.Size = New Size(80, 50)
patiekalai.Image = Image.FromFile("../M/Karštieji patiekalai.jpg")
Private Sub Patiekalai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Patiekalai.Click
I get error while trying to add a Click event handler for my dynamically created button patiekalai:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

You can't do this because the button does not exist until Button1 is clicked, at run-time. What you want to do is add a handler as well at run-time.
Remove the "Handles" from the "Patiekalai_Click" method. Then, after you create your control at run-time: "patiekalai.Image = Image...." add this line
AddHandler patiekalai.Click AddressOf Patiekalai_Click
This tells the click event of the button to call the selected method.

Related

Add event handlers for runtime created controls in runtime created forms

Im making a class that will show a form with some text and a OK button. But I can't seem to find how to handle clicking the OK button. Here is the code that im having problems with:
Private Sub ok_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myForm.okButton.Click
'code
End Sub
Runtime form is called myForm
Runtime button is called okButton
How I can fix this?
You'd use AddHandler:
' ... run-time control is created ...
Dim btn As New Button
btn.Text = "Hello World!"
AddHandler btn.Click, AddressOf ok_click
In your existing handler, you do NOT use the "Handles" keyword on the end. If you need a reference to the button, use the "sender" parameter:
Private Sub ok_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
' ... do something with "btn" ...
btn.Enabled = False
End Sub

A problem on right-click menu using Vb.Net

I want to add a right click menu to a dynamic added button by using ContextMenuStr. My code are as follows:
Public Class Form1
Private mybutton As New Button
Private i As Integer
Private mybutton_cms As ContextMenuStrip = New ContextMenuStrip()
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "addbutton" Then
***Dim mybutton As New Button***
Me.Controls.Add(mybutton)
i = i + 1 'give a ID to the dynamic added button
mybutton.Left = 200
mybutton.Top = 200
mybutton.Name = "mybutton"
AddHandler mybutton.MouseMove, AddressOf mybutton_MouseMove
ElseIf e.Node.Text = "deletebutton" Then
Me.Controls.RemoveByKey("mybutton")
i = i - 1 'delete the the ID of button
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tsm As ToolStripMenuItem = New ToolStripMenuItem("change icon")
mybutton_cms.Items.Add(tsm)
mybutton.ContextMenuStrip = mybutton_cms
End Sub
Private Sub mybutton_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
OBJ_Resize.ResizeOBJ(sender, e, Me)
Dim file As New IO.StreamWriter("data2.xml", OpenMode.Append)
file.WriteLine("width=" & sender.width)
file.WriteLine("height=" & sender.height)
file.WriteLine("x=" & sender.left)
file.WriteLine("y=" & sender.top)
file.Close()
End Sub
Private Sub mybutton_changeicon(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
changeIcon.changeicon(sender, e)
End Sub
End Classa
there is a bug that if I delete the Code referenced with *,namely :
Dim mybutton As New Button
when I run the code, there is no right click menu on the button dynamiclly added.
And if I add this code,there is only one dynamical added button on the form1. But the rigtht click menu will show.
What I want to do is click “addbutton” once to get a button, and each button can have a right-click menu
What's wrong with my code?
Beside,could you plese tell me How to add left-click events to the right menu item?
Thanks!

Get text of self object in vb.net

Let's say I have a button with the text 'A'.
When I click this button, I want a messagebox to appear with the text of what name of the button that was clicked.
I tried setting it to MessageBox.Show(Me.Text) but that just gives me the form name.
How can I refer to the text of button I just clicked?
If you have to handle multiple button click try following method
Private Sub btn_a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn_a.Click _
,btn_b.Click 'you can add other buttons click event here(ex. btn_c,btn_d etc)
Dim objButton As Button = DirectCast(sender, Button)
MessageBox.Show(objButton.Text)
End Sub
DirectCast() vs. CType()
The reason why MessageBox.Show(Me.Text) didn't work is that Me refers the class containing the code, in this case, the Form).
If your button's event handler only handles one button, you can just hard code the buttons name like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(Button1.Text)
End Sub
If the event handler can handle more than one button, you can use the sender argument to reference the button being clicked:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim but as Button = CType(sender, Button)
MessageBox.Show(but.Text)
End Sub

Get the name of the button that triggered the event

I have an event handler that handles the click event of multiple buttons:
Private Sub primeHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _2s.Click, _3s.Click, _4s.Click, _5s.Click, _6s.Click
End Sub
_2s, _3s, etc are all buttons.
Now I need a way to determine which button triggered the event and also get the button's name as string. Any way to do that? Thanks
You can cast sender to type Button and access the Name property.
Private Sub primeHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _2s.Click, _3s.Click, _4s.Click, _5s.Click, _6s.Click
Dim myButton As Button = CType(sender, Button)
Dim myName As String = myButton.Name
End Sub
Use sender - it's what it's designed to do.
MessageBox.Show((sender as Button).Name);
If you're going to use it more than once, assign it to a variable to make it easier.
var button = (sender as Button);
MessageBox.Show(button.Name);

assigning object to button.tag

I created a Sub I want to run everytime one of two buttons is clicked. I added the handles of both buttons to the sub so that clicking either one will fire the subroutine.
I placed listview object A in buttonA.tag, and listview object B in buttonB.
When the button is clicked I do my best to extract the listview instance tucked into the button's tag. The problem is there is no instance in the tag. It is simply "nothing."
Private Sub Execute(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click, btnB.Click
Dim buttonSender As Button = Nothing
buttonSender = CType(sender, Button)
Dim btnListView As ListView = buttonSender.Tag
End Sub
-------------------Edit-1
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.btnA.Tag = Me.lvA
Me.btnB.Tag = Me.lvB
End Sub
Your time is appreciated.
Try this,
Dim buttonSender As Button = CType(sender, Button)