Results 1 to 10 of 10

Thread: Create Buttons dynamically

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Create Buttons dynamically

    Hi,
    I'm creating an APP using Vb.Net 2012, and like to make something diferent.
    The actual application have a datagridview, and the user double click one cell for editing, but it is not functional.

    Actually, I get the data from SQL Server, and in my query, I got for example 30 records with the fields (ID, Reference, Name, Color)

    What I want, is the app draw 30 buttons in the form, one for each record, with the text of the id.
    Something similiar to this image:

    Attached Images Attached Images  

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,623

    Re: Create Buttons dynamically

    Well to create a button dynamically all you need to do is -

    Code:
    Dim loc As New Point
            Dim btn As New Button
    
            btn.Name = "name"
            btn.Text = "text"
            loc.X = 1   'You will need to change your X & Y to position your buttons
            loc.Y = 1
            btn.Location = loc
            Me.Controls.Add(btn)
    All you need to do is create a loop to add multiple buttons, and for each button Change the name / text and position.

    You may also want to dynamically add event handlers for you new buttons.. like this -

    Code:
    AddHandler btn.Click, AddressOf btn1_Click
    before you add your control to the forms control collection.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Create Buttons dynamically

    Ok, I was tried that
    But, How can I know wich button I pressed???
    For example, if I click the button in the line 3, column 2, how the software knows wich buttons is, and what's the ID of the recod in the database?

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,623

    Re: Create Buttons dynamically

    Thats what the AddHandler code is for!

    What i gave you was an example not fully functioning code

    If you add 1 event handler for all you buttons (which i think you will have to do) in your event handler you just need to do a Select Case around the sender button name! e.g.

    So first change your add Handler code to this -
    Code:
    AddHandler Btn.Click, AddressOf Buttons_Click
    Then in your handler you can do this;

    Code:
    Private Sub Buttons_Click(sender As System.Object, e As System.EventArgs) 
    
            Dim btn As Button
    
            btn = DirectCast(sender, Button)
    
            Select Case btn.Name
    
                Case "Button1"
                         'do stuff here
    
                Case "Button2"
                         ' Do stuff here
            End Select
    
        End Sub
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Create Buttons dynamically

    You can use OOP tecniques to solve that. Create a custom button control. So, create a class. Inherit Button.
    Create a property called ID As Integer

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Create Buttons dynamically

    Right, but this ways ir doesnt work..
    Cause I need the name of the button is the ID of the record at the database,
    Otherwise, I lost the identity of the button..

    Doing this way, I dont know at the coding time, wich name my button will have

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Create Buttons dynamically

    Or it's better changing the paradigm and create objects?
    I mean, each record are one object?

  8. #8
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,623

    Re: Create Buttons dynamically

    Quote Originally Posted by helderjsd View Post
    Right, but this ways ir doesnt work..
    Cause I need the name of the button is the ID of the record at the database,
    Otherwise, I lost the identity of the button..

    Doing this way, I dont know at the coding time, which name my button will have
    Err no nothing about my approach stops you from knowing which button is which, remember i am providing examples which require you to think and do some extra work to complete.

    Just use your head a bit;

    When you are creating the buttons in a loop of some sort (probably a loop of a set of records from a database by the sound of it) all you need to do it set the Name of each button to your database id record.

    Then each button had the ID of the database record !!!

    You can use OOP tecniques to solve that. Create a custom button control. So, create a class. Inherit Button.
    Create a property called ID As Integer
    While you could do that it just isn't required, you have a name property of the button just use that, and set it to the ID.

    helderjsd do you know how to work with databases?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  9. #9
    Lively Member
    Join Date
    Aug 2014
    Posts
    77

    Re: Create Buttons dynamically

    If you are using data from a datatable, then you could us that data and assign it to the button using the tag. Like NeedSomeAnswers said, all it is is a loop, this would go through say each row and assign the data to the tag. You can also read the data off of the tag that is attached to a specific button too which could be useful.
    This is an example from a project I am working on:
    Code:
     For Each c As Column In flx.Cols
    
                    If c.Name = "Add" Then
    
                        For Each r As Row In flx.Rows
    
                            If r.Index > 0 Then
    
                                Dim btn As New Button
                                Dim rowData As Part = r.DataSource
    
                                r(0) = r.Index
    
                                btn.BackColor = SystemColors.Control
                                btn.Text = "Add Option"
                                btn.Tag = rowData
    
                                AddHandler btn.Click, AddressOf Button_Click
                                _buttonList.Add(New HostedControl(flx, btn, r.Index, c.Index))
    
                            End If
    
                        Next
    
                    End If
                Next
    I am using Component One extras, but the principle are still the same.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,110

    Re: Create Buttons dynamically

    Quote Originally Posted by helderjsd View Post
    ...
    Cause I need the name of the button is the ID of the record at the database,
    ...
    You could also use the name of the button clicked, rather than the tag.
    I would hook all the buttons to the same event handler, and then use the name to do what you need, if the name will suffice.
    Otherwise, place the needed information in the tag, as already suggested and use that.

    It looks like the content of the event handler where you get the name or tag hasn't been shown.
    <edit> I was wrong. NeedSomeAnswers gave an example in post #4 <edit>

    Here is a bit I just copied from an existing project.
    So where I directCast to a radio button, you would directcast to a button.
    In this case I'm using rb.Name, but I could use rb.Name as well.
    And these radio buttons were not dynamically created, which is why there are handles clauses for three, which you wouldn't have. You would be assigning the handler in code, not the IDE.
    Code:
      Private Sub rbVehHealth_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbVehHealthy.CheckedChanged,
                                                                                                     rbVehDegraded.CheckedChanged,
                                                                                                     rbVehFailed.CheckedChanged
        Dim rb As RadioButton = DirectCast(sender, RadioButton)
        If VehicleList.ContainsKey(SelectedVehicle) Then
          SyncLock VehListLock
            Dim veh As Vehicle = VehicleList(SelectedVehicle)
            veh.Cmd_Equip_Health(0) = CInt(rb.Tag)
          End SyncLock
        End If
      End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width