Results 1 to 12 of 12

Thread: Error with dynamically created buttons array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    226

    Error with dynamically created buttons array

    Dear all,
    Today I have another issue that I tried to figure out. I have no idea where I did a mistake. Code is below. After creating new buttons array the buttons are not exactly fitting the borders of frmMain.Width and frmMain.Height. In form area I need zero tolerance fitting. I could not solve this problem possibly with your assist I would be much appreciated.
    Code:
    Public Class frmMain
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim btns As New BtnClass(10, 10)
        End Sub
    
        Class BtnClass
            Dim BtnArray As Button(,) = {}
            Dim BtnText As String(,) = {}
    
            Sub New(pRow As Integer, pCol As Integer)
                Dim TotalWidth As Integer = 0
                Dim TotalHeight As Integer = 0
                Dim ButtonSize As Integer = 50
                Dim NewBtn As Button = Nothing
                BtnText = New String(pRow - 1, pCol - 1) {}
                BtnArray = New Button(pRow - 1, pCol - 1) {}
    
                For r As Integer = 0 To UBound(BtnArray, 1)
                    For c As Integer = 0 To UBound(BtnArray, 2)
                        NewBtn = New Button
                        NewBtn.FlatStyle = FlatStyle.Flat
                        NewBtn.Top = r * ButtonSize
                        NewBtn.Left = c * ButtonSize
                        NewBtn.Width = ButtonSize
                        NewBtn.Height = ButtonSize
                        NewBtn.Text = ""
                        NewBtn.Name = Trim(Str(r)) + "-" + Trim(Str(c))
                        BtnArray(r, c) = NewBtn
    
                        TotalWidth = NewBtn.Left + NewBtn.Width
                        TotalHeight = NewBtn.Top + NewBtn.Height
                        frmMain.Controls.Add(NewBtn)
                    Next
                Next
                frmMain.Width = TotalWidth
                frmMain.Height = TotalHeight
            End Sub
    
    End Class
    End Class

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Error with dynamically created buttons array

    What does your form look like, and what do you want it to look like?

    It looks fine for me, it depends on what you mean by zero tolerance fitting.
    I can see a single pixel black line around the whole form (I assume you're using a borderless form), which is what you would expect to see around the outside edge of packed buttons that have a single pixel wide border.
    The inner lines are two pixels wide, because you have two single pixel border buttons adjacent to each other, so you see both borders next to each other, thus two pixels wide border, one pixel belonging to one button, the second pixel the other.

    Do you want the buttons to overlap so that you only see a single pixel between button faces, i.e. the borders overlap between two buttons so appear to be only one border?
    Or do you want to offset the top and left by one pixel and make the form three pixels wider and taller so you can paint a one pixel border around the buttons so that the outside borders of the group of buttons looks two pixels wide to match the interior?

    Or are you having a different problem, e.g. perhaps you're not using a borderless form, which you need to capture and show us so we know what you're talking about?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    226

    Re: Error with dynamically created buttons array

    You are right passel when I modify the FormBorderStyle to none I have no problem. But when I prefer FixedToolWindow I am getting this problem. The image looks like described below . Possibly I have forgotten the absolute coordinates and relative coordinates for the form to scale. Sorry that. Any clue for this issue. I started to search internet at the same time waiting response from you. Possibly you can solve this issue before me. I would be much appreciated…
    Regards
    algea


  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Error with dynamically created buttons array

    Well, the problem there is you don't compensate for the borders. The size includes the border, and the clientsize (the area inside the borders) is obviously smaller than the size of the form as a whole.
    You could just take the difference between the clientsize height and the height you want, and the clientsize width and the width you want, and then add the delta to the size of the form.
    Probably something like
    Code:
                frmMain.Width += (TotalWidth - frmMain.ClientSize.Width)
                frmMain.Height += (TotalHeight - frmMain.ClientSize.Height)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    226

    Re: Error with dynamically created buttons array

    Thanks passel for your feedback. My code is being shaped now. But I have another issue now. Could you please take a look at my code. The dynamically code creation is not working now previously on my code the code was working clear but now have a possible glitch. Sorry for my new inconvenience. Any assist I would be much appreciated.
    Regards
    algea
    Code:
    'frmMain
    Public Class frmMain
    ...
    Private Sub cmdOpenNewGame_Click(sender As Object, e As EventArgs) Handles cmdOpenNewGame.Click
            Dim myGame As New frmGame
            Dim newPO As ProgramOptions
            PO.GameMatrixBounds = txtGameMatrix.Text
            newPO = myGame.myShowDialog(PO)
            myGame.Dispose()
        End Sub
    Class ProgramOptions
            Public GameMatrixBounds As String
            Public ClickCount As Integer
            Public GameTime As Integer
        End Class
    End Class
    
    
    'frmGame
    Imports System.ComponentModel
    
    Public Class frmGame
        Dim gFlagFormisWorking As Boolean = False
        Dim gDialogSelection As Integer = 0
        Dim PO As frmMain.ProgramOptions = Nothing
        Dim gButtonArray As BtnClass
    
        Private Sub frmGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            gButtonArray = New BtnClass(10, 10)
        End Sub
    
        Public Function myShowDialog(ByVal pPO As frmMain.ProgramOptions) As frmMain.ProgramOptions
            PO = pPO
            LoadProgramOptions()
            gFlagFormisWorking = True
            Me.Show()
            Do While gFlagFormisWorking = True
                Application.DoEvents()
                System.Threading.Thread.Sleep(10) 'new line so that the application have no lags
            Loop
            Return PO
        End Function
    
        Private Sub frmOptions_Closing(sender As Object, e As CancelEventArgs) Handles MyBase.Closing
            gDialogSelection = 0
            gFlagFormisWorking = False
        End Sub
    
        Private Function LoadProgramOptions() As Integer
            Dim retcode As Integer = 0
    
            Return retcode
        End Function
    
        Function SaveProgramOptions() As Integer
            Dim retcode As Integer = 0
            Return retcode
        End Function
    
        Class BtnClass
            Dim BtnArray As Button(,) = {}
            Dim BtnText As String(,) = {}
            Dim WaitThread As System.Threading.Thread
            Dim WaitFlag As Integer = 0
    
            Sub New(pRow As Integer, pCol As Integer)
                Dim TotalWidth As Integer = 0
                Dim TotalHeight As Integer = 0
                Dim ButtonSize As Integer = 50
                Dim NewBtn As Button = Nothing
                BtnText = New String(pRow - 1, pCol - 1) {}
                BtnArray = New Button(pRow - 1, pCol - 1) {}
    
                For r As Integer = 0 To UBound(BtnArray, 1)
                    For c As Integer = 0 To UBound(BtnArray, 2)
                        NewBtn = New Button
                        NewBtn.Top = r * ButtonSize
                        NewBtn.Left = c * ButtonSize
                        NewBtn.Width = ButtonSize
                        NewBtn.Height = ButtonSize
                        NewBtn.Text = ""
                        NewBtn.Name = Trim(Str(r)) + "-" + Trim(Str(c))
                        BtnArray(r, c) = NewBtn
                        TotalWidth = NewBtn.Left + NewBtn.Width
                        TotalHeight = NewBtn.Top + NewBtn.Height
                        frmGame.Controls.Add(NewBtn)
                    Next
                Next
                frmGame.Width = TotalWidth
                frmGame.Height = TotalHeight
                frmGame.Width += (TotalWidth - frmGame.ClientSize.Width)
                frmGame.Height += (TotalHeight - frmGame.ClientSize.Height)
            End Sub
        End Class
    End Class

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Error with dynamically created buttons array

    I don't see why you're creating the Buttons in code in the first place. Just add a TableLayoutPanel to your form, add the appropriate number of rows and columns, add a Button to each cell and then set all the Dock properties to Fill. You'll need to set the Padding to zero on all the Buttons too. Done. No code at all. Everything will even resize with the form, if you need that.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    226

    Re: Error with dynamically created buttons array

    Thanks jmcilhinney for your assist. This is my way for creating controllers dynamically. I wonder why frmGame.Controls.Add(NewBtn) have no effect when I call frmGame. I only want to know. I have restricted time any help would be much appreciated.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Error with dynamically created buttons array

    My guess is that it's adding it to the default instance of the form, not the current instance. Change frmGame.Controls.Add(NewBtn) to Me.Controls.Add(NewBtn)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Error with dynamically created buttons array

    My thought earlier was that it is referring to the default instance as well, but the code is inside the btnClass Class, so Me refers to an instance of the btnClass, not a Form so using Me.Controls is not applicable. I find the code convoluted. The instance of frmGame is created in frmMain, but not passed where needed.
    I don't understand the need for all these intertwined classes, but if you pass the instance of the form through to the myShowDialog function, and then cast the sender object in the frmGame's Load event on to the BtnClass so it can add the buttons to the correct form it will work.

    I've always depended on fairly simple programming methods (for over 40 years), so don't understand how new programmers come up with such convoluted code. I guess it is because the newer languages make it easier to make things complicated compared to what I started with.
    Anyway, here's a patched version showing passing the form instance as I mentioned above.
    Code:
    'The main form code (I added PO to get the reference satisfied and commented out the txtbox reference since It didn't seem needed for testing
    Public Class Form1
      Private PO As New ProgramOptions
    
      Private Sub cmdOpenNewGame_Click(sender As Object, e As EventArgs) Handles cmdOpenNewGame.Click
        Dim myGame As New frmGame
        Dim newPO As ProgramOptions
        '  PO.GameMatrixBounds = txtGameMatrix.Text
        newPO = myGame.myShowDialog(PO, myGame)  'pass the form created to myShowDialog so it can be shown
        myGame.Dispose()
      End Sub
      Class ProgramOptions
        Public GameMatrixBounds As String
        Public ClickCount As Integer
        Public GameTime As Integer
      End Class
    End Class
    
    'The frmGame.vb code
    Imports System.ComponentModel
    
    Public Class frmGame
      Dim gFlagFormisWorking As Boolean = False
      Dim gDialogSelection As Integer = 0
      Dim PO As Form1.ProgramOptions = Nothing
      Dim gButtonArray As BtnClass
    
      Private Sub frmGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        gButtonArray = New BtnClass(10, 10, DirectCast(sender, frmGame))   'cast the form being loaded (the once created, i.e. myGame) so buttons can be added
      End Sub
    
      Public Function myShowDialog(ByVal pPO As Form1.ProgramOptions, frm As frmGame) As Form1.ProgramOptions
        PO = pPO
        LoadProgramOptions()
        gFlagFormisWorking = True
        frm.Show()
        Do While gFlagFormisWorking = True
          Application.DoEvents()
          System.Threading.Thread.Sleep(10) 'new line so that the application have no lags
        Loop
        Return PO
      End Function
    
      Private Sub frmOptions_Closing(sender As Object, e As CancelEventArgs) Handles MyBase.Closing
        gDialogSelection = 0
        gFlagFormisWorking = False
      End Sub
    
      Private Function LoadProgramOptions() As Integer
        Dim retcode As Integer = 0
    
        Return retcode
      End Function
    
      Function SaveProgramOptions() As Integer
        Dim retcode As Integer = 0
        Return retcode
      End Function
    
      Class BtnClass
        Dim BtnArray As Button(,) = {}
        Dim BtnText As String(,) = {}
        Dim WaitThread As System.Threading.Thread
        Dim WaitFlag As Integer = 0
    
        Sub New(pRow As Integer, pCol As Integer, frm As frmGame)
          Dim TotalWidth As Integer = 0
          Dim TotalHeight As Integer = 0
          Dim ButtonSize As Integer = 50
          Dim NewBtn As Button = Nothing
          BtnText = New String(pRow - 1, pCol - 1) {}
          BtnArray = New Button(pRow - 1, pCol - 1) {}
    
          For r As Integer = 0 To UBound(BtnArray, 1)
            For c As Integer = 0 To UBound(BtnArray, 2)
              NewBtn = New Button
              NewBtn.Top = r * ButtonSize
              NewBtn.Left = c * ButtonSize
              NewBtn.Width = ButtonSize
              NewBtn.Height = ButtonSize
              NewBtn.Text = ""
              NewBtn.Name = Trim(Str(r)) + "-" + Trim(Str(c))
              BtnArray(r, c) = NewBtn
              TotalWidth = NewBtn.Left + NewBtn.Width
              TotalHeight = NewBtn.Top + NewBtn.Height
              frm.Controls.Add(NewBtn)
            Next
          Next
          frm.Width = TotalWidth
          frm.Height = TotalHeight
          frm.Width += (TotalWidth - frm.ClientSize.Width)
          frm.Height += (TotalHeight - frm.ClientSize.Height)
        End Sub
      End Class
    End Class

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Error with dynamically created buttons array

    Yeah, I didn't see that it was in a bnClass class.... what is it doing in there? Makes no sense.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Error with dynamically created buttons array

    Hi,

    I have to agree with passel,

    add 50 Buttons in a simple way
    Code:
    Public Class Form4
    
        Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer
            For i = 1 To 50
    
                Dim btn As New Button
                AddHandler btn.Click, AddressOf Clickme
    
                btn.Width = 30
                btn.Height = 30
                btn.Text = i
                btn.Visible = True
                btn.Tag = "Button " & i
                FlowLayoutPanel1.Controls.Add(btn)
            Next
        End Sub
    
        Private Sub Clickme(ByVal sender As Object, ByVal e As EventArgs)
            Dim btn As Button
            btn = CType(sender, Button)
            Dim str As String = btn.Tag
            MessageBox.Show(str)
        End Sub
    End Class
    regards
    Chris
    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.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    226

    Re: Error with dynamically created buttons array

    Thank passel for your response. And thanks all for the tolerance and understanding to solve this puzzle. I am about to finish my project now
    Regards
    algea

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