I am getting an ‘}’ expected error on the buttonNamesAndFormNames method. I can seem to be figuring out why it’s throwing the error as you can clearly see it at the end of the method. I have included the other methods as I am trying to get my dynamically created buttons to redirect to my DocumentUnderstand form. I want these buttons in my SideBar to redirect to all different forms in VB.NET. I have tried ‘Clean’ & ‘Rebuild’, but the error is still there. Any help is greatly appreciated.
' Define an array with button names and their corresponding form names
Private buttonNamesAndFormNames() As (buttonName As String, formName As String) = {
("Document understanding", "DocUnderstand.vb"),
("Button 2", "Form2"),
("Button 3", "Form3"),
("Button 4", "Form4"),
("Button 5", "Form5"),
("Button 6", "Form6")
' Add more button names and corresponding form names as needed
}
Private Sub SideMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeDynamicControls()
End Sub
Private Sub InitializeDynamicControls()
' Add buttons dynamically based on the buttonNamesAndFormNames array
For Each pair In buttonNamesAndFormNames
Dim button As New Button()
button.Text = pair.buttonName
button.Dock = DockStyle.Top
AddHandler button.Click, AddressOf OpenForm
Panel1.Controls.Add(button)
Next
End Sub
Private Sub OpenForm(sender As Object, e As EventArgs)
' Handle button click event
Dim button As Button = DirectCast(sender, Button)
Dim index As Integer = -1
' Find the index of the clicked button in the buttonNamesAndFormNames array
For i As Integer = 0 To buttonNamesAndFormNames.Length - 1
If buttonNamesAndFormNames(i).buttonName = button.Text Then
index = i
Exit For
End If
Next
' Open the corresponding form based on the index
If index >= 0 AndAlso index < buttonNamesAndFormNames.Length Then
Dim formName As String = buttonNamesAndFormNames(index).formName
' Assuming that the form files are in the same namespace as SideBar
If Not String.IsNullOrEmpty(formName) Then
Dim formInstance As Form = CType(Activator.CreateInstance(Type.GetType(formName)), Form)
formInstance.Show()
Else
MessageBox.Show("Invalid form name or form not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show("Invalid button index.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub