Ciao a tutti,
leggendo qua e là per il forum ho scoperto che non sono l'unico ad
avere questo problema.
Se si inserisce un controllo ComboBox in un form di C#, non è
possibile impostare la sua altezza ad un valore diverso da 21. Forse,
cambiando la dimensione del font, si potrebbe riuscire a cambiare anche
l'impostazione dell'altezza... (MS farebbe comunque meglio ad
introdurre per il ComboBox una proprietà AutoSize impostabile a
"false" come nel caso del TextBox).
La cosa strana è però la seguente:
al momento del caricamento del form (tramite il relativo costruttore di
classe), l'altezza del ComboBox è impostata a 20; finito il
caricamento, il valore cambia a 21... Pazzesco!!!
Ho fatto una prova molto semplice: ho costruito un form in cui ho
inserito 3 ComboBox uno sotto l'altro. Ho impostato la proprietà Top
di ciascun ComboBox come la somma della proprietà Top di quello sopra
+ la sua altezza + 20. In più, ho anche inserito alcuni altri
controlli per notare come invece le loro altezze restano invariate.
Alla fine del costruttore del form ho scritto una chiamata ad una
procedura che stampa sulla Console Window le posizioni e le dimensioni
dei vari controlli (l'altezza dei vari ComboBox a quel punto risulta
20). Cliccando su un pulsante "Print" viene chiamata la stessa
procedura, che stavolta però evidenzierà che l'altezza dei ComboBox
è ora a 21.
Cliccando infine su un pulsante "Relocate" viene chiamata una procedura
che riesegue le stesse istruzioni di Location dei controlli eseguite
nel costruttore del form (con le proprietà Top calcolate nel modo
suddetto, solo che stavolta cambieranno anch'esse, essendo le altezze
aumentate di 1). L'effetto visivo è che, quando si clicca su
"Relocate", si vedrà i ComboBox abbassarsi leggermente... Cliccando di
nuovo su "Print" ci si renderà conto del cambiamento dei valori delle
proprietà Top.
Poco male, basta impostare il Top di un ComboBox come il Top del
ComboBox sovrastante + 20 (la sua altezza teorica) + altri 20 = 40...
Qui sotto, se qualcuno vuole fare la prova, metto il codice sorgente...
Ciao
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();
}
}