为什么自动尺寸调整功能设置宽度尺寸错误?

时间:2019-02-13 05:02:48

标签: c# winforms

我正在以编程方式创建标签,并在标签旁边创建一个文本框。

要在其旁边创建文本框,我使用以下计算方法确定文本框的Location.X: label2.X + label2.Width + 5;

这是问题所在;标签被创建,并且我已经为其启用了自动调整大小,然后在输入文本后适当调整其大小。但是,除了创建文本框外,它不会被创建,但是会重叠一定距离。

我调试了代码,标签返回的宽度为100,而在使用autosize手动创建重复的标签并使用相同的文本字符串后,宽度变为149。为什么存在此问题,并且有解决方案而不是每次出现问题时都要手动操作?

我的下面的代码:

//Qd
                //label
                Label label2 = new Label();
                label2.Location = new System.Drawing.Point(6, 68);
                label2.Name = "label2";
                //label2.Size = new System.Drawing.Size(24, 13);
                label2.TabIndex = 16;
                label2.Text = "Characteristic Strength Qd:";
                label2.AutoSize = true;
                label2.MouseHover += new EventHandler(BoucWen_Qd_MouseHover);
                //textbox
                TextBox textBox3 = new TextBox();
                textBox3.Location = new System.Drawing.Point(/*90*/149+5+6, 65);
                textBox3.Name = "Qd";
                textBox3.Size = new System.Drawing.Size(197, 20);
                textBox3.TabIndex = 17;
                textBox3.Tag = "Characteristic Strength\r\n Link: )_Element";
                textBox3.HelpRequested += new HelpEventHandler(Node_label1_HelpRequested);
                //create units label 
                x_unit = textBox3.Location.X + textBox3.Width + 5;
                y_unit = textBox3.Location.Y;
                labelUnit = new Label();
                labelUnit.Location = new System.Drawing.Point(x_unit, y_unit);
                labelUnit.AutoSize = true;
                labelUnit.Text = forceunit;
                Fixidity_panel.Controls.Add(labelUnit); 

//adding the above two label&textbox:
 Fixidity_panel.Controls.AddRange(new Control[] {

                     comboBox2,
                     label11,
                     textBox11,
                     label10,
                     comboBox1,
                     label9,
                     textBox9,
                     label8,
                     textBox8,
                     label7,
                     textBox7,
                     label6,
                     textBox6,
                     label5,
                     textBox5,
                     label4,
                     textBox4,
                     label3,
                    textBox3,
                     label2,
                     textBox2,
                     Stiffness_label, });

1 个答案:

答案 0 :(得分:1)

AutoSize属性设置为true时,宽度的计算仅在将其添加到面板之后进行。
,因此,您应该首先将其添加到面板,然后添加texbox并设置其位置.Location = label2.Location.X+label2.Width +5以获得预期的结果。
这是我的建议,基于您的代码:

(请阅读我用大写字母写的评论)

//label
Label label2 = new Label();
label2.Location = new System.Drawing.Point(6, 68);
label2.Name = "label2";
//label2.Size = new System.Drawing.Size(24, 13);
label2.TabIndex = 16;
label2.Text = "Characteristic Strength Qd:";
label2.AutoSize = true;
// ADD IT TO THE PANEL IN ORDER TO GAIN A WIDTH
Fixidity_panel.Controls.Add(label2);
label2.MouseHover += new EventHandler(BoucWen_Qd_MouseHover);
//textbox
TextBox textBox3 = new TextBox();
// NOW YOU CAN DO:  .Location = label2.X+label2.Width+5
textBox3.Location = new System.Drawing.Point(label2.Location.X+label2.Width +5, 65);
textBox3.Name = "Qd";
textBox3.Size = new System.Drawing.Size(197, 20);
textBox3.TabIndex = 17;
textBox3.Tag = "Characteristic Strength\r\n Link: )_Element";
// NOW ADD THE TEXTBOX
Fixidity_panel.Controls.Add(textBox3);