c# - 使用 C# 以单一形式动态创建多个文本框?

标签 c# winforms dynamic textbox class-library

我的目标是动态创建一个包含 TextBox、Button 的 .dll 文件,任何人都可以在使用 Visual C# 的程序中使用它。

它将在类库中创建,不会使用 WFA 工具。

我需要帮助创建一个可以根据用户提供的属性生成多个 TextBox 的表单。

1)文本框数量 2)位置 3)尺寸等

代码

CLASS.CS

 using System;
 using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;


namespace Forms
{
public class TextForm : Form
{
    public TextBox txtBox1;        
    public TextForm(int a, int b, int c, int d, string e)
    {

        Form f1 = new Form();
        txtBox1 = new TextBox();
        txtBox1.Visible = true;
        //f1.ActiveControl=txtBox1;
        f1.Controls.Add(txtBox1);
        txtBox1.Focus();
        f1.Visible = true;
            txtBox1.Size = new Size(a, b);
            txtBox1.Location = new Point(c, d);
            txtBox1.Text = (e).ToString();
            this.Controls.Add(txtBox1);
            txtBox1.Visible = true;

    }
    [STAThread]
    static void Main()
    { 
        Application.EnableVisualStyles();
    }
  }}

程序.CS

using System;
using Forms;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
class Program
{

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        TextForm Box1 = (new TextForm(150, 14, 20, 32, "This is a TextBox 1"));
        TextForm Box2 = (new TextForm(180, 34, 40, 52, "This is a TextBox 2"));

    }}}

代码应该是什么?

最佳答案

问题是您要为每个 TextBox 创建一个 Form。这不是您想要的,前提是您计划拥有包含多个文本框的表单。

我看到了两种可能性:您要么想要创建 a) 可以轻松添加到表单中的文本框,要么 b) 带有文本框的表单。

public class TextInput : Form
{
    public TextBox TxtBox {
        get; private set;
    }

    public Control Container {
        get; private set;
    }

    public TextInput(Control c, int a, int b, int c, int d, string e)
    {
        this.Container = c;
        this.TxtBox = new TextBox();

        var txtBox1 = this.TxtBox;
        txtBox1.Visible = true;
        c.Controls.Add(txtBox1);
        txtBox1.Focus();
        txtBox1.Size = new Size(a, b);
        txtBox1.Location = new Point(c, d);
        txtBox1.Text = (e).ToString();
        txtBox1.Visible = true;
    }
}

您可以按如下方式使用它:

var f = new Form();
var txtBox1 = new TextInput( f, 100, 25, 10, 10, "Name" );
var txtBox1 = new TextInput( f, 100, 25, 10, 50, "Age" );
var txtBox1 = new TextInput( f, 100, 25, 10, 100, "Address" );
var txtBox1 = new TextInput( f, 100, 25, 10, 150, "Phone" );

在我看来,第二种可能性要有趣得多。您想要创建一个特殊的表单,只要您调用一个简单的方法,它就会自动添加文本框。不过,我将简化您的代码。在表单中使用绝对定位(根本)不是一个好主意。

以下代码创建了一个带有文本框及其标签的表单。文本框占据了表单的整个宽度。这是通过使用 TableLayoutPanel 实现的,其中每行使用一个 Panel subPanel。 此子面板 包含一个标签和一个文本框。

public class InputForm: Form {
        public InputForm()
        {
            this.Panel = new TableLayoutPanel{ Dock = DockStyle.Fill };            
            this.textBoxes = new List<TextBox>();
            this.Controls.Add( this.Panel );
        }

        public TextBox AddTextBox(string label)
        {
            var subPanel = new Panel { Dock = DockStyle.Top };
            var lblLabel = new Label { Text = label, Dock = DockStyle.Left };
            var tbEdit = new TextBox{ Dock = DockStyle.Fill };

            subPanel.Controls.Add( tbEdit );
            subPanel.Controls.Add( lblLabel );
            this.Panel.Controls.Add( subPanel );

            return tbEdit;
        }

        public TableLayoutPanel Panel {
            get; private set;
        }

        public TextBox[] TextBoxes {
            get {
                return this.textBoxes.ToArray();
            }
        }

