Determine which groupbox triggered button click event

  • Thread starter Thread starter x73
  • Start date Start date
X

x73

Guest
I have a series of groupboxes on a form, lets say 2 for this example. Each groupbox has an associated "Add" button located on the form, that when clicked will add the following dynamic controls inside the group box; Label, textbox and a Delete button aligned in a row.

I have button click handlers for the Add buttons on the form. I also have handlers for click events for the labels (the labels represent status green for good and red for not so good). I also have a handler for the dynamically created Delete button located in the groupbox.


Here is my question: Can I reference the groupbox where the delete button was clicked? I am trying to eliminate repetitive code.

I have:

Dim button As Button = CType(sender, Button).

.

.

and I want to change "groupbox1.Controls.Remove(groupbox1.Controls.Find(("txt_" & index), True)(0))" to the following:

button.parent.name.controls.remove(button.parent.name.controls.find(("txt_" & index),true)(0))

if I click the delete button in groupbox1 then the corresponding row will be removed from groupbox1, and if I click delete button in groupbox2 that row will be removed using the same code block..

The way its setup now, I'll need to have a nested if for every groupbox container on my form - which eventually will be five.

Some of the code is not mine but sourced from various sites on the interweb.. Any help would be greatly appreciated.


CODE:

Public Class Form1



Private Sub lbl_click(ByVal sender As Object, ByVal e As EventArgs)

Dim label As Label = CType(sender, Label)



'determine index of Label

Dim lblindex As Integer = Integer.Parse(label.Name.Split("_")(1))



label.Text = lblindex ' only for testing will be removed when final

' Flip the background color red <--> green





If label.BackColor = Color.FromArgb(170, 0, 0) Then

label.BackColor = Color.FromArgb(0, 255, 0)

Else

label.BackColor = Color.FromArgb(170, 0, 0)

End If





End Sub



Private Sub btnDelete_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))

Dim count As Integer = groupbox1.Controls.OfType(Of TextBox).ToList.Count

''Find the TextBox and Label using Index and remove it.







Groupbox1.Controls.Remove(Panel1.Controls.Find(("txt_" & index), True)(0))

Groupbox1.Controls.Remove(Panel1.Controls.Find(("lbl_" & index), True)(0))



Dim controlIndex As Integer = Integer.Parse(button.Name.Split("_")(1))





'Remove the Button.



Groupbox1.Controls.Remove(button)



'Rearranging the Location controls.

For Each btn As Button In groupbox1.Controls.OfType(Of Button)()

Dim controlIndex As Integer = Integer.Parse(btn.Name.Split("_")(1))



‘ do some stuff

Next





End Sub





Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

For Each Ctrl As Control In Controls

If TypeOf Ctrl Is Button Then

AddHandler DirectCast(Ctrl, Button).Click, AddressOf addbtnclick

End If

Next

End Sub



Private Sub addbtnclick(sender As Object, e As EventArgs)

Dim textbox As TextBox = New TextBox

Dim label As Label = New Label



If DirectCast(sender, Button).Name = "GB1addbtn" Then

Dim pmcount As Integer = Groupbox1.Controls.OfType(Of TextBox).ToList.Count

textbox.Location = New System.Drawing.Point(35, (35 * pmcount) + 30)

textbox.Size = New System.Drawing.Size(80, 20)

textbox.Name = "txt_" & (pmcount + 1)



label.Location = New System.Drawing.Point(5, (35 * pmcount) + 30)

label.Size = New System.Drawing.Size(25, 25)

label.Name = "lbl_" & (pmcount + 1)

label.BackColor = Color.FromArgb(170, 0, 0)



Groupbox1.Controls.Add(textbox)

Groupbox1.Controls.Add(label)



' 'Create the dynamic Button to remove the TextBox.

Dim button As Button = New Button

button.Location = New System.Drawing.Point(120, (35 * pmcount) + 30)

button.Size = New System.Drawing.Size(85, 30)

button.Name = "btnDelete_" & (pmcount + 1)

button.Text = "Delete"

AddHandler button.Click, AddressOf Me.btnDelete_Click

AddHandler label.Click, AddressOf Me.lbl_click



Groupbox1.Controls.Add(button)

ElseIf (DirectCast(sender, Button).Name) = "GB2addbtn" Then

Dim dcount As Integer = Groupbox2.Controls.OfType(Of TextBox).ToList.Count

textbox.Location = New System.Drawing.Point(35, (35 * dcount) + 30)

textbox.Size = New System.Drawing.Size(80, 20)

textbox.Name = "txt_" & (dcount + 1)



label.Location = New System.Drawing.Point(5, (35 * dcount) + 30)

label.Size = New System.Drawing.Size(25, 25)

label.Name = "lbl_" & (dcount + 1)

label.BackColor = Color.FromArgb(170, 0, 0)



Groupbox2.Controls.Add(textbox)

Groupbox2.Controls.Add(label)



' 'Create the dynamic Button to remove the TextBox.

Dim button As Button = New Button

button.Location = New System.Drawing.Point(120, (35 * dcount) + 30)

button.Size = New System.Drawing.Size(85, 30)

button.Name = "btnDelete_" & (dcount + 1)

button.Text = "Delete"



AddHandler button.Click, AddressOf Me.btnDelete_Click

AddHandler label.Click, AddressOf Me.lbl_click



Groupbox2.Controls.Add(button)



End If

End Sub

End Class

Continue reading...
 
Back
Top