Hi all,
before writing this thread I have noticed that many other people have my problem…
If I use a ComboBox in a C# form, there is no way to change its height (unfortunately it hasn’t an “AutoSize” property settable to “false” as the TextBox).
Perhaps, changing the font size, one can change the ComboBox height too (even if it would be better that MS introduce an AutoSize property…)
But the very strange thing I have noticed is another one:
I have defined in my program many ComboBox controls, leaving the predefined settings about font size. I have disposed them one under another, calculating the top of the succeeding by adding previous top + previous height + 20.
The incredible thing is that, when the form is loaded with its class constructor, the height of the ComboBoxes is 20, after that it magically becomes 21!!!
In fact, if you re-execute the Location statements of the ComboBoxes, you’ll see that these controls will (slightly) move to the bottom of the form.
This visual effect (that verifies if you have to redraw ComboBox controls) is a little bit unpleasant, isn’t it?
However, a banal solution is to set the height of a ComboBox adding the top of the previous one to 20 (its initial height) + 20 = 40…
Here there is the source code that highlights the problem (beside ComboBox controls, I have defined a Label, a TextBox and a Button control, to illustrate that they don’t suffer this effect):
As last statement of the form constructor, there is the call to a procedure that prints in the console window the location and dimension of all the controls. If you click on the “Print” button, this procedure is executed again, and you will see in the console window that the ComboBoxes height is changed from 20 to 21 (however, the Top properties don’t change).
Now, if you click on the “Relocate” button, a procedure that relocate the controls (with the same statements of the Form constructor) is called. You will see that the ComboBoxes will move to the bottom (if you re-click on the “Print” button again, you will realize that the Top properties are changed).
This is the code:
before writing this thread I have noticed that many other people have my problem…
If I use a ComboBox in a C# form, there is no way to change its height (unfortunately it hasn’t an “AutoSize” property settable to “false” as the TextBox).
Perhaps, changing the font size, one can change the ComboBox height too (even if it would be better that MS introduce an AutoSize property…)
But the very strange thing I have noticed is another one:
I have defined in my program many ComboBox controls, leaving the predefined settings about font size. I have disposed them one under another, calculating the top of the succeeding by adding previous top + previous height + 20.
The incredible thing is that, when the form is loaded with its class constructor, the height of the ComboBoxes is 20, after that it magically becomes 21!!!
In fact, if you re-execute the Location statements of the ComboBoxes, you’ll see that these controls will (slightly) move to the bottom of the form.
This visual effect (that verifies if you have to redraw ComboBox controls) is a little bit unpleasant, isn’t it?
However, a banal solution is to set the height of a ComboBox adding the top of the previous one to 20 (its initial height) + 20 = 40…
Here there is the source code that highlights the problem (beside ComboBox controls, I have defined a Label, a TextBox and a Button control, to illustrate that they don’t suffer this effect):
As last statement of the form constructor, there is the call to a procedure that prints in the console window the location and dimension of all the controls. If you click on the “Print” button, this procedure is executed again, and you will see in the console window that the ComboBoxes height is changed from 20 to 21 (however, the Top properties don’t change).
Now, if you click on the “Relocate” button, a procedure that relocate the controls (with the same statements of the Form constructor) is called. You will see that the ComboBoxes will move to the bottom (if you re-click on the “Print” button again, you will realize that the Top properties are changed).
This is the code:
Code:
Hi all,
before writing this thread I have noticed that many other people have my problem…
If I use a ComboBox in a C# form, there is no way to change its height (unfortunately it hasn’t an “AutoSize” property settable to “false” as the TextBox).
Perhaps, changing the font size, one can change the ComboBox height too (even if it would be better that MS introduce an AutoSize property…)
But the very strange thing I have noticed is another one:
I have defined in my program many ComboBox controls, leaving the predefined settings about font size. I have disposed them one under another, calculating the top of the succeeding by adding previous top + previous height + 20.
The incredible thing is that, when the form is loaded with its class constructor, the height of the ComboBoxes is 20, after that it magically becomes 21!!!
In fact, if you re-execute the Location statements of the ComboBoxes, you’ll see that these controls will (slightly) move to the bottom of the form.
This visual effect (that verifies if you have to redraw ComboBox controls) is a little bit unpleasant, isn’t it?
However, a banal solution is to set the height of a ComboBox adding the top of the previous one to 20 (its initial height) + 20 = 40…
Here there is the source code that highlights the problem (beside ComboBox controls, I have defined a Label, a TextBox and a Button control, to illustrate that they don’t suffer this effect):
As last statement of the form constructor, there is the call to a procedure that prints in the console window the location and dimension of all the controls. If you click on the “Print” button, this procedure is executed again, and you will see in the console window that the ComboBoxes height is changed from 20 to 21 (however, the Top properties don’t change).
Now, if you click on the “Relocate” button, a procedure that relocate the controls (with the same statements of the Form constructor) is called. You will see that the ComboBoxes will move to the bottom (if you re-click on the “Print” button again, you will realize that the Top properties are changed).
This is the code:
using System;
using System.Drawing;
using System.Windows.Forms;
public class FormLogicTrace: Form
{
public static void Main()
{
Application.Run(new FormLogicTrace());
}
Label lbl;
TextBox txt;
Button btn;
ComboBox cbo1, cbo2, cbo3;
Button btnPrintComboBoxesLocations_Dimensions, btnRelocateControls;
public FormLogicTrace()
{
Size = new Size(200, 420);
CenterToScreen();
lbl = new Label();
lbl.Parent = this;
lbl.Size = new Size(120, 20);
lbl.Location = new Point(20, 20);
lbl.Text = "lbl";
txt = new TextBox();
txt.Parent = this;
txt.Size = new Size(120, 20);
txt.Location = new Point(20, lbl.Top + lbl.Height + 20);
txt.Text = "txt";
btn = new Button();
btn.Parent = this;
btn.Size = new Size(120, 25);
btn.Location = new Point(20, txt.Top + txt.Height + 20);
btn.Text = "btn";
cbo1 = new ComboBox();
cbo1.Parent = this;
cbo1.Size = new Size(120, 30);
cbo1.Location = new Point(20, btn.Top + btn.Height + 20);
cbo1.DropDownStyle = ComboBoxStyle.DropDownList;
cbo1.Items.AddRange(new string[] {"cbo1", "cbo2", "cbo3"});
cbo1.SelectedIndex = 0;
cbo2 = new ComboBox();
cbo2.Parent = this;
cbo2.Size = new Size(120, 30);
cbo2.Location = new Point(20, cbo1.Top + cbo1.Height + 20);
cbo2.DropDownStyle = ComboBoxStyle.DropDownList;
cbo2.Items.AddRange(new string[] {"cbo1", "cbo2", "cbo3"});
cbo2.SelectedIndex = 0;
cbo3 = new ComboBox();
cbo3.Parent = this;
cbo3.Size = new Size(120, 30);
cbo3.Location = new Point(20, cbo2.Top + cbo2.Height + 20);
cbo3.DropDownStyle = ComboBoxStyle.DropDownList;
cbo3.Items.AddRange(new string[] {"cbo1", "cbo2", "cbo3"});
cbo3.SelectedIndex = 0;
btnPrintComboBoxesLocations_Dimensions = new Button();
btnPrintComboBoxesLocations_Dimensions.Parent = this;
btnPrintComboBoxesLocations_Dimensions.Size = new Size(120, 25);
btnPrintComboBoxesLocations_Dimensions.Location = new Point(20, cbo3.Top + cbo3.Height + 20);
btnPrintComboBoxesLocations_Dimensions.Text = "Print";
btnPrintComboBoxesLocations_Dimensions.Click += new EventHandler(btnPrintComboBoxDimension_Click);
btnRelocateControls = new Button();
btnRelocateControls.Parent = this;
btnRelocateControls.Size = new Size(120, 25);
btnRelocateControls.Location = new Point(20, btnPrintComboBoxesLocations_Dimensions.Top + btnPrintComboBoxesLocations_Dimensions.Height + 20);
btnRelocateControls.Text = "Relocate";
btnRelocateControls.Click += new EventHandler(btnRelocateControls_Click);
PrintControlsLocations_Dimensions();
}
private void btnPrintComboBoxDimension_Click(object sender, EventArgs e)
{
PrintControlsLocations_Dimensions();
}
void PrintControlsLocations_Dimensions()
{
Console.WriteLine("Label location / dimensions: Left = " + lbl.Left + ", Top = " + lbl.Top + ", Width = " + lbl.Width + ", Height = " + lbl.Height);
Console.WriteLine("TextBox location / dimensions: Left = " + txt.Left + ", Top = " + txt.Top + ", Width = " + txt.Width + ", Height = " + txt.Height);
Console.WriteLine("Button location / dimensions: Left = " + btn.Left + ", Top = " + btn.Top + ", Width = " + btn.Width + ", Height = " + btn.Height);
Console.WriteLine("ComboBox1 location / dimensions: Left = " + cbo1.Left + ", Top = " + cbo1.Top + ", Width = " + cbo1.Width + ", Height = " + cbo1.Height);
Console.WriteLine("ComboBox2 location / dimensions: Left = " + cbo2.Left + ", Top = " + cbo2.Top + ", Width = " + cbo2.Width + ", Height = " + cbo2.Height);
Console.WriteLine("ComboBox3 location / dimensions: Left = " + cbo3.Left + ", Top = " + cbo3.Top + ", Width = " + cbo3.Width + ", Height = " + cbo3.Height);
Console.WriteLine("Print Button location / dimensions: Left = " + btnPrintComboBoxesLocations_Dimensions.Left + ", Top = " + btnPrintComboBoxesLocations_Dimensions.Top + ", Width = " + btnPrintComboBoxesLocations_Dimensions.Width + ", Height = " + btnPrintComboBoxesLocations_Dimensions.Height);
Console.WriteLine("Relocate Button location / dimensions: Left = " + btnRelocateControls.Left + ", Top = " + btnRelocateControls.Top + ", Width = " + btnRelocateControls.Width + ", Height = " + btnRelocateControls.Height);
Console.WriteLine();
}
void RelocateControls()
{
lbl.Location = new Point(20, 20);
txt.Location = new Point(20, lbl.Top + lbl.Height + 20);
btn.Location = new Point(20, txt.Top + txt.Height + 20);
cbo1.Location = new Point(20, btn.Top + btn.Height + 20);
cbo2.Location = new Point(20, cbo1.Top + cbo1.Height + 20);
cbo3.Location = new Point(20, cbo2.Top + cbo2.Height + 20);
btnPrintComboBoxesLocations_Dimensions.Location = new Point(20, cbo3.Top + cbo3.Height + 20);
btnRelocateControls.Location = new Point(20, btnPrintComboBoxesLocations_Dimensions.Top + btnPrintComboBoxesLocations_Dimensions.Height + 20);
}
private void btnRelocateControls_Click(object sender, EventArgs e)
{
RelocateControls();
}
}