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] how to inspect a generated object.-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] how to inspect a generated object.

  1. #1

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,803

    Resolved [RESOLVED] how to inspect a generated object.

    Hi,

    I can find plenty of examples of how to 'Click' objects generated at runtime, but not how to inspect their attributes... I'm generating a load of PictureBoxes and need to know their size without having to click one. I can of course fix their size when I generate 'em, but having changed the form to 'Maximum' (Form1_Load), everything in the TableLayoutPanel changes size. (Dock set to 'Fill').

    I ought to know how to do this, I'm sure I've done it before but can't find where.


    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: how to inspect a generated object.

    I'm probably misunderstanding, but I read your post twice and it appears to me the solution is simply to reference the control and return X attribute?

    Code:
    Form Load
    
        MsgBox(Picturebox1.width)
    
    End sub
    That will show the width of Picturebox1 in a messagebox.

    If you want to know the size of ALL your pictureboxes without typing in every name by hand, then that is another matter.
    I don't work a lot with winforms anymore, so I can't supply that code on the go (it used to be soooo easy with control arrays back in VB6 sigh).

    Here's an example I found looping through all textboxes inside a specified panel:

    Code:
    Dim ctrl As Control
      For Each ctrl In Panel1.Controls
      If (ctrl.GetType() Is GetType(TextBox)) Then
          Dim txt As TextBox = CType(ctrl, TextBox)
          txt.BackColor = Color.LightYellow
      End If

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: how to inspect a generated object.

    You can use the PropertyGrid Control to view/modify a control's properties. Here is a quick example. Just create a new WinForm project and use this for the code.

    VB.Net Code:
    1. Public Class Form1
    2.     Private tlp As New TableLayoutPanel
    3.  
    4.     Public Sub New()
    5.         InitializeComponent()
    6.         SetupDemo()
    7.     End Sub
    8.  
    9.     Private Sub SetupDemo()
    10.         Me.ClientSize = New Size(400, 400)
    11.         With tlp
    12.             .ColumnCount = 4
    13.             .ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    14.             .ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    15.             .ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    16.             .ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    17.             .Location = New System.Drawing.Point(64, 48)
    18.  
    19.             .RowCount = 4
    20.             .RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    21.             .RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    22.             .RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    23.             .RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
    24.  
    25.             .CellBorderStyle = TableLayoutPanelCellBorderStyle.InsetDouble
    26.             .Dock = DockStyle.Fill
    27.             .Parent = Me
    28.         End With
    29.         For r As Int32 = 0 To 3
    30.             For c As Int32 = 0 To 3
    31.                 Dim pb As PictureBox = New PictureBox With {.Dock = DockStyle.Fill}
    32.                 tlp.Controls.Add(pb, c, r)
    33.                 pb.Visible = True
    34.                 pb.BorderStyle = BorderStyle.Fixed3D
    35.                 AddHandler pb.Click, AddressOf PB_Click
    36.             Next
    37.         Next
    38.  
    39.     End Sub
    40.  
    41.     Private Sub PB_Click(sender As Object, e As EventArgs)
    42.         Dim pb As PictureBox = DirectCast(sender, PictureBox)
    43.         Using f As New Form
    44.             Dim pg As New PropertyGrid With {.Dock = DockStyle.Fill}
    45.             pg.Parent = f
    46.             pg.SelectedObject = pb
    47.             f.ShowDialog()
    48.         End Using
    49.     End Sub
    50. End Class

  4. #4

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,803

    Re: how to inspect a generated object.

    Thank you TnTinMN,

    But you see the PictureBox has to be clicked that's exactly what I was saying, all the examples I can find need something to be clicked.

    I know how to do that, I do it often, I'm sure there's a way to read the height (say) of an object without having to actually do something to it.

    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  5. #5

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,803

    Re: how to inspect a generated object.

    Quote Originally Posted by Nirwanda View Post
    I'm probably misunderstanding, but I read your post twice and it appears to me the solution is simply to reference the control and return X attribute?
    Code:
    Form Load
        MsgBox(Picturebox1.width)
    End sub
    That will show the width of Picturebox1 in a messagebox.
    Actually, that's the problem, as soon as I try to run this code I'll get an error because there IS no Picturebox1, it's not been generated yet.

    However your next solution has given me the clue... My code looks a bit 'clumsy' but it works correctly.
    If you want to know the size of ALL your pictureboxes without typing in every name by hand, then that is another matter.
    I don't work a lot with winforms anymore, so I can't supply that code on the go (it used to be soooo easy with control arrays back in VB6 sigh).

    Here's an example I found looping through all textboxes inside a specified panel:
    Code:
    Dim ctrl As Control
      For Each ctrl In Panel1.Controls
      If (ctrl.GetType() Is GetType(TextBox)) Then
          Dim txt As TextBox = CType(ctrl, TextBox)
          txt.BackColor = Color.LightYellow
      End If
    This code is that which calls for Form1 to be maximised, Then having re-sized everything, it looks for the PictureBox called 'Pic5' and sets it to be square. Which is the purpose of the exercise.
    Almost invariably computer screens are wider than they are tall but the aspect ratio can differ, I want one particular PictureBox to be exactly square but there's no guarantee that if the PictureBox were square at the design stage, it will still be square when the form is maximised.

    The TLP cell in which Pic5 resides has it's row height set as Percentage, so it's height will be related to the height of the screen. It's column width is set to AutoSize, so... maximise the form measure the PictureBox's height and set it's width to that. The column will set it's width to the widest control in that column which will always be PictureBox Pic5. Problem solved.

    Thank you Nirwanda, brilliant.


    Code:
        Private Sub Button2_Click() Handles Button2.Click
            Dim ctrl As Control
            Me.WindowState = 2
            For Each ctrl In TableLayoutPanel1.Controls
                If (ctrl.GetType() Is GetType(PictureBox)) Then
                    If CType(ctrl, PictureBox).Name = "Pic5" Then
                        CType(ctrl, PictureBox).Width = CType(ctrl, PictureBox).Height
                    End If
                End If
            Next
        End Sub


    Poppa.

    VS2015
    Windows 10.
    Along with the sunshine there has to be a little rain sometime.

  6. #6

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,803

    Re: [RESOLVED] how to inspect a generated object.

    Hi again.

    Actually this works just fine and is less contrived (neater) !

    Code:
        Private Sub Button2_Click() Handles Button2.Click
            Dim ctrl As Control
            Me.WindowState = 2
            For Each ctrl In TableLayoutPanel1.Controls
                If ctrl.Name = "Pic5" Then ctrl.Width = ctrl.Height
            Next
        End Sub
    Poppa.

    Last edited by Poppa Mintin; Dec 13th, 2016 at 08:21 PM. Reason: Typo !
    Along with the sunshine there has to be a little rain sometime.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,907

    Re: [RESOLVED] how to inspect a generated object.

    When I create dynamic controls, if there are going to be a known number of them then I just dimension an array of the appropriate size and add the controls to the array so I can access the controls directly by an index in the array.
    If the number of controls depend on some run time conditions, then I would use a list.
    If there are some hierarchical structure to how the controls are organized, then I might use combinations of things, like a dictionary of lists or linked lists.

    Since I'm creating the controls for some purpose, I find it easiest to save a reference to the control created in my own "collection" in addition to the collection of whatever container the control is in. Often, I even have references to the controls in more than one list depending on how I might want to look them up, essentially cross reference tables so I can get access to the control quickly by more than one "key".

    Having a reference to the control in your own storage also makes it easier to do things like deciding to put the control in a different container, e.g. a different panel because of a GUI reorganization, without having to change the code that accesses the control.
    Last edited by passel; Dec 14th, 2016 at 07:52 AM.

  8. #8

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,803

    Re: [RESOLVED] how to inspect a generated object.

    Quote Originally Posted by passel View Post
    Personally when I create dynamic controls, if there are going to be a known number of them then I just dimension an array of the appropriate size and add the controls to the array so I can access the controls directly by an index in the array.
    Thanks Passel, that's certainly the way I'll go next time... I think there's a good chance that I did it that way before... I knew it wasn't difficult.


    Poppa.
    Along with the sunshine there has to be a little rain sometime.

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