如何动态创建 MaterialSingleLineTextField 控件 [英] How to create MaterialSingleLineTextField Controls dynamically

查看:70
本文介绍了如何动态创建 MaterialSingleLineTextField 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 MaterialSingleLineTextField 动态添加到表单.
我使用了

我正在尝试在Form.Load上动态创建多个MaterialSkin文本框.但是,主机面板中没有显示任何控件.

  private void Form1_Load(对象发送者,EventArgs e){整数n = 5;int pointX = 30;int pointY = 40;//panel1.Controls.Clear();对于(int i = 0; i< n-1; i ++){MaterialSingleLineTextField a =新的MaterialSingleLineTextField();a.Text =(i + 1).ToString();a.Visible = true;a.Location =新Point(pointX,pointY);panel1.Controls.Add(a);panel1.Show();pointY + = 20;}} 

此代码块对普通的TextBoxes来说效果很好.
有什么方法可以动态添加 MaterialSingleLineTextField ?

解决方案

使用默认主题值的示例表单初始化.

  • MaterialSkinManager 在窗体构造器中初始化,将主题设置为 MaterialSkinManager.Themes.LIGHT 和默认配色方案.表单基本类型设置为 MaterialForm .
  • 指定数量的 MaterialSingleLineTextField 控件被添加到父容器(面板),从定义的位置开始向下.这些控件固定在父控件上,并且Height设置为 Parent.Font.Height + 4 .
    • 指定这些控件的大小很重要,否则将获得一个最小的大小,以防止控件显示其内容.
  • 正如您在 AddTextFields()方法中看到的那样,如果要用新的控件替换现有的控件,则必须丢弃添加到父容器中的以前的控件,这一点很重要.使用此库甚至更重要.
    调用 Control.Controls 集合的 Clear()方法(就像您注释掉的那一行一样)不会处理任何东西,那些控件仍然是活着.

 公共局部类Form1:MaterialForm{私有只读MaterialSkinManager msManager = null;公共Form1(){InitializeComponent();msManager = MaterialSkinManager.Instance;msManager.AddFormToManage(this);msManager.Theme = MaterialSkinManager.Themes.LIGHT;}私有void Form1_Load(对象发送者,EventArgs e){AddTextFields(panel1,5,new Point(30,10),false);}私有无效AddTextFields(控制父级,int controlsCount,Point startPosition,布尔值ClearExisting){if(clearExisting&& parent.Controls.Count> 0){for(int i = parent.Controls.Count-1; i> = 0; i--){parent.Controls [i] .Dispose();}}int controlHeight = parent.Font.Height + 4;int yIncrement = 0;for(int i = 0; i< controlsCount; i ++){var textField = new MaterialSingleLineTextField(){文字=(i + 1).ToString(),大小=新大小(parent.ClientSize.Width-startPosition.X-4,controlHeight),位置=新Point(startPosition.X,startPosition.Y + yIncrement),锚点= AnchorStyles.Left |AnchorStyles.Top |AnchorStyles.Right};parent.Controls.Add(textField);yIncrement + =(controlHeight + 10);}}私人void matBtnChangeTheme_Click(object sender,EventArgs e){msManager.Theme = MaterialSkinManager.Themes.DARK;msManager.ColorScheme =新的ColorScheme(Primary.Blue600,Primary.Blue900,Primary.Blue500,Accent.LightBlue200,TextShade.WHITE);}私人无效matBtnAddControls_Click(对象发送者,EventArgs e){AddTextFields(panel1,7,new Point(30,10),true);}} 

示例功能:

I would like to add MaterialSingleLineTextField dynamically to a Form.
I have used MaterialSkin NuGet package:

I am trying to create multiple MaterialSkin TextBoxes dynamically on Form.Load. But no Controls are displaying in the hosing Panel.

private void Form1_Load(object sender, EventArgs e)
{
    
    int n = 5;

    int pointX = 30;
    int pointY = 40;
    //panel1.Controls.Clear();
    for (int i = 0; i < n - 1; i++)
    {
        MaterialSingleLineTextField a = new MaterialSingleLineTextField();
        a.Text = (i + 1).ToString();
        a.Visible = true;
        a.Location = new Point(pointX, pointY);
        panel1.Controls.Add(a);
        panel1.Show();
        pointY += 20;
    }
}

This code block works perfectly fine for normal TextBoxes.
Is there any way to add MaterialSingleLineTextField dynamically?

解决方案

A sample Form initialization with default Theme values.

  • The MaterialSkinManager is initialized in the Form Constructor, setting the Theme to MaterialSkinManager.Themes.LIGHT and default color scheme. The Form base Type is set to MaterialForm.
  • A specified number of MaterialSingleLineTextField controls is added to a parent container (a Panel), starting from a defined Location downwards. These Controls are anchored to the parent and the Height is set to Parent.Font.Height + 4.
    • It's important that you specify the Size of these Controls, otherwise you'll get a minimal size that prevents the Controls from showing their content.
  • As you can see in the AddTextFields() method, it's important that you dispose of the previous Controls added to the Parent container, if you want to replace the existing with new ones. Even more important using this Library.
    Calling the Clear() method of a Control.Controls collection (as in the line you have commented out) doesn't dispose of anything, those controls are still alive.

public partial class Form1 : MaterialForm
{
    private readonly MaterialSkinManager msManager = null;

    public Form1()
    {
        InitializeComponent();
        msManager = MaterialSkinManager.Instance;
        msManager.AddFormToManage(this);
        msManager.Theme = MaterialSkinManager.Themes.LIGHT;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        AddTextFields(panel1, 5, new Point(30, 10), false);
    }

    private void AddTextFields(Control parent, int controlsCount, Point startPosition, bool ClearExisting)
    {
        if (clearExisting && parent.Controls.Count > 0) {
            for (int i = parent.Controls.Count - 1; i >= 0; i--) {
                parent.Controls[i].Dispose();
            }
        }

        int controlHeight = parent.Font.Height + 4;
        int yIncrement = 0;

        for (int i = 0; i < controlsCount; i++) {
            var textField = new MaterialSingleLineTextField() {
                Text = (i + 1).ToString(),
                Size = new Size(parent.ClientSize.Width - startPosition.X - 4, controlHeight),
                Location = new Point(startPosition.X, startPosition.Y + yIncrement),
                Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right
            };
            parent.Controls.Add(textField);
            yIncrement += (controlHeight + 10);
        }
    }

    private void matBtnChangeTheme_Click(object sender, EventArgs e)
    {
        msManager.Theme = MaterialSkinManager.Themes.DARK;
        msManager.ColorScheme = new ColorScheme(Primary.Blue600, Primary.Blue900, Primary.Blue500, Accent.LightBlue200, TextShade.WHITE);
    }

    private void matBtnAddControls_Click(object sender, EventArgs e)
    {
        AddTextFields(panel1, 7, new Point(30, 10), true);
    }
}

Sample functionality:

这篇关于如何动态创建 MaterialSingleLineTextField 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