        private List<TextBox> textBoxes;
    }

您可以通过以下简单代码使用它:

        var form = new InputForm();

        var tbName = form.AddTextBox( "Name" );
        var tbAge = form.AddTextBox( "Age" );
        var tbAddress = form.AddTextBox( "Address" );

        form.Show();
        Application.Run( form );

The resulting form.

如果您想为要创建的文本框提供一些属性(颜色、字体、粗体...),那么您有两种方法。第一个是向 AddTextBox() 方法添加参数,但随着属性数量的增加,这种方法的扩展效果不会很好。另一种方法是创建一个 TextBoxAttributes 类,它将保存给定 TextBox 的配置属性。

public class InputForm: Form {
    public class TextBoxAttributes {
        public TextBoxAttributes() {
            this.ForeColor = DefaultForeColor;
            this.BackColor = DefaultBackColor;
            this.Font = DefaultFont;
        }

        public Color ForeColor {
            get; set;
        }

        public Color BackColor {
            get; set;
        }

        public Font Font {
            get; set;
        }

        public bool Bold {
            get {
                return this.Font.Bold;
            }
            set {
                var style = FontStyle.Regular;

                if ( value ) {
                    style = FontStyle.Bold;
                }

                this.Font = new Font( this.Font, style );
            }
        }

        public bool Italic {
            get {
                return this.Font.Bold;
            }
            set {
                var style = FontStyle.Regular;

                if ( value ) {
                    style = FontStyle.Italic;
                }

                this.Font = new Font( this.Font, style );
            }
        }

        public bool Underline {
            get {
                return this.Font.Bold;
            }
            set {
                var style = FontStyle.Regular;

                if ( value ) {
                    style = FontStyle.Underline;
                }

                this.Font = new Font( this.Font, style );
            }
        }

        public float FontSize {
            get {
                return this.Font.Size;
            }
            set {
                this.Font = new Font( this.Font.FontFamily, value );
            }
        }
    }

    // ... more things...

    public TextBox AddTextBox(string label)
        => this.AddTextBox( label, new TextBoxAttributes() );

    public TextBox AddTextBox(string label, TextBoxAttributes attr)
    {
        var subPanel = new Panel { Dock = DockStyle.Top };
        var lblLabel = new Label { Text = label, Dock = DockStyle.Left };
        var tbEdit = new TextBox{
            Dock = DockStyle.Fill,
            ForeColor = attr.ForeColor,
            BackColor = attr.BackColor,
            Font = attr.Font
        };

        subPanel.Controls.Add( tbEdit );
        subPanel.Controls.Add( lblLabel );
        this.Panel.Controls.Add( subPanel );

        return tbEdit;
    }

    // ... more things...
}

主要代码是:

        public static void Main()
        {
            var form = new InputForm();

            var tbName = form.AddTextBox( "Name", new InputForm.TextBoxAttributes {
                ForeColor = Color.Yellow,
                BackColor = Color.Blue
            });
            var tbAge = form.AddTextBox( "Age", new InputForm.TextBoxAttributes {
                ForeColor = Color.Green,
                BackColor = Color.Black,
                Bold = true
            });
            var tbAddress = form.AddTextBox( "Address" );

            form.Show();
            Application.Run( form );
        }

Text boxes with modified attributes.

希望这对您有所帮助。

关于c# - 使用 C# 以单一形式动态创建多个文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782241/

相关文章:

C# 锁定和一般线程设计问题

c# - 为什么要使用 LinesOfCodeAvoidedAttribute 类?

c# - 如何在 gridview 中创建密码类型的列?

ajax - docpad 系统中的静态与动态内容 - 如何创建动态内容?

c# - 异步 MVC Controller 和 HttpTaskAsyncHandler

c# - 如何在 C++ 中将整数转换为字节,以便 Unity 在通过 UDP 传输后可以理解它们

c# - 向面板添加垂直滚动条

c# - 如何将 Properties.Settings.Default 的副本保存到变量?

c#-4.0 - 使用 FieldInfo.SetValue 和 DynamicObject 作为参数 2

c - C语言二维数组的动态内存分配