我动态地将基于XML文件中的数据的TextBox控件绘制到Windows C#表单,但生成的绘制文本框具有奇怪的行为问题。我试过在一个空白的项目中这样做,同样的问题出现了。 TextBox控件中的文本似乎处于奇怪的位置,我没有能力滚动文本。我不确定这里的问题是什么。 我的代码:
private void DrawElements()
{
NameValueCollection DatabaseConnectionList = ConfigurationManager.GetSection("databaseTypes") as NameValueCollection;
int x = 50;
int y = 70;
for (int i = 0; i < DatabaseConnectionList.Count; i++)
{
Label l = new Label();
l.Text = "Prefix";
l.Location = new Point(x, y);
TextBox T = new TextBox();
T.Text = DatabaseConnectionList.GetKey(i).ToString();
T.Size = new Size(200, 20);
T.TextAlign = HorizontalAlignment.Left;
x += 20;
T.Location = new Point(x, y);
Label l2 = new Label();
l2.Text = "Connection String";
x += 20 + 200;
l2.Location = new Point(x, y);
TextBox T2 = new TextBox();
T2.Text = DatabaseConnectionList.Get(i).ToString();
T2.Size = new Size(200, 20);
T2.TextAlign = HorizontalAlignment.Left;
x += 20;
T2.Location = new Point(x, y);
this.Controls.Add(l);
this.Controls.Add(T);
this.Controls.Add(l2);
this.Controls.Add(T2);
y += 25;
x = 50;
}
this.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
DrawElements();
}
XML:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="databaseTypes" type="System.Configuration.NameValueSectionHandler" />
<section name="dataDictionary" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<databaseTypes>
<add key="ExampleServerPrefix_T" value="Connection_String_For_ExampleServer" />
<add key="ExampleServer2Prefix_T" value="Connection_String_For_ExampleServer_2" />
<add key="COPYLIVE_" value="ODBC;DSN=blah;" />
</databaseTypes>
</configuration>
点击按钮后输出的表格始终为:
你可以看到文字被砍掉并处于奇怪的位置。