dynamically creating controls in windows forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BillE

    #1

    dynamically creating controls in windows forms

    When I create a control dynamically and it grows according to the content,
    the control Height property still returns the original default control
    height instead of the height after expanding.

    How can I get the height of the control (in code) after it grows?

    I am building a windows form dynamically, creating controls and placing them
    on the form. There can be dozens of controls of all types on the form.
    This is for dynamic questionnaires.

    The controls can grow, depending on the content. For example, a label
    control can expand in height if the content is lengthy.

    I am placing the controls on the form by setting the location to a point,
    using a Y Position variable that increments with each control by adding the
    height of the control to the previous Y position.

    However, it seems that if a control expands in height, the Height property
    still contains the original height, and so the next control is not placed
    low enough on the form.

    What is the best way to determine the placement of the controls?

    I tried dynamically building a table using the TableLayoutPane l, which
    worked fine but the performance was terrible, so I abandoned that approach.

    thanks
    Bill


  • Armin Zingler

    #2
    Re: dynamically creating controls in windows forms

    "BillE" <belgie@datamti .comschrieb
    When I create a control dynamically and it grows according to the
    content, the control Height property still returns the original
    default control height instead of the height after expanding.
    >
    How can I get the height of the control (in code) after it grows?
    >
    I am building a windows form dynamically, creating controls and
    placing them on the form. There can be dozens of controls of all
    types on the form. This is for dynamic questionnaires.
    >
    The controls can grow, depending on the content. For example, a
    label control can expand in height if the content is lengthy.
    >
    I am placing the controls on the form by setting the location to a
    point, using a Y Position variable that increments with each control
    by adding the height of the control to the previous Y position.
    >
    However, it seems that if a control expands in height, the Height
    property still contains the original height, and so the next control
    is not placed low enough on the form.
    >
    What is the best way to determine the placement of the controls?
    >
    I tried dynamically building a table using the TableLayoutPane l,
    which worked fine but the performance was terrible, so I abandoned
    that approach.

    How do the controls "grow"? I can not reproduce the problem. The Height
    always returns the Height of the control. Can you post some code?


    Armin

    Comment

    • BillE

      #3
      Re: dynamically creating controls in windows forms

      In the snippet below, the height of the first control is the initial height
      (20), not the height which it grew to to accommodate the long text. The
      controls have AutoSize set to True.


      Dim lbl1 as New Label
      dim xPos as int32 = 100
      dim yPos as int32 = 50
      'create the first label
      lbl1.AutoSize = True
      lbl1.Width = 100
      lbl1.Height = 20
      lbl1.MaximumSiz e = New Size (400,1000)
      lbl1.MinimumSiz e = New Size (100,20)
      lbl1.Location = New Point (xPos, yPos)
      lbl1.Text = [a long string forcing the label to increase in size to the
      maximum width and a height greater than the original height of 20]

      'increase the Y position, so the next label will be placed below the first
      label

      '************** *************** *
      'this returns the initial height, not the increased height
      yPos += lbl.Height
      '************** *************** *

      dim lbl2 as New Label
      lbl1.AutoSize = True
      lbl1.Width = 100
      lbl1.Height = 20
      lbl1.MaximumSiz e = New Size (400,1000)
      lbl1.MinimumSiz e = New Size (100,20)
      lbl1.Location = New Point (xPos, yPos) '
      lbl2.Text ="whatever"



      "Armin Zingler" <az.nospam@free net.dewrote in message
      news:OBYM5dOOIH A.3816@TK2MSFTN GP04.phx.gbl...
      "BillE" <belgie@datamti .comschrieb
      >When I create a control dynamically and it grows according to the
      >content, the control Height property still returns the original
      >default control height instead of the height after expanding.
      >>
      >How can I get the height of the control (in code) after it grows?
      >>
      >I am building a windows form dynamically, creating controls and
      >placing them on the form. There can be dozens of controls of all
      >types on the form. This is for dynamic questionnaires.
      >>
      >The controls can grow, depending on the content. For example, a
      >label control can expand in height if the content is lengthy.
      >>
      >I am placing the controls on the form by setting the location to a
      >point, using a Y Position variable that increments with each control
      >by adding the height of the control to the previous Y position.
      >>
      >However, it seems that if a control expands in height, the Height
      >property still contains the original height, and so the next control
      >is not placed low enough on the form.
      >>
      >What is the best way to determine the placement of the controls?
      >>
      >I tried dynamically building a table using the TableLayoutPane l,
      >which worked fine but the performance was terrible, so I abandoned
      >that approach.
      >
      >
      How do the controls "grow"? I can not reproduce the problem. The Height
      always returns the Height of the control. Can you post some code?
      >
      >
      Armin

      Comment

      • zacks@construction-imaging.com

        #4
        Re: dynamically creating controls in windows forms

        On Dec 7, 11:18 am, "BillE" <bel...@datamti .comwrote:
        In the snippet below, the height of the first control is the initial height
        (20), not the height which it grew to to accommodate the long text. The
        controls have AutoSize set to True.
        >
        Dim lbl1 as New Label
        dim xPos as int32 = 100
        dim yPos as int32 = 50
        'create the first label
        lbl1.AutoSize = True
        lbl1.Width = 100
        lbl1.Height = 20
        lbl1.MaximumSiz e = New Size (400,1000)
        lbl1.MinimumSiz e = New Size (100,20)
        lbl1.Location = New Point (xPos, yPos)
        lbl1.Text = [a long string forcing the label to increase in size to the
        maximum width and a height greater than the original height of 20]
        I have tried this before and could never get a Label control's Height
        to grow no matter how much text I put in the control's Text property.
        The Label control does not have AcceptTab or AcceptReturn properties
        and doesn't have a MultiLine property. I could never get it to go
        multiline. Does yours?
        >
        'increase the Y position, so the next label will be placed below the first
        label
        >
        '************** *************** *
        'this returns the initial height, not the increased height
        yPos += lbl.Height
        '************** *************** *
        >
        dim lbl2 as New Label
        lbl1.AutoSize = True
        lbl1.Width = 100
        lbl1.Height = 20
        lbl1.MaximumSiz e = New Size (400,1000)
        lbl1.MinimumSiz e = New Size (100,20)
        lbl1.Location = New Point (xPos, yPos) '
        lbl2.Text ="whatever"
        >
        "Armin Zingler" <az.nos...@free net.dewrote in message
        >
        news:OBYM5dOOIH A.3816@TK2MSFTN GP04.phx.gbl...
        >
        >
        >
        "BillE" <bel...@datamti .comschrieb
        When I create a control dynamically and it grows according to the
        content, the control Height property still returns the original
        default control height instead of the height after expanding.
        >
        How can I get the height of the control (in code) after it grows?
        >
        I am building a windows form dynamically, creating controls and
        placing them on the form. There can be dozens of controls of all
        types on the form. This is for dynamic questionnaires.
        >
        The controls can grow, depending on the content. For example, a
        label control can expand in height if the content is lengthy.
        >
        I am placing the controls on the form by setting the location to a
        point, using a Y Position variable that increments with each control
        by adding the height of the control to the previous Y position.
        >
        However, it seems that if a control expands in height, the Height
        property still contains the original height, and so the next control
        is not placed low enough on the form.
        >
        What is the best way to determine the placement of the controls?
        >
        I tried dynamically building a table using the TableLayoutPane l,
        which worked fine but the performance was terrible, so I abandoned
        that approach.
        >
        How do the controls "grow"? I can not reproduce the problem. The Height
        always returns the Height of the control. Can you post some code?

        Comment

        • BillE

          #5
          Re: dynamically creating controls in windows forms

          If I set AutoSize = True, it grows horizontally and vertically to
          accommodate the content.

          But the Height property still returns the initial height, not the expanded
          height.

          Armin, do you see the same behavior? Do you know a way to determine the
          height which the control grows to?


          <zacks@construc tion-imaging.comwrot e in message
          news:21fcdf29-8cde-42e7-adf3-54f9a5e6c492@f3 g2000hsg.google groups.com...
          On Dec 7, 11:18 am, "BillE" <bel...@datamti .comwrote:
          >In the snippet below, the height of the first control is the initial
          >height
          >(20), not the height which it grew to to accommodate the long text. The
          >controls have AutoSize set to True.
          >>
          >Dim lbl1 as New Label
          >dim xPos as int32 = 100
          >dim yPos as int32 = 50
          >'create the first label
          >lbl1.AutoSiz e = True
          >lbl1.Width = 100
          >lbl1.Height = 20
          >lbl1.MaximumSi ze = New Size (400,1000)
          >lbl1.MinimumSi ze = New Size (100,20)
          >lbl1.Locatio n = New Point (xPos, yPos)
          >lbl1.Text = [a long string forcing the label to increase in size to the
          >maximum width and a height greater than the original height of 20]
          >
          I have tried this before and could never get a Label control's Height
          to grow no matter how much text I put in the control's Text property.
          The Label control does not have AcceptTab or AcceptReturn properties
          and doesn't have a MultiLine property. I could never get it to go
          multiline. Does yours?
          >
          >>
          >'increase the Y position, so the next label will be placed below the
          >first
          >label
          >>
          >'************* *************** **
          >'this returns the initial height, not the increased height
          >yPos += lbl.Height
          >'************* *************** **
          >>
          >dim lbl2 as New Label
          >lbl1.AutoSiz e = True
          >lbl1.Width = 100
          >lbl1.Height = 20
          >lbl1.MaximumSi ze = New Size (400,1000)
          >lbl1.MinimumSi ze = New Size (100,20)
          >lbl1.Locatio n = New Point (xPos, yPos) '
          >lbl2.Text ="whatever"
          >>
          >"Armin Zingler" <az.nos...@free net.dewrote in message
          >>
          >news:OBYM5dOOI HA.3816@TK2MSFT NGP04.phx.gbl.. .
          >>
          >>
          >>
          "BillE" <bel...@datamti .comschrieb
          >When I create a control dynamically and it grows according to the
          >content, the control Height property still returns the original
          >default control height instead of the height after expanding.
          >>
          >How can I get the height of the control (in code) after it grows?
          >>
          >I am building a windows form dynamically, creating controls and
          >placing them on the form. There can be dozens of controls of all
          >types on the form. This is for dynamic questionnaires.
          >>
          >The controls can grow, depending on the content. For example, a
          >label control can expand in height if the content is lengthy.
          >>
          >I am placing the controls on the form by setting the location to a
          >point, using a Y Position variable that increments with each control
          >by adding the height of the control to the previous Y position.
          >>
          >However, it seems that if a control expands in height, the Height
          >property still contains the original height, and so the next control
          >is not placed low enough on the form.
          >>
          >What is the best way to determine the placement of the controls?
          >>
          >I tried dynamically building a table using the TableLayoutPane l,
          >which worked fine but the performance was terrible, so I abandoned
          >that approach.
          >>
          How do the controls "grow"? I can not reproduce the problem. The Height
          always returns the Height of the control. Can you post some code?

          Comment

          • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

            #6
            Re: dynamically creating controls in windows forms



            "BillE" wrote:
            If I set AutoSize = True, it grows horizontally and vertically to
            accommodate the content.
            >
            But the Height property still returns the initial height, not the expanded
            height.
            >
            Armin, do you see the same behavior? Do you know a way to determine the
            height which the control grows to?
            >
            >
            <zacks@construc tion-imaging.comwrot e in message
            news:21fcdf29-8cde-42e7-adf3-54f9a5e6c492@f3 g2000hsg.google groups.com...
            On Dec 7, 11:18 am, "BillE" <bel...@datamti .comwrote:
            In the snippet below, the height of the first control is the initial
            height
            (20), not the height which it grew to to accommodate the long text. The
            controls have AutoSize set to True.
            >
            Dim lbl1 as New Label
            dim xPos as int32 = 100
            dim yPos as int32 = 50
            'create the first label
            lbl1.AutoSize = True
            lbl1.Width = 100
            lbl1.Height = 20
            lbl1.MaximumSiz e = New Size (400,1000)
            lbl1.MinimumSiz e = New Size (100,20)
            lbl1.Location = New Point (xPos, yPos)
            lbl1.Text = [a long string forcing the label to increase in size to the
            maximum width and a height greater than the original height of 20]
            I have tried this before and could never get a Label control's Height
            to grow no matter how much text I put in the control's Text property.
            The Label control does not have AcceptTab or AcceptReturn properties
            and doesn't have a MultiLine property. I could never get it to go
            multiline. Does yours?
            >
            'increase the Y position, so the next label will be placed below the
            first
            label
            >
            '************** *************** *
            'this returns the initial height, not the increased height
            yPos += lbl.Height
            '************** *************** *
            >
            dim lbl2 as New Label
            lbl1.AutoSize = True
            lbl1.Width = 100
            lbl1.Height = 20
            lbl1.MaximumSiz e = New Size (400,1000)
            lbl1.MinimumSiz e = New Size (100,20)
            lbl1.Location = New Point (xPos, yPos) '
            lbl2.Text ="whatever"
            >
            "Armin Zingler" <az.nos...@free net.dewrote in message
            >
            news:OBYM5dOOIH A.3816@TK2MSFTN GP04.phx.gbl...
            >
            >
            >
            "BillE" <bel...@datamti .comschrieb
            When I create a control dynamically and it grows according to the
            content, the control Height property still returns the original
            default control height instead of the height after expanding.
            >
            How can I get the height of the control (in code) after it grows?
            >
            I am building a windows form dynamically, creating controls and
            placing them on the form. There can be dozens of controls of all
            types on the form. This is for dynamic questionnaires.
            >
            The controls can grow, depending on the content. For example, a
            label control can expand in height if the content is lengthy.
            >
            I am placing the controls on the form by setting the location to a
            point, using a Y Position variable that increments with each control
            by adding the height of the control to the previous Y position.
            >
            However, it seems that if a control expands in height, the Height
            property still contains the original height, and so the next control
            is not placed low enough on the form.
            >
            What is the best way to determine the placement of the controls?
            >
            I tried dynamically building a table using the TableLayoutPane l,
            which worked fine but the performance was terrible, so I abandoned
            that approach.
            >
            How do the controls "grow"? I can not reproduce the problem. The Height
            always returns the Height of the control. Can you post some code?
            >
            >
            My experience is the same as Zacks. How are you identifying that the Height
            actually changed in your running code?


            Comment

            • Armin Zingler

              #7
              Re: dynamically creating controls in windows forms

              "BillE" <belgie@datamti .comschrieb
              If I set AutoSize = True, it grows horizontally and vertically to
              accommodate the content.
              >
              But the Height property still returns the initial height, not the
              expanded height.
              >
              Armin, do you see the same behavior? Do you know a way to determine
              the height which the control grows to?
              In the past, a label with autosize=true was always 1 line high because the
              width has been adjusted automatically. Therefore I can't answer this.


              Armin

              Comment

              • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

                #8
                Re: dynamically creating controls in windows forms

                Armin,

                The label should grow vertically if the content contains newline characters.
                But the height property should reflect this.

                Kerry Moorman


                "Armin Zingler" wrote:
                "BillE" <belgie@datamti .comschrieb
                If I set AutoSize = True, it grows horizontally and vertically to
                accommodate the content.

                But the Height property still returns the initial height, not the
                expanded height.

                Armin, do you see the same behavior? Do you know a way to determine
                the height which the control grows to?
                >
                In the past, a label with autosize=true was always 1 line high because the
                width has been adjusted automatically. Therefore I can't answer this.
                >
                >
                Armin
                >
                >

                Comment

                • BillE

                  #9
                  Re: dynamically creating controls in windows forms

                  I can visually see that the control has grown - in the code i posted, it is
                  created with a width of 100 and a height of 20, but when displayed the
                  control grows vertically to display the full content. However, if i check
                  for the height of the control in code it is still 20.

                  "Family Tree Mike" <FamilyTreeMike @discussions.mi crosoft.comwrot e in
                  message news:109ED49D-49A6-4E0C-AC84-961F32585941@mi crosoft.com...
                  >
                  >
                  "BillE" wrote:
                  >
                  >If I set AutoSize = True, it grows horizontally and vertically to
                  >accommodate the content.
                  >>
                  >But the Height property still returns the initial height, not the
                  >expanded
                  >height.
                  >>
                  >Armin, do you see the same behavior? Do you know a way to determine the
                  >height which the control grows to?
                  >>
                  >>
                  ><zacks@constru ction-imaging.comwrot e in message
                  >news:21fcdf2 9-8cde-42e7-adf3-54f9a5e6c492@f3 g2000hsg.google groups.com...
                  On Dec 7, 11:18 am, "BillE" <bel...@datamti .comwrote:
                  >In the snippet below, the height of the first control is the initial
                  >height
                  >(20), not the height which it grew to to accommodate the long text.
                  >The
                  >controls have AutoSize set to True.
                  >>
                  >Dim lbl1 as New Label
                  >dim xPos as int32 = 100
                  >dim yPos as int32 = 50
                  >'create the first label
                  >lbl1.AutoSiz e = True
                  >lbl1.Width = 100
                  >lbl1.Height = 20
                  >lbl1.MaximumSi ze = New Size (400,1000)
                  >lbl1.MinimumSi ze = New Size (100,20)
                  >lbl1.Locatio n = New Point (xPos, yPos)
                  >lbl1.Text = [a long string forcing the label to increase in size to
                  >the
                  >maximum width and a height greater than the original height of 20]
                  >
                  I have tried this before and could never get a Label control's Height
                  to grow no matter how much text I put in the control's Text property.
                  The Label control does not have AcceptTab or AcceptReturn properties
                  and doesn't have a MultiLine property. I could never get it to go
                  multiline. Does yours?
                  >
                  >>
                  >'increase the Y position, so the next label will be placed below the
                  >first
                  >label
                  >>
                  >'************* *************** **
                  >'this returns the initial height, not the increased height
                  >yPos += lbl.Height
                  >'************* *************** **
                  >>
                  >dim lbl2 as New Label
                  >lbl1.AutoSiz e = True
                  >lbl1.Width = 100
                  >lbl1.Height = 20
                  >lbl1.MaximumSi ze = New Size (400,1000)
                  >lbl1.MinimumSi ze = New Size (100,20)
                  >lbl1.Locatio n = New Point (xPos, yPos) '
                  >lbl2.Text ="whatever"
                  >>
                  >"Armin Zingler" <az.nos...@free net.dewrote in message
                  >>
                  >news:OBYM5dOOI HA.3816@TK2MSFT NGP04.phx.gbl.. .
                  >>
                  >>
                  >>
                  "BillE" <bel...@datamti .comschrieb
                  >When I create a control dynamically and it grows according to the
                  >content, the control Height property still returns the original
                  >default control height instead of the height after expanding.
                  >>
                  >How can I get the height of the control (in code) after it grows?
                  >>
                  >I am building a windows form dynamically, creating controls and
                  >placing them on the form. There can be dozens of controls of all
                  >types on the form. This is for dynamic questionnaires.
                  >>
                  >The controls can grow, depending on the content. For example, a
                  >label control can expand in height if the content is lengthy.
                  >>
                  >I am placing the controls on the form by setting the location to a
                  >point, using a Y Position variable that increments with each
                  >control
                  >by adding the height of the control to the previous Y position.
                  >>
                  >However, it seems that if a control expands in height, the Height
                  >property still contains the original height, and so the next
                  >control
                  >is not placed low enough on the form.
                  >>
                  >What is the best way to determine the placement of the controls?
                  >>
                  >I tried dynamically building a table using the TableLayoutPane l,
                  >which worked fine but the performance was terrible, so I abandoned
                  >that approach.
                  >>
                  How do the controls "grow"? I can not reproduce the problem. The
                  Height
                  always returns the Height of the control. Can you post some code?
                  >>
                  >>
                  >
                  My experience is the same as Zacks. How are you identifying that the
                  Height
                  actually changed in your running code?
                  >
                  >

                  Comment

                  Working...