【问题标题】:Unexpected behaviour when drawing textbox controls C#绘制文本框控件 C# 时的意外行为
【发布时间】: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>

点击按钮后输出的形式总是:

您可以看到文本被截断并且位置很奇怪。

【问题讨论】:

  • 我看到您正在创建文本框。我没看到你它们。

标签: c# winforms


【解决方案1】:

我认为问题在于标签的长度,而不是文本框的位置。

我建议使用TableLayoutPanel,这是最容易用于此类布局的控件。

【讨论】:

  • 这是标签的问题,但我将使用TableLayoutPanel,因为这似乎更合适。
  • @Ben:我知道这是标签(甚至在答案中)。我自己去过那里。 :)
猜你喜欢
  • 2016-09-10
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
相关资源
最近更新 更多