Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Refreshig a form with dynamic buttons.

Refreshig a form with dynamic buttons.

Scheduled Pinned Locked Moved Visual Basic
databasehelp
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rudyvb08
    wrote on last edited by
    #1

    Hello all! So I'm having a little problem refreshing dynamic buttons on a form. I'm slowly making progress, but I want to make sure I'm doing it the right way, or maybe there is a better way. So here is some code. So this gets the data from the database so we know what buttons to load, etc,

    Private Function GetButtonData() As List(Of ButtonInfo)
    Dim constr As New SqlConnection(PVDBConn)
    'Dim PVDBConn As New SqlConnection("")
    'cmdUpd = New SqlCommand("SelVisName", constr)
    cmdUpd.CommandType = CommandType.StoredProcedure

        Dim lst As New List(Of ButtonInfo)
    
        Dim da As New SqlDataAdapter("SELECT VisId, VisName, VisEnabled, VisFlag, visPort, VisCpuName FROM tblVis ORDER BY VisId", constr)
    
        Dim t As New DataTable
    
        Try
            constr.Open()
            'PVDBConn.Open()
    
            da.Fill(t)
    
            For Each r As DataRow In t.Rows
    
    
                lst.Add(New ButtonInfo(CInt(r("VisId")), CStr(r("VisName")), CBool(r("VisEnabled")), CBool(r("VisFlag")), CStr(r("VisPort")), CStr(r("VisCpuName"))))
    
            Next
    

    Here is the code that will load the buttons on the form. I have this procedure in the OnLoad event of the form. This is the first form and the main form of the program.

    Private Sub GetButtonOnScreen()
    GetButtonData()

        Buttons = GetButtonData()
    
        For Each bi As ButtonInfo In Buttons
    
            If bi.Enabled Then
                'Dim prtLabel As New Label,
    
    
                ' MsgBox(prtLabel.Text.ToString)
                Dim newbutton As New Button
    
    
    
                'this is were it determines the color is red or blue
                If bi.HasChanges Then
                    newbutton.Image = My.Resources.pvmetal2red
                Else
                    newbutton.Image = My.Resources.pvmetal3
    
    
                End If
                newbutton.Text = bi.VisText
    
                Dim MinFont As New Font("arial", 12, FontStyle.Bold)
                newbutton.Font = MinFont
    
    
                newbutton.TextAlign = ContentAlignment.MiddleCenter
    
                newbutton.Tag = bi
    
                newbutton.Width = 102
    
    
                newbutton.Height = 42
    
                newbutton.BackColor = Color.AliceBlue
    
                'Plus 80 for Top
    
                newbutton.Top = 80 '+ (0 + bi.VisId)
    
                newbutton.Left = -95 + (100 \* bi.VisId)
    
    
                If newbutton.Left >= 906 Then
    
    C I 2 Replies Last reply
    0
    • R Rudyvb08

      Hello all! So I'm having a little problem refreshing dynamic buttons on a form. I'm slowly making progress, but I want to make sure I'm doing it the right way, or maybe there is a better way. So here is some code. So this gets the data from the database so we know what buttons to load, etc,

      Private Function GetButtonData() As List(Of ButtonInfo)
      Dim constr As New SqlConnection(PVDBConn)
      'Dim PVDBConn As New SqlConnection("")
      'cmdUpd = New SqlCommand("SelVisName", constr)
      cmdUpd.CommandType = CommandType.StoredProcedure

          Dim lst As New List(Of ButtonInfo)
      
          Dim da As New SqlDataAdapter("SELECT VisId, VisName, VisEnabled, VisFlag, visPort, VisCpuName FROM tblVis ORDER BY VisId", constr)
      
          Dim t As New DataTable
      
          Try
              constr.Open()
              'PVDBConn.Open()
      
              da.Fill(t)
      
              For Each r As DataRow In t.Rows
      
      
                  lst.Add(New ButtonInfo(CInt(r("VisId")), CStr(r("VisName")), CBool(r("VisEnabled")), CBool(r("VisFlag")), CStr(r("VisPort")), CStr(r("VisCpuName"))))
      
              Next
      

      Here is the code that will load the buttons on the form. I have this procedure in the OnLoad event of the form. This is the first form and the main form of the program.

      Private Sub GetButtonOnScreen()
      GetButtonData()

          Buttons = GetButtonData()
      
          For Each bi As ButtonInfo In Buttons
      
              If bi.Enabled Then
                  'Dim prtLabel As New Label,
      
      
                  ' MsgBox(prtLabel.Text.ToString)
                  Dim newbutton As New Button
      
      
      
                  'this is were it determines the color is red or blue
                  If bi.HasChanges Then
                      newbutton.Image = My.Resources.pvmetal2red
                  Else
                      newbutton.Image = My.Resources.pvmetal3
      
      
                  End If
                  newbutton.Text = bi.VisText
      
                  Dim MinFont As New Font("arial", 12, FontStyle.Bold)
                  newbutton.Font = MinFont
      
      
                  newbutton.TextAlign = ContentAlignment.MiddleCenter
      
                  newbutton.Tag = bi
      
                  newbutton.Width = 102
      
      
                  newbutton.Height = 42
      
                  newbutton.BackColor = Color.AliceBlue
      
                  'Plus 80 for Top
      
                  newbutton.Top = 80 '+ (0 + bi.VisId)
      
                  newbutton.Left = -95 + (100 \* bi.VisId)
      
      
                  If newbutton.Left >= 906 Then
      
      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      You using the button_id to determine the left position of your button... Thats really cruel ... if I may say so. On the other hand you can do your   "If newbutton.Left >= 906 Then" in a loop, but I think you should have a break and think about what way you should choose to determine the left position. [edit] I meant left position not top. [/edit]

      modified on Thursday, October 29, 2009 11:11 AM

      1 Reply Last reply
      0
      • R Rudyvb08

        Hello all! So I'm having a little problem refreshing dynamic buttons on a form. I'm slowly making progress, but I want to make sure I'm doing it the right way, or maybe there is a better way. So here is some code. So this gets the data from the database so we know what buttons to load, etc,

        Private Function GetButtonData() As List(Of ButtonInfo)
        Dim constr As New SqlConnection(PVDBConn)
        'Dim PVDBConn As New SqlConnection("")
        'cmdUpd = New SqlCommand("SelVisName", constr)
        cmdUpd.CommandType = CommandType.StoredProcedure

            Dim lst As New List(Of ButtonInfo)
        
            Dim da As New SqlDataAdapter("SELECT VisId, VisName, VisEnabled, VisFlag, visPort, VisCpuName FROM tblVis ORDER BY VisId", constr)
        
            Dim t As New DataTable
        
            Try
                constr.Open()
                'PVDBConn.Open()
        
                da.Fill(t)
        
                For Each r As DataRow In t.Rows
        
        
                    lst.Add(New ButtonInfo(CInt(r("VisId")), CStr(r("VisName")), CBool(r("VisEnabled")), CBool(r("VisFlag")), CStr(r("VisPort")), CStr(r("VisCpuName"))))
        
                Next
        

        Here is the code that will load the buttons on the form. I have this procedure in the OnLoad event of the form. This is the first form and the main form of the program.

        Private Sub GetButtonOnScreen()
        GetButtonData()

            Buttons = GetButtonData()
        
            For Each bi As ButtonInfo In Buttons
        
                If bi.Enabled Then
                    'Dim prtLabel As New Label,
        
        
                    ' MsgBox(prtLabel.Text.ToString)
                    Dim newbutton As New Button
        
        
        
                    'this is were it determines the color is red or blue
                    If bi.HasChanges Then
                        newbutton.Image = My.Resources.pvmetal2red
                    Else
                        newbutton.Image = My.Resources.pvmetal3
        
        
                    End If
                    newbutton.Text = bi.VisText
        
                    Dim MinFont As New Font("arial", 12, FontStyle.Bold)
                    newbutton.Font = MinFont
        
        
                    newbutton.TextAlign = ContentAlignment.MiddleCenter
        
                    newbutton.Tag = bi
        
                    newbutton.Width = 102
        
        
                    newbutton.Height = 42
        
                    newbutton.BackColor = Color.AliceBlue
        
                    'Plus 80 for Top
        
                    newbutton.Top = 80 '+ (0 + bi.VisId)
        
                    newbutton.Left = -95 + (100 \* bi.VisId)
        
        
                    If newbutton.Left >= 906 Then
        
        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #3

        Ouch... Ok, let me try to understand this... You have a series of buttons defined in your database, and you're trying to lay them out in a left-to-right, top-to-bottom format... I think you need to come at this from a different angle. Let's set aside the actual coordinates for a second, and think of how you want to lay it out.

        0 1 2 3 4 5 6 7 8 9
        10 11 12 13 14 15 16 17 18 19
        20 21 22 23 24 25 26 27 28 29

        Something this simple has to have a pattern, and that's what algorithms are all about. Now, if you're doing this in a static grid (As in all the controls are the same size, and all of the rows have the same number of controls), this becomes REALLY simple. Instead of using the "visid" to calculate the "Left", use it to figure out what row and column you're in. Row Number = ID / Number of Controls in Row, rounded down Column Number = ID Mod Number of Controls in Row (Mod = modulus operator. A Mod B = Remainder when calculating A/B) Example, using above grid: ID = 17 Column Count (Number of controls in row) = 10 Row number = 17 / 10 = 1.7, rounded down to 1 -- Row 1 (Second row, since first is zero) Column number = 17 Mod 10 = 7 -- Column 7 (8th column) See where I'm going with this?

        Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

        R 1 Reply Last reply
        0
        • I Ian Shlasko

          Ouch... Ok, let me try to understand this... You have a series of buttons defined in your database, and you're trying to lay them out in a left-to-right, top-to-bottom format... I think you need to come at this from a different angle. Let's set aside the actual coordinates for a second, and think of how you want to lay it out.

          0 1 2 3 4 5 6 7 8 9
          10 11 12 13 14 15 16 17 18 19
          20 21 22 23 24 25 26 27 28 29

          Something this simple has to have a pattern, and that's what algorithms are all about. Now, if you're doing this in a static grid (As in all the controls are the same size, and all of the rows have the same number of controls), this becomes REALLY simple. Instead of using the "visid" to calculate the "Left", use it to figure out what row and column you're in. Row Number = ID / Number of Controls in Row, rounded down Column Number = ID Mod Number of Controls in Row (Mod = modulus operator. A Mod B = Remainder when calculating A/B) Example, using above grid: ID = 17 Column Count (Number of controls in row) = 10 Row number = 17 / 10 = 1.7, rounded down to 1 -- Row 1 (Second row, since first is zero) Column number = 17 Mod 10 = 7 -- Column 7 (8th column) See where I'm going with this?

          Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

          R Offline
          R Offline
          Rudyvb08
          wrote on last edited by
          #4

          Hi Guys! Wow! I wasn't even thinking that was my problem. But I'll do anything to make it better. So Ian, ID=17 refers to how many rows I will have. In my database, I have an ENABLED column. So if there is a TRUE value, than the button will be built. Your right about the layout, that's exactly how I will have it. But I also need to have it so if button 23, 24, 25 is not loaded, there will be a empty space there. So each button is always locked to it's location, so if I want two groups, I can have 1 to 20 and then 40 to 60. And I have a big gap between 20 to 40. Thanks for your help! Rudy

          I 1 Reply Last reply
          0
          • R Rudyvb08

            Hi Guys! Wow! I wasn't even thinking that was my problem. But I'll do anything to make it better. So Ian, ID=17 refers to how many rows I will have. In my database, I have an ENABLED column. So if there is a TRUE value, than the button will be built. Your right about the layout, that's exactly how I will have it. But I also need to have it so if button 23, 24, 25 is not loaded, there will be a empty space there. So each button is always locked to it's location, so if I want two groups, I can have 1 to 20 and then 40 to 60. And I have a big gap between 20 to 40. Thanks for your help! Rudy

            I Offline
            I Offline
            Ian Shlasko
            wrote on last edited by
            #5

            Well, if you use the VisId field as the ID in the calculation I posted, that's exactly what you'll get. Of course, if your buttons start at 1, you might want to subtract 1 so it starts at the top-left corner. Since the formulas are based on the ID, and not just counting through as they go, any missing IDs will cause it to skip a spot.

            Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

            R 1 Reply Last reply
            0
            • I Ian Shlasko

              Well, if you use the VisId field as the ID in the calculation I posted, that's exactly what you'll get. Of course, if your buttons start at 1, you might want to subtract 1 so it starts at the top-left corner. Since the formulas are based on the ID, and not just counting through as they go, any missing IDs will cause it to skip a spot.

              Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

              R Offline
              R Offline
              Rudyvb08
              wrote on last edited by
              #6

              Thanks Ian! I'll give it a shot. May have to bug you if I get stuck writing out the code. Thanks again! Rudy :-D

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups