dynamically creating controls in windows forms

B

BillE

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 TableLayoutPanel, which
worked fine but the performance was terrible, so I abandoned that approach.

thanks
Bill
 
A

Armin Zingler

BillE said:
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 TableLayoutPanel,
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
 
B

BillE

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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = 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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = New Size (100,20)
lbl1.Location = New Point (xPos, yPos) '
lbl2.Text ="whatever"
 
Z

zacks

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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = 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?
 
B

BillE

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 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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = 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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = New Size (100,20)
lbl1.Location = New Point (xPos, yPos) '
lbl2.Text ="whatever"
 
A

Armin Zingler

BillE said:
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
 
F

Family Tree Mike

BillE said:
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 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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = 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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = New Size (100,20)
lbl1.Location = New Point (xPos, yPos) '
lbl2.Text ="whatever"





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 TableLayoutPanel,
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?
 
K

Kerry Moorman

Armin,

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

Kerry Moorman
 
B

BillE

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 said:
BillE said:
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 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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = 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.MaximumSize = New Size (400,1000)
lbl1.MinimumSize = New Size (100,20)
lbl1.Location = New Point (xPos, yPos) '
lbl2.Text ="whatever"





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 TableLayoutPanel,
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top