Solved

Calling VB.Net form from dynamically created buttons

Posted on 2007-11-22
16
464 Views
Last Modified: 2013-11-26
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
Comment
Question by:mcdermon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
16 Comments
 
LVL 18

Accepted Solution

by:
armoghan earned 500 total points
ID: 20333468
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
 

Author Comment

by:mcdermon
ID: 20333481
Hello, Thanks for your reply. That code (.AddHandler cmdButton.Click, clickHandler
) is in the Function MakeANewButton. Is this the wrong place for it?
0
 
LVL 18

Expert Comment

by:armoghan
ID: 20333527
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mcdermon
ID: 20333576
I have tried both, but I still only get one working button.
0
 

Author Comment

by:mcdermon
ID: 20333804
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20333950
Better way is that
after this frmFind.ShowDialog()
do
frmFind.Dispose
and
frmFind = nothing

0
 

Author Comment

by:mcdermon
ID: 20334427
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20334602
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
 

Author Comment

by:mcdermon
ID: 20349328
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
 

Author Comment

by:mcdermon
ID: 20350000
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20350528
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
 

Author Comment

by:mcdermon
ID: 20357893
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
 

Author Comment

by:mcdermon
ID: 20358896
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
 

Author Comment

by:mcdermon
ID: 20359065
Could I use a dictionary. Do you have any advice on dictionaries as I have never used one before.
Thanks,
mcdermon
0
 
LVL 18

Expert Comment

by:armoghan
ID: 20359230
Try Making an arrayList and start adding each button in it
0
 

Author Comment

by:mcdermon
ID: 20364400
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question