Solved

Calling VB.Net form from dynamically created buttons

Posted on 2007-11-22
16
433 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
  • 10
  • 6
16 Comments
 
LVL 18

Accepted Solution

by:
armoghan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Author Comment

by:mcdermon
Comment Utility
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
Comment Utility
Better way is that
after this frmFind.ShowDialog()
do
frmFind.Dispose
and
frmFind = nothing

0
 

Author Comment

by:mcdermon
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:mcdermon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Try Making an arrayList and start adding each button in it
0
 

Author Comment

by:mcdermon
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now