Dynamic Buttons-VBForums
Results 1 to 11 of 11

Thread: Dynamic Buttons

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Dynamic Buttons

    Hi all! this one is driving me crazy!.

    Got this code (assigned to a button in my form) to create a new button:

    Code:
      Dim btn As New ToolStripMenuItem(Me.tbTemplateName.Text)
            Form1.ProgrammeTemplatesToolStripMenuItem.DropDownItems.Add(btn)
            With btn
                .Visible = True
            End With
    
            Form1.TS.Visible = True
    The code is working fine. However, on closing my form, and re launching...the button i've added is not there. How can i store it for when i reopen my form?

    Huge thanks in advance! Help and advice is great.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,471

    Re: Dynamic Buttons

    use a my.settings stringcollection to store information about your buttons + add to it as you add buttons.

    at startup, you can read the stringcollection + recreate your dynamic buttons

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: Dynamic Buttons

    Thanks Paul. Would you be able to guide me through the process?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,471

    Re: Dynamic Buttons

    Project-->Properties-->Settings + add a new stringcollection setting

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         If My.Settings.dynamicButtons Is Nothing Then
    5.             My.Settings.dynamicButtons = New Specialized.StringCollection
    6.         End If
    7.  
    8.         For Each s As String In My.Settings.dynamicButtons
    9.             addDynamicButton(s)
    10.         Next
    11.     End Sub
    12.  
    13.     Private Sub addDynamicButton(ByVal text As String)
    14.         'your button adding code here
    15.     End Sub
    16.  
    17.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    18.         'add new button at runtime
    19.         addDynamicButton("button text")
    20.         My.Settings.dynamicButtons.Add("button text")
    21.         My.Settings.Save()
    22.     End Sub
    23.  
    24. End Class

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: Dynamic Buttons

    Hi Paul! Thanks for this.

    I've got a problem in that I'd like the text displayed on the button to be taken from a text box: "tbTemp"

    And indeed, this seems to work at runtime. The button is added with the correct text. And it loads the data. however when i close and re open the form, i have a massive list of empty buttons and none of them seem to do anything.

    Any advice?

  6. #6
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: Dynamic Buttons

    This works for adding buttons


    Code:
    Public Class Form1
    
        Dim btnx As Integer = 100
        Dim btny As Integer = 100
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If My.Settings.dynamicButtons Is Nothing Then
                My.Settings.dynamicButtons = New Specialized.StringCollection
            End If
    
            For Each s As String In My.Settings.dynamicButtons
                addDynamicButton(s)
            Next
        End Sub
    
        Private Sub addDynamicButton(ByVal text As String)
            Dim dynamicBtn As New Button
            
            dynamicBtn.Left = btnx
            dynamicBtn.Top = btny
            btnx = btnx + 20
            btny = btny + 20
            dynamicBtn.Name = "dbtn"
            dynamicBtn.Text = "Dynamic button"
            dynamicBtn.Visible = True
            Me.Controls.Add(dynamicBtn)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'add new button at runtime
            addDynamicButton("does nothing")
            My.Settings.dynamicButtons.add("Button text")
            My.Settings.Save()
        End Sub
    End Class

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: Dynamic Buttons

    IKdekker, that works great...how would i transfer it to a toolstrip menu item?

  8. #8
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: Dynamic Buttons

    Thank me, lol
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If My.Settings.dynamicButtons Is Nothing Then
                My.Settings.dynamicButtons = New Specialized.StringCollection
            End If
    
            For Each s As String In My.Settings.dynamicButtons
                addDynamicButton(s)
            Next
        End Sub
    
        Private Sub addDynamicButton(ByVal text As String)
            Dim dynamicBtn As New ToolStripMenuItem
            ToolStripDropDownButton1.DropDownItems.Add("Your item")
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'add new button at runtime
            addDynamicButton("does nothing")
            My.Settings.dynamicButtons.Add("new")
            My.Settings.Save()
        End Sub
    End Class

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: Dynamic Buttons

    Thank you very much indeed! I honestly cannot thank the people on here enough for all the help provided! It's awesome!

    Cheers

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,471

    Re: Dynamic Buttons

    Quote Originally Posted by ikdekker View Post
    Thank me, lol
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If My.Settings.dynamicButtons Is Nothing Then
                My.Settings.dynamicButtons = New Specialized.StringCollection
            End If
    
            For Each s As String In My.Settings.dynamicButtons
                addDynamicButton(s)
            Next
        End Sub
    
        Private Sub addDynamicButton(ByVal text As String)
            Dim dynamicBtn As New ToolStripMenuItem
            ToolStripDropDownButton1.DropDownItems.Add("Your item")
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'add new button at runtime
            addDynamicButton("does nothing")
            My.Settings.dynamicButtons.Add("new")
            My.Settings.Save()
        End Sub
    End Class
    the text parameter in addDynamicButton is there for a reason, which you have completely failed to grasp.

  11. #11
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: Dynamic Buttons

    I am not familiar with these such things so I'm not sure what it does.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.