【发布时间】:2014-12-11 20:20:11
【问题描述】:
我正在根据 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>
点击按钮后输出的形式总是:
您可以看到文本被截断并且位置很奇怪。
【问题讨论】:
-
我看到您正在创建文本框。我没看到你画它们。