C# 绘制textbox控件C时出现意外行为#
我正在基于XML文件中的数据动态地将TextBox控件绘制到Windows C#表单中,但最终绘制的TextBox存在奇怪的行为问题。我在一个空白项目中尝试过这样做,同样的问题也出现了。TextBox控件中的文本似乎处于奇怪的位置,我无法滚动文本。我不确定这里有什么问题。 我的代码:C# 绘制textbox控件C时出现意外行为#,c#,winforms,C#,Winforms,我正在基于XML文件中的数据动态地将TextBox控件绘制到Windows C#表单中,但最终绘制的TextBox存在奇怪的行为问题。我在一个空白项目中尝试过这样做,同样的问题也出现了。TextBox控件中的文本似乎处于奇怪的位置,我无法滚动文本。我不确定这里有什么问题。 我的代码: private void抽屉元素() { NameValueCollection DatabaseConnectionList=ConfigurationManager.GetSection(“数据库类型”)作为N
private void抽屉元素()
{
NameValueCollection DatabaseConnectionList=ConfigurationManager.GetSection(“数据库类型”)作为NameValueCollection;
int x=50;
int y=70;
for(int i=0;i
XML:
单击按钮后输出的表单始终为:
你可以看到文本被切碎并且位置怪异。我认为问题在于标签的长度,而不是文本框的位置
我建议使用a,这是用于此类布局的最简单控件。我看到您正在创建文本框。我看不到你在画它们。这是标签的问题,但我将使用
表格布局面板
,因为这似乎更合适。@Ben:我知道是标签(甚至在答案中)。我自己也去过那里
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 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>