PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] VS2015 need to create 21 buttons with 1 array-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] VS2015 need to create 21 buttons with 1 array

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,703

    Resolved [RESOLVED] VS2015 need to create 21 buttons with 1 array

    hey,
    i created 21 buttons in 3 diffrent arrays and variables
    is it possible to create in 1 time the button controls?
    this is what i did
    Code:
        Dim BttnsFirstRow(6) As Button ' first row buttons 
    
    
           'first row buttons ============
    
            Dim A As Integer
            For A = 0 To BttnsFirstRow.Length - 1
                BttnsFirstRow(A) = New Button
                BttnsFirstRow(A).Top = 0
                BttnsFirstRow(A).Width = 100
                BttnsFirstRow(A).Height = 43
                BttnsFirstRow(A).Left = 98 * A
                BttnsFirstRow(A).Cursor = Cursors.Hand
                BttnsFirstRow(A).Enabled = False
                Controls.Add(BttnsFirstRow(A))
            Next
    but the catch is that the 8 button should be under the first 1
    and the 15 button under the 8 button
    well you get the idea
    tnx in advanced
    salsa
    Last edited by salsa31; Yesterday at 02:12 AM.

  2. #2

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,703

    Re: VS2015 need to create 21 buttons with 1 array

    never mind
    i used a select case

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: [RESOLVED] VS2015 need to create 21 buttons with 1 array

    I have to question why you're creating these controls in code in the first place, given that you know everything about them ahead of time. Normally, you only create controls in code if you don't know how many there will be. Even if it's the case that you may or may not need the entire group, you should probably create a user control containing all those Buttons and then just create one instance of that at run time.

    Either way, you should be using a TableLayoutPanel. You create one with three rows and seven columns and then just add the Buttons to it and it will put everything in the right place. Setting the location and size manually like that is just a waste of time and code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,703

    Re: [RESOLVED] VS2015 need to create 21 buttons with 1 array

    I have to question why you're creating these controls in code in the first place
    because i need it to be an array
    and i dont know any other way

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: [RESOLVED] VS2015 need to create 21 buttons with 1 array

    Quote Originally Posted by salsa31 View Post
    because i need it to be an array
    and i dont know any other way
    That makes no sense at all. An array is an array. You create one in exactly the same all the time. Why do you think that populating an array is different if you use Buttons created in code as opposed to Buttons created in the designer? They're still Buttons. Here, I'm creating an array containing Buttons:
    vb.net Code:
    1. Dim buttons = {Button1, Button2, Button3}
    If you added Buitton1, Button2 and Button3 to the form in code then the array contains Buttons that were created in the designer.

    It seems to me that you are still hung up on the difference between control arrays in VB6 and arrays of controls in VB.NET. You should just take a step back and spend some time thinking about what an array actually is. You can really just think of an array as an object that groups multiple variables together, with each element of the array effectively being a variable. If you were to declare a variable of type Button in your code, do you think that there would any issue assigning a new Button created in code or an existing Button created in the designer to it? I would hope not:
    vb.net Code:
    1. Dim btn1 As Button = New Button
    2. Dim btn2 As Button = Me.Button1 'Added to form in designer
    Likewise, assigning newly created Buttons or existing Buttons to array elements is no different:
    vb.net Code:
    1. Dim btns1(2) As Button
    2.  
    3. btns1(0) = New Button
    4. btns2(1) = New Button
    5. btns3(2) = New Button
    6.  
    7. Dim btns2(2) As Button
    8.  
    9. btns2(0) = Me.Button1
    10. btns2(1) = Me.Button2
    11. btns2(2) = Me.Button3
    or:
    vb.net Code:
    1. Dim btns1 = {New Button, New Button, New Button}
    2. Dim btns2 = {Me.Button1, Me.Button2, Me.Button3}
    As you can see, populating an array is basically the same operation regardless. You simply assign something to each element, whether it's an existing control or a newly created one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,423

    Re: [RESOLVED] VS2015 need to create 21 buttons with 1 array

    Quote Originally Posted by salsa31 View Post
    because i need it to be an array
    and i dont know any other way
    Hi Salsa,
    in Post#3 you got a hint = TableLayoutPanel, I don't want to be picky but you should
    read a bit more on they way .Net changed in regards to VB6

    well anyway here a sample with the TableLayoutPanel
    the code will add everything to the Form
    Code:
    Public Class Form2
    
        Private numberOfItems As Integer = 21I 'create 21 
    
        Private myLayOutPanel As New FlowLayoutPanel With _
        {.FlowDirection = FlowDirection.LeftToRight, _
         .AutoScroll = True, .Dock = DockStyle.Fill}
    
        Private TableLayoutPanel1 As New TableLayoutPanel With _
        {.Dock = DockStyle.Fill, .ColumnCount = 7, .RowCount = 3} 'Set columns, rows
    
        Private SubmitBtn As New Button With _
        {.Text = "Submit", .Anchor = AnchorStyles.Right}
    
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            AddHandler Me.SubmitBtn.Click, AddressOf SubmitBtnClicked
    
            For i As Integer = 1I To numberOfItems
                Dim rRadio As New RadioButton With {.Text = "R " & i, .Tag = i, _
                                .Checked = False, _
                                .Enabled = True, _
                                .AutoCheck = True, _
                                .Height = 45, _
                                .Width = 55, .Appearance = Appearance.Button}
    
                AddHandler rRadio.Click, AddressOf rButtonClicked
                Me.myLayOutPanel.Controls.Add(rRadio)
            Next
    
            Me.TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100.0!))
            Me.TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Percent, 100.0!))
            Me.TableLayoutPanel1.RowStyles.Add(New RowStyle())
            Me.TableLayoutPanel1.Controls.Add(Me.myLayOutPanel, 0I, 0I)
            Me.TableLayoutPanel1.Controls.Add(Me.SubmitBtn, 0I, 1I)
            Me.Controls.Add(Me.TableLayoutPanel1)
        End Sub
    
        Private Sub rButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
            Dim rButtonSelected As RadioButton
            If TypeOf sender Is RadioButton Then rButtonSelected = DirectCast(sender, RadioButton) Else Exit Sub
    
            For Each ctrl As Control In Me.myLayOutPanel.Controls
                If TypeOf ctrl Is RadioButton Then
                    Dim ctrlRadio As RadioButton = DirectCast(ctrl, RadioButton)
                    If CInt(ctrl.Tag) <> CInt(rButtonSelected.Tag) Then
                        RemoveHandler ctrlRadio.Click, AddressOf Me.rButtonClicked
                        ctrlRadio.Checked = False
                        AddHandler ctrlRadio.Click, AddressOf rButtonClicked
                    End If
                End If
            Next
        End Sub
    
        Private Sub SubmitBtnClicked(ByVal sender As Object, ByVal e As EventArgs)
            For Each ctrl As Control In Me.myLayOutPanel.Controls
                If TypeOf ctrl Is RadioButton Then
                    Dim ctrlRadio As RadioButton = DirectCast(ctrl, RadioButton)
                    If ctrlRadio.Checked Then
                        MsgBox(ctrlRadio.Tag)
                        ctrlRadio.Checked = False
                        Exit For
                    End If
                End If
            Next
        End Sub
    End Class
    HTH
    Last edited by ChrisE; Yesterday at 04:22 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,703

    Re: [RESOLVED] VS2015 need to create 21 buttons with 1 array

    wow jmcilhinney you are so right
    what was i thinking?
    thanks for clarifying that for me

    tnx chris

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