Results 1 to 7 of 7

Thread: Dynamic Buttons

  1. #1
    jhaynes is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jun 2011
    Posts
    4
    Reputation
    0

    Dynamic Buttons

    Hello All,
    I am new to the group, and this seems like an appropriate place for my question.
    I am creating a windows console app, with .net 3.5. I would like to be able to dynamically display buttons based on user need. As this is for dynamic research the number of buttons need to change...not often, but to minimize code complexity it would be nice. Today it is 5, tomorrow it could be 15. Here is the sudo code of what I would like:

    int NumberofButtons = 5
    for (i = 0 to NumberofButtons)
    - add new button
    - Set name and text of button. (I know how to do this)
    - position button

    Thank you for your help,
    Justin
    Attached Images Attached Images

  2. #2
    Joshdbr is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Location
    Montana, USA
    Posts
    211
    Reputation
    39
    Here's an example:


    Dim oldX As Integer = 10
    Dim oldY As Integer = 10

    Private Sub AddButtons()
    Dim NumberOfButtons As Integer = 5
    Dim Name() As String = {"Button1", "Button2", "Button3", "Button4", "Button5", "Button6"}
    Dim Text() As String = {"Button1", "Button2", "Button3", "Button4", "Button5", "Button6"}
    For i = 0 To NumberOfButtons
    Dim NewButton As New Button 'Create a new button
    NewButton.Text = Name(i) 'Set the NewButton's Name property based on the Name array above
    NewButton.Name = Text(i) 'Set the NewButton's Text property based on the Text array above
    NewButton.Location = getLocation(NewButton) 'Set the location, see the function "getLocation" below
    Me.Controls.Add(NewButton) 'Add the NewButton to the form
    AddHandler NewButton.Click, AddressOf buttonClick 'This will make it so you can have a Click event
    Next
    End Sub

    Private Function getLocation(ByVal NewButton As Button) As Point 'This will return the "next point" so to speak, it basically adds the buttons width + 10 each time, if it runs out of room it will move down on Y
    Dim pReturn As Point = New Point(oldX, oldY)
    oldX += 10 + NewButton.Width
    If (oldX + NewButton.Width) > (Me.Width - 10) Then oldY += 10 + NewButton.Height : oldX = 10
    Return pReturn
    End Function

    Private Sub buttonClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim b As Button = sender 'Get's you the button that was clicked
    'Do your button click here
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddButtons()
    End Sub


    Hope this helps
    -Josh
    "Trust in the LORD with all your heart and do not lean on your own understanding. In all your ways acknowledge Him, and He will make your paths straight." -Proverbs 3:5-6

  3. #3
    jhaynes is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jun 2011
    Posts
    4
    Reputation
    0
    Hi Josh,
    Thank you for your help, it worked well. The only problem is that I am getting duplicate buttons.

    Button1 Button 2 Button 3
    Button1 Button 2 Button 3

    Do you know how to fix this?

  4. #4
    Joshdbr is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Location
    Montana, USA
    Posts
    211
    Reputation
    39
    Duplicates? That's odd, it works fine for me. Did you modify the code at all?
    "Trust in the LORD with all your heart and do not lean on your own understanding. In all your ways acknowledge Him, and He will make your paths straight." -Proverbs 3:5-6

  5. #5
    jhaynes is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jun 2011
    Posts
    4
    Reputation
    0
    My apologies, I had a call statement by accident. Thank you for your help : )

  6. #6
    Joshdbr is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Location
    Montana, USA
    Posts
    211
    Reputation
    39
    Glad it worked.
    "Trust in the LORD with all your heart and do not lean on your own understanding. In all your ways acknowledge Him, and He will make your paths straight." -Proverbs 3:5-6

  7. #7
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    Using one of the container controls FlowLayoutPanel or TableLayoutPanel would simplify placement of controls added.

Tags for this Thread

Bookmarks

Posting Permissions

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