代码之家  ›  专栏  ›  技术社区  ›  ScottishTapWater

以编程方式添加的文本框不显示

  •  1
  • ScottishTapWater  · 技术社区  · 7 年前

    我正在使用 WinForms 在里面 VS15 C#

    我正在动态添加 TextBox s和 Label s到我的 Form ComboBox (本质上,这是在数据集合中查找一个值,该值告诉我的UI它需要什么控件)。

    当我尝试生成控件时 s的出现和布局都很好,但是 文本框

    我试着和 MaximumSize MinimumSize

    我使用的代码如下(我知道 List<Pair<Label,TextBox>> 是相当不必要的,但我发现它有助于可读性):

    private void GenerateControls(string formType)
        {
            string[] formParameters = engine.GetFormParameters(formType);
            if (formParameters == null) return;
            SplitterPanel panel = splitContainer.Panel1;
            panel.Controls.Clear();
            List<Pair<Label, TextBox>> controlPairs = new List<Pair<Label, TextBox>>();
            int tabIndex = 0;
            Point labelPoint = panel.Location + new Size(20, 20);
            Size initialOffset = new Size(0, 30);
            Size horizontalOffset = new Size(40, 0);
            Size tBoxSize = new Size(40,20);
            foreach (string parameter in formParameters)
            {
                Label label = new Label
                {
                    Text = parameter,
                    Tag = "Parameter Label",
                    Name = $"lbl{parameter}",
                    Location = (labelPoint += initialOffset)
                };
                TextBox textBox = new TextBox
                {
                    AcceptsTab = true,
                    TabIndex = tabIndex++,
                    Text = "",
                    Tag = parameter,
                    Name = $"txt{parameter}",
                    MaximumSize = tBoxSize,
                    MinimumSize = tBoxSize,
                    Size = tBoxSize,
                    Location = labelPoint + horizontalOffset
                };
                controlPairs.Add(new Pair<Label, TextBox>(label, textBox));
            }
    
            foreach (Pair<Label, TextBox> pair in controlPairs)
            {
                panel.Controls.Add(pair.First);
                panel.Controls.Add(pair.Second);
            }
        }
    

    Point + Size Point 类重写 + 这样的操作员:

    Point+

    1 回复  |  直到 6 年前
        1
  •  1
  •   ScottishTapWater    7 年前

    不幸的是,对我来说,问题似乎只是dX的值不够大,无法阻止文本框隐藏在标签下,我忘记了标签没有透明的背景。

    当我这么做的时候:我删除了多余的 List<Pair<<>> TextBox 位置基于 Label

        private void GenerateControls(string formType)
        {
            string[] formParameters = engine.GetFormParameters(formType);
            if (formParameters == null) return;
            SplitterPanel panel = splitContainer.Panel1;
            panel.Controls.Clear();
            int tabIndex = 0;
            Point labelPoint = panel.Location + new Size(20, 20);
            Size verticalOffset = new Size(0, 30);
            Size tBoxSize = new Size(200,20);
            int maxLabelLength = 0;
            foreach (string parameter in formParameters)
            {
                Label label = new Label
                {
                    Text = parameter,
                    Tag = "Parameter Label",
                    Name = $"lbl{parameter}",
                    Location = (labelPoint += verticalOffset),
                    AutoSize = true
                };
                panel.Controls.Add(label);
                if (label.Size.Width > maxLabelLength)
                {
                    maxLabelLength = label.Size.Width;
                }
            }
            Size horizontalOffset = new Size(maxLabelLength + 30, 0);
            labelPoint = panel.Location + new Size(20, 20) + horizontalOffset;
            foreach (string parameter in formParameters)
            { 
                TextBox textBox = new TextBox
                {
                    AcceptsTab = true,
                    TabIndex = tabIndex++,
                    Text = "",
                    Tag = parameter,
                    Name = $"txt{parameter}",
                    MaximumSize = tBoxSize,
                    MinimumSize = tBoxSize,
                    Size = tBoxSize,
                    Location = labelPoint += verticalOffset
                };
                panel.Controls.Add(textBox);
            }
    
        }
    

    谢谢所有帮助过我的人!