Search code examples
c#winformsdynamictextboxclass-library

Create multiple textbox dynamically in single form using c#?


My aim is to create a .dll file dynamically having TextBox,Button which can be used by anyone in a program using Visual C#.

It would get created in Class Library, No WFA tools would get used.

I need help in creating a form which can generate multiple TextBox according to the attributes provided by the user.

1)No of TextBox 2)Location 3)Size etc

Code

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();
    }
  }}

PROGRAM.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"));

    }}}

What should be the code?


Solution

  • The problem is that you are creating a Form for each TextBox. This is not what you want, provided that you plan to have forms with multiple text boxes.

    I see two possibilities: you either want to create a) a textbox that you can easily add to your form, or b) a form with textboxes.

    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;
        }
    }
    

    You'd use this as follows:

    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" );
    

    The second possibility is much more interesting, in my opinion. You want to create a special Form that automatically adds text boxes as soon a you call a simple method. I'm going to simplify your code, though. It is not a good idea (at all), to use absolute positioning in your forms.

    The following creates a form with text boxes and their labels. The textboxes occupy the whole width of the form. This is achieved by using a TableLayoutPanel in which a Panel subPanel is used for each row. This subPanel holds a label and a text box.

    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;
        }
    

    You can use this with the following simple code:

            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.

    If you'd like to give a few attributes to the text boxes to be created (colors, font, bold...), then you have two ways. The first one is to add parameters to the AddTextBox() method, though that would not scalate well as long as the number of attributes grows. The alternative is to create a TextBoxAttributes class, which will hold the configuring attributes for a given 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...
    }
    

    The main code would be:

            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.

    Hope this helps.