Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

Calling VB.Net form from dynamically created buttons

Hello,
   I have created a menu in VB.Net which creates several button dynamically. Each of these buttons opens the same form, but I would like to pass through a unique number depending on which button is chosen. At the minute my menu is created but only the last button opens the form - instead of all the buttons. Any ideas where I am going wrong???
Here is some relevant code:-

While sqlDtr.Read
               .
               .
               cmdButton = Me.MakeANewButton(toolBar, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")), nextInt, AddressOf btnSearch_Click)
               .
               .
                nextInt = nextInt + 1
            End While
            sqlDtr.Close()

Function MakeANewButton(ByVal commandBar As Office.CommandBar, ByVal caption As String, ByVal GroupId As Integer, ByVal ParentID As Integer, ByVal faceID As Integer, ByVal clickHandler As Office._CommandBarButtonEvents_ClickEventHandler) As Office.CommandBarButton
        Try
            newButton = CType(commandBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            newButton.Style = Office.MsoButtonStyle.msoButtonCaption
            newButton.Caption = caption
            newButton.FaceId = faceID
            AddHandler newButton.Click, clickHandler
            If Begin_Group = -1 Then
                Begin_Group = 0
                newButton.BeginGroup = True
            Else
                newButton.BeginGroup = False
            End If
            newButton.DescriptionText = CStr(GroupId)
            Return newButton
        Catch ex As System.Exception
            ' Add code here to handle the exception.
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function

Private Sub btnSearch_Click(ByVal barButton As Office.CommandBarButton, ByRef someBool As Boolean)
        Try
            If Not IsNothing(frmFind) Then
                If Not frmFind.IsDisposed Then
                    frmFind.BringToFront()
                Else
                    frmFind = New frmSearch()
                End If
            Else
                frmFind = New frmSearch()
            End If
            frmFind.ShowDialog()

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            frmFind = Nothing
        End Try
    End Sub

Thanks,
mcdermon
0
mcdermon
Asked:
mcdermon
  • 10
  • 6
1 Solution
 
armoghanCommented:
Are you using cmdButton somewhere.. As it is over written everytime and at the end of the loop only last one will be left

Try doing some thing like

While sqlDtr.Read
               cmdButton = Me.MakeANewButton(toolBar, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")), nextInt, AddressOf btnSearch_Click)
               .AddHandler cmdButton.Click, clickHandler
              nextInt = nextInt + 1
            End While
            sqlDtr.Close()
0
 
mcdermonAuthor Commented:
Hello, Thanks for your reply. That code (.AddHandler cmdButton.Click, clickHandler
) is in the Function MakeANewButton. Is this the wrong place for it?
0
 
armoghanCommented:
No it should actually work from there as well

There is a difference in name as well. May be that is creating the problem  

AddHandler cmdButton.Click, Addressof btnSearch_Click
or
AddHandler newButton.Click, Addressof btnSearch_Click

should be correct
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
mcdermonAuthor Commented:
I have tried both, but I still only get one working button.
0
 
mcdermonAuthor Commented:
Oops, that is not actually the problem. I took out the form and just did a msgbox of the caption of each button. This works fine. It seems to be that when I hide the form it does not become visible again. This is my code:-

Private Sub btnSearch_Click(ByVal barButton As Office.CommandBarButton, ByRef someBool As Boolean)
        MsgBox(barButton.Caption) ' this works ok for each button!
        Try
            If Not IsNothing(frmFind) Then
                If Not frmFind.IsDisposed Then
                    frmFind.BringToFront()
                Else
                    frmFind = New frmSearch()
                End If
            Else
                frmFind = New frmSearch()
            End If
            frmFind.ShowDialog()

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            frmFind = Nothing
        End Try
    End Sub

Any suggestions??
0
 
armoghanCommented:
Better way is that
after this frmFind.ShowDialog()
do
frmFind.Dispose
and
frmFind = nothing

0
 
mcdermonAuthor Commented:
Thanks again, but I have tried that and it still is not working. The first time I open the form from any button it opens OK, but then will not open at all after that. Except the very last button. This one I can open and close as much as I like. I am really confused.
0
 
armoghanCommented:
ok the code seems right, Just simplyfy it with the following code and check and replace the try finally with the code given

Try
            frmFind= New frmSearch()
            frmFind.ShowDialog()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            frmFind.Dispose()
            frmFind = Nothing
        End Try
0
 
mcdermonAuthor Commented:
Oh no I am back to square one!!! I can only get one instance of the form to open - the very last button and now no matter what I do I cannot even get a message box to work on any of the other buttons. Slowly losing the will to live!!! My code is still the same as in my first post. Any suggestions?
0
 
mcdermonAuthor Commented:
In one of your ealry posts you thought it might be because cmdButton is being overwritten each time the loop goes round. There might be something in this. Is there a way of concatenating an integer onto the field name dynamically so that first it would be cmdbutton1 and next cmdbutton2 etc. Something like:-

 Me(cmdbutton & nextint)

what do you think?
0
 
armoghanCommented:
you said that this works for every button
Private Sub btnSearch_Click(ByVal barButton As Office.CommandBarButton, ByRef someBool As Boolean)
        MsgBox(barButton.Caption) ' this works ok for each button!
end sub

It means you have some problem with you frmSearch or logic present
Why dont you make a simple form and do something like

Try
            dim frmFind as  New Form()
frmFind.Text = barButton.Caption
            frmFind.ShowDialog()
            frmFind.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message)
       End Try

and see that it shows a form with the caption present or not ?
0
 
mcdermonAuthor Commented:
That did work, but it won't work now. I am so frustrated. Is there any way that I can upload my entire solution so that you can have a look at it and see how it works?
0
 
mcdermonAuthor Commented:
You were definitley partly right. It is the cmdbutton getting overwritten. So now I have this:-

Sub Get_Options(ByVal Parent As Integer, ByVal temp As Integer, Optional ByVal PopUp As Office.CommandBarPopup = Nothing)
        Static nextInt As Integer
        Static cmdButton As Office.CommandBarButton
        Static cmdButton1 As Office.CommandBarButton
        Static cmdButton2 As Office.CommandBarButton
        Static cmdButton3 As Office.CommandBarButton
        Static cmdButton4 As Office.CommandBarButton
        Dim sqlcmdAll As SqlClient.SqlCommand = Nothing
        Dim sqlCmd As SqlClient.SqlCommand = Nothing
        Dim sqlCount As SqlClient.SqlCommand = Nothing
        Dim sqlDtr As SqlDataReader = Nothing
        Dim subcmdAll As SqlClient.SqlCommand = Nothing
        Dim subCmd As SqlClient.SqlCommand = Nothing
        Dim subDtr As SqlDataReader = Nothing
        Dim NewId As Integer = 0
        Dim NewParent As Integer = 0
        Dim countRecs As Integer = 0
        Dim paramCount As SqlParameter = Nothing
        Dim I As Integer = 0
        Dim tempCount As Integer = 0

        Try

            Open_Connection()

            sqlCount = New SqlClient.SqlCommand("sp_Count_Options", Conn)
            sqlCount.Parameters.Add("@ParentID", SqlDbType.Int).Value = Parent
            sqlCount.CommandType = CommandType.StoredProcedure
            paramCount = sqlCount.Parameters.Add("ReturnValue", SqlDbType.Int)
            paramCount.Direction = ParameterDirection.ReturnValue
            sqlCount.ExecuteNonQuery()
            countRecs = CInt(sqlCount.Parameters("ReturnValue").Value)

            If IsDBNull(countRecs) Then
                countRecs = 0
            End If

            sqlCmd = New SqlClient.SqlCommand("sp_Get_Options", Conn)

            sqlCmd.Parameters.Add("@ParentID", SqlDbType.Int).Value = Parent
            sqlCmd.CommandType = CommandType.StoredProcedure
            sqlDtr = sqlCmd.ExecuteReader()

            While sqlDtr.Read
                NewId = CInt(sqlDtr("Id"))
                NewParent = CInt(sqlDtr("ParentId"))

                If CInt(sqlDtr("Type")) = 0 Then
                    If Parent = 0 Then ' rootnode
                        'If temp = 0 Then
                        If temp = 3 Then
                            cmdPopup = Me.MakeASubPopup(PopUp, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")))
                        Else
                            cmdPopup = Me.MakeANewPopup(toolBar, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")))
                        End If
                    Else
                        If temp = 0 Then
                            If Default_View = NewParent Then
                                'If (Status = 1) Then
                                cmdPopup = Me.MakeANewPopup(toolBar, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")))
                            Else
                                cmdPopup = Me.MakeASubPopup(PopUp, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")))
                            End If
                        Else
                            MakeASubPopup(PopUp, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")))
                            'temp = 1
                        End If
                    End If

                        Status = Status + 1

                        If tempCount = countRecs Then
                            'do nothing
                    Else
                        If temp = 3 Then
                            Get_Options(NewId, 3, newPopup)
                        Else
                            Get_Options(NewId, 1, newPopup)
                        End If
                    End If

                    tempCount = tempCount + 1

                Else

                    nextInt = nextInt + 1

                    If Parent = 0 Then
                        If temp = 3 Then
                            cmdButton = Me.MakeASubButton(PopUp, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")), nextInt, AddressOf btnSearch_Click)
                        Else
                            cmdButton1 = Me.MakeANewButton(toolBar, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")), nextInt, AddressOf btnSearch_Click)
                        End If
                    Else
                        If Default_View = NewParent Then
                            If temp = 3 Then
                                cmdButton2 = Me.MakeASubButton(PopUp, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")), nextInt, AddressOf btnSearch_Click)
                            Else
                                cmdButton3 = Me.MakeANewButton(toolBar, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")), nextInt, AddressOf btnSearch_Click)
                            End If
                        Else
                            cmdButton4 = Me.MakeASubButton(PopUp, CStr(sqlDtr("Caption")), CInt(sqlDtr("Group")), CInt(sqlDtr("ParentId")), nextInt, AddressOf btnSearch_Click)
                        End If
                    End If
                End If
            End While

            sqlDtr.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Close_Connection()
        End Try
    End Sub

and it seems that the last instance of each particular button works (i.e.the last button1, button2 etc)
0
 
mcdermonAuthor Commented:
Could I use a dictionary. Do you have any advice on dictionaries as I have never used one before.
Thanks,
mcdermon
0
 
armoghanCommented:
Try Making an arrayList and start adding each button in it
0
 
mcdermonAuthor Commented:
Hello, I could literally jump for joy. I got it sorted eventually. I did use a dictionary in the end. You were right in that each time my Sub called itself it was re-dimming the variable and overwritting the existing ones. Thanks for your help.
mcdermon
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now