Accessing each button's properties after they are dynamically generated in vb.net? - vb.net

I have used the following code to generate buttons dynamically. I want to know how to code in such a way that if i click one button, there should be some change done to some other button in the same form. Since all the buttons are generated in the loop, i do not know how to call one button elsewhere in the code.
Private Sub random2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 16
Dim btn As New Button
AddHandler btn.Click, AddressOf ClickMe
btn.Width = 23
btn.Height = 23
btn.Text = ""
btn.Tag = i
btn.Name = i
btn.Name = "Button" & i
flp.Controls.Add(btn) 'flp stand for flow layout panel
Next
End Sub
Private Sub ClickMe(ByVal Sender As Object, ByVal e As EventArgs)
Dim btn As Button
btn = CType(Sender, Button)
dim str as string = btn.tag
MsgBox(str)
End Sub
End Class

You have added all your dynamically created buttons to the FlowLayoutPanel control collection.
You will find them there with syntax like this
Dim btn As Button = TryCast(flp.Controls("name"), Button)
if btn IsNot Nothing then
btn.Caption = "New Text"
....
End If
Or if you want a button at a specific index
Dim btn As Button = TryCast(flp.Controls(index), Button)

Related

Dynamically create multiple button and assign single click and double click comand

I created multiple button dynamically inside a panel based on multiple click on add button. Each button deletes When I click on it.
I want each button to delete when I single click on it, and say hello when I double click. Thank you.
I have tried using this code to delete which works fine, but I cannot figure how to assign seperate code to display hello when I double click or right click on it without affecting delete aspect of it.
Private Sub btnDynamic_Click(ByVal sender As Object, ByVal e As EventArgs)
'Reference the Button which was clicked.
Dim button As Button = CType(sender, Button)
'Determine the Index of the Button.
Dim index As Integer = Integer.Parse(button.Name.Split("_")(1))
'Find the TextBox using Index and remove it.
FlowLayoutPanel1.Controls.Remove(FlowLayoutPanel1.Controls.Find(("btnDynamic_" & index), True)(0))
'Remove the Button.
FlowLayoutPanel1.Controls.Remove(button)
'Rearranging the Location controls.
For Each btn As Button In FlowLayoutPanel1.Controls.OfType(Of Button)()
Dim controlIndex As Integer = Integer.Parse(btn.Name.Split("_")(1))
If (controlIndex > index) Then
Dim btn1 As Button = CType(FlowLayoutPanel1.Controls.Find(("btnDynamic_" & controlIndex), True)(0), Button)
btn1.Top = (btn.Top - 25)
'txt.Top = (txt.Top - 25)
End If
Next
End Sub
Here is the create button code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If TextBox1.Text = "" Then
Exit Sub
End If
Dim count As Integer = Form2.FlowLayoutPanel2.Controls.OfType(Of Button).ToList.Count
Dim button As Button = New Button
button.Size = New System.Drawing.Size(28, 21)
button.Name = "btnDynamic_" & (count + 1)
button.Text = TextBox1.Text
AddHandler button.Click, AddressOf Me.button_click
Form2.FlowLayoutPanel2.Controls.Add(button)
End Sub
You have to enable DoubleClick option first.
You do so with:
Public Class DoubleClickButton
Inherits Button
Public Sub New()
SetStyle(ControlStyles.StandardClick Or ControlStyles.StandardDoubleClick, True)
End Sub
End Class
Then create a "DoubleClick" and "SingleClick" methods and assign them to (new custom) buttons when you are creating them.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For x = 0 To 2
Dim ButtonX As New DoubleClickButton
ButtonX.Text = x
AddHandler ButtonX.MouseClick, AddressOf SingleClickE
AddHandler ButtonX.MouseDoubleClick, AddressOf DoubleClickE
FlowLayoutPanel1.Controls.Add(ButtonX)
Next
End Sub
Private Sub SingleClickE(sender As Object, e As MouseEventArgs)
Debug.Print("Hello!")
End Sub
Private Sub DoubleClickE(sender As Object, e As MouseEventArgs)
FlowLayoutPanel1.Controls.Remove(sender)
End Sub
Thank you all for attempting to answer my question. I have been able to figure it out. I intend firing diffrent events with dynamically created buttons using single click, double click or right click. I ended up using single and right click command. Though would have prefered double click, but it is ok. Below is the code.
'Create buttons
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 5
Dim button As New Button
button.Text = i
AddHandler button.Click, AddressOf SingleClickE
AddHandler button.MouseDown, AddressOf RightClickE
FlowLayoutPanel1.Controls.Add(button)
Next
End Sub
'Fire it up with single click
Private Sub SingleClickE(sender As Object, e As MouseEventArgs)
Dim button As Button = CType(sender, Button)
MsgBox("Hello World")
End Sub
'Fire it up with Right Click
Private Sub RightClickE(sender As Object, e As MouseEventArgs)
Dim button As Button = CType(sender, Button)
If e.Button = MouseButtons.Right Then
FlowLayoutPanel1.Controls.Remove(button)
End If
End Sub

How to disable and get name for control in FlowLayoutPanel from ContextMenuStrip in VB.Net

My program contains buttons in a FlowLayoutPanel.
I want to disable any button when right click on it and click "Disable" in the ContextMenuStrip.
My code is:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 30
Dim btn As New Button
btn.Name = i
btn.Text = i
btn.ContextMenuStrip = ContextMenuStrip1
FlowLayoutPanel1.Controls.Add(btn)
Next
End Sub
End Class
declare a public varibale for keeping a control
public ctrl as button = nothing
you can create a right click by putting this code on mouse down...
If e.Button <> Windows.Forms.MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
ctrl = sender
Dim item1 = cms.Items.Add("Disable")
item1.Tag = 1
AddHandler item1.Click, AddressOf Disable
end if
and in the diable sub you can code like this...
Private Sub Disable()
ctrl.enabled = false
End Sub

vb.net control button in popup

I'm new to programming and I need help with something really basic..
I have a form with a datagridview and an "insert" button that opens a popup window (new form). This popup contains labels and textboxes, and 2 buttons (insert & cancel). How do you add event handlers to the buttons inside the popup? For example, if the user clicks "Cancel", the popup should close.
Here's the code I have so far:
Public Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click
Dim insertPopup As New Form
insertPopup.Size = New System.Drawing.Size(300, 400)
insertPopup.StartPosition = FormStartPosition.CenterScreen
insertPopup.Show()
Dim acceptButton As New Button
Dim cancelButton As New Button
acceptButton.Location = New System.Drawing.Point(100, 325)
acceptButton.Text = "Insert"
acceptButton.Size = New System.Drawing.Size(85, 24)
acceptButton.TabIndex = 1
cancelButton.Location = New System.Drawing.Point(190, 325)
cancelButton.Text = "Cancel"
cancelButton.Size = New System.Drawing.Size(85, 24)
cancelButton.TabIndex = 2
insertPopup.Controls.Add(acceptButton)
insertPopup.Controls.Add(cancelButton)
End Sub
Thanks for your help.
You use the AddHandler Method to add the proper EventHandler. You will need to be sure to create your handler with the correct signature.
AddHandler acceptButton.Click, AddressOf acceptButton_Click
AddHandler cancelButton.Click, AddressOf cancelButton_Click
Private Sub acceptButton_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub cancelButton_Click(sender As System.Object, e As System.EventArgs)
CType(CType(sender, Button).Parent, Form).Close()
End Sub

indexing button in vb.net 2010

my code is as follows:
Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles btn1.Click, btn2.Click, btn3.Click
Dim objBtn() As Object = {btn1, btn2, btn3}
Dim btn As Button
With objBtn
btn = CType(objBtn(x), Button)
If btn.FlatStyle = FlatStyle.Standard Then
btn.FlatStyle = FlatStyle.Flat
btn.FlatAppearance.BorderColor = Color.OrangeRed
Else
btn.FlatStyle = FlatStyle.Standard
End If
End With
End Sub
What must i do for the program to control the value of x automatically? that is, suppose i click on btn1, value of x must become 0; if i click on btn2, value of x must become 1 and so on. Thank you.
This will do what you want. :D
x = objBtn.IndexOf(sender)
I think you are trying to (badly) implement a NumericUpDown control.
http://msdn.microsoft.com/en-us/library/729xt55s.aspx
Jus drag&drop it into your Form, maybe set its properties, and it will be ready to go.
It looks like you want to set the state of one button differently from the others in the set.
Here's code to help you to do that.
Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles _
btn1.Click, btn2.Click, btn3.Click
Dim buttons() As Button = {btn1, btn2, btn3}
For Each btn in buttons
If btn Is sender Then
btn.FlatStyle = FlatStyle.Standard
Else
btn.FlatStyle = FlatStyle.Flat
btn.FlatAppearance.BorderColor = Color.OrangeRed
End If
Next
End Sub

Control array in VB.NET

How do I make a control array for buttons in VB.NET? Like in Visual Basic 6.0...
Is it possible that the syntax can be like the following?
dim a as button
for each a as button in myForm
a.text = "hello"
next
Controls in .NET are just normal objects so you can freely put them into normal arrays or lists. The special VB6 construct of control arrays is no longer necessary.
So you can for example say,
Dim buttons As Button() = { Button1, Button2, … }
For Each button As Button In Buttons
button.Text = "foo"
End For
Alternatively, you can directly iterate over the controls inside a container (e.g. a form):
For Each c As Control In MyForm.Controls
Dim btt As Button = TryCast(c, Button)
If btt IsNot Nothing Then ' We got a button!
btt.Text = "foo"
End If
End For
Notice that this only works for controls that are directly on the form; controls nested into containers will not be iterated this way; you can however use a recursive function to iterate over all controls.
You can't create a control array in VB.NET, but you can archive similar functionality using the Handles keyword.
public sub Button_Click(sender as Object, e as EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
'Do Something
End Sub
Yes, you can do this. But I don't think you can iterate buttons directly by giving myForm.
You create a Form and add a Layout 10 * 10, and try this,
Public Class Form1
Private NRow As Integer = 10
Private NCol As Integer = 10
Private BtnArray(NRow * NCol - 1) As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TableLayoutPanel1.Size = Me.ClientSize
For i As Integer = 0 To BtnArray.Length - 1
BtnArray(i) = New Button()
BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray(i).Text = CStr(i)
TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod NCol, i \ NCol)
AddHandler BtnArray(i).Click, AddressOf ClickHandler
Next
End Sub
Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("I am button #" & CType(sender, Button).Text)
End Sub
End Class