[C #] Controls dynamically added in the source –2 Identifies and references dynamically added text boxes

5 minute read

I want to dynamically add controls to a form and use them

C # article

-Use MySQL in C #– 1. Development environment installation
-Use MySQL in C #– 2. SELECT / screen display
-Use MySQL in C #– 3. Additions and removals
-Determining whether database data is NULL in C #
-C # delegate and => (lambda expression) are treated like pointers to C functions
-Dynamic add control– 1. Button click
–Dynamic addition of controls –2 Reference of text box [This article]

Last time I dynamically added a button control to the form in the source and used a click event, but this time I applied it , And dynamically add textboxes to browse them

Dynamically add a textbox and browse for it

Try adding a new text box

Now that we’ve added a button dynamically, we’ve added a textbox dynamically to see if it can be referenced by others (˙꒳ ˙ᐢ). When you dynamically reference a textbox from the source, you have to assign a Distinguished Name to Name for each textbox **. Originally, it is handled by the configuration file and database, but suddenly the story becomes complicated, so this time it is a method verification program. .. ..

フォームデザイン
This time, a new “button Check Entry” button has been added! Click this button to create the source code so that it will pick up the contents of the dynamically added text form (it will be grayed out before dynamically creating the text form).

Form1.cs


using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyTrainingCsFrm1
{
    public partial class Form1 : Form
    {
        //Button to create by calling form(Windows Forms Buttons-Inherit Button)
        private Kazumi75Button[] manyButtons;

        //Text box created by form call and its distinguished name
        private TextBox[] manyTextBoxes;
        private string[] txtbxNm;

        //Number of elements in the array(8 here)
        private const int ElementNum = 8;

        public Form1()
        {
            InitializeComponent();
            this.manyButtons = null;
            this.manyTextBoxes = null;
            this.txtbxNm = null;
        }

        //Button to call form
        private void formCallButton_Click(object sender, EventArgs e)
        {
            if (this.manyButtons != null || this.manyTextBoxes != null)
            {
                MessageBox.Show("Already displayed");
                return;
            }

            //Each button message is preset here
            string[] msgs = new string[ElementNum];
            msgs[0] = "Uraga";
            msgs[1] = "Kamakura";
            msgs[2] = "Misaki";
            msgs[3] = "Kannonzaki";
            msgs[4] = "Yokosuka Chuo";
            msgs[5] = "Jogashima";
            msgs[6] = "Shichirigahama";
            msgs[7] = "Umahori";

            //Each distinguished name in the text box is preset here
            this.txtbxNm = new string[ElementNum];
            this.txtbxNm[0] = "txbUraga";
            this.txtbxNm[1] = "txbKamakura";
            this.txtbxNm[2] = "txbMisaki";
            this.txtbxNm[3] = "txbKannonzaki";
            this.txtbxNm[4] = "txbYokosuka";
            this.txtbxNm[5] = "txbJogashima";
            this.txtbxNm[6] = "txbShichirigahama";
            this.txtbxNm[7] = "txbMabori";

            //button(Inherit Windows Forms Buttons)And textbox creation
            this.manyButtons = new Kazumi75Button[ElementNum];
            this.manyTextBoxes = new TextBox[ElementNum];

            //Property settings and form placement for each element of the button and text box
            for (int i = 0; i < this.manyButtons.Length; i++)
            {
                //Instance creation
                this.manyButtons[i] = new Kazumi75Button();
                this.manyTextBoxes[i] = new TextBox();
                
                //Set button name and text properties
                this.manyButtons[i].Name = "OriginalButton" + i;
                this.manyButtons[i].Text = "button" + i;
                //Set text box name and text properties
                this.manyTextBoxes[i].Name = txtbxNm[i];
                this.manyTextBoxes[i].Text = "(" + (i + 1) + "Put something in the second)";

                //Specify the list box to be referenced when the button is clicked
                this.manyButtons[i].targetLbox = listBox1;

                //Set the message when the button is clicked
                this.manyButtons[i].buttonMsg = msgs[i];

                //Button size and placement
                this.manyButtons[i].Size = new Size(100, 20);
                this.manyButtons[i].Location = new Point(10, 10 + i * 22);
                //Text box size and placement
                this.manyTextBoxes[i].Size = new Size(150, 20);
                this.manyTextBoxes[i].Location = new Point(120, 10 + i * 22);

                //Add to form
                this.Controls.Add(this.manyButtons[i]);
                this.Controls.Add(this.manyTextBoxes[i]);

                //Create an event behavior for each button on click
                this.manyButtons[i].eventMaking();
            }

            //After calling the form, enable the content confirmation button of the text box
            buttonCheckEntry.Enabled = true;
        }

        //Button to check the contents of the dynamically called text box
        private void buttonCheckEntry_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            
            //Find the text box by distinguished name and get its contents
            for (int i = 0; i < ElementNum; i++)
            {
                sb.Append("[" + i + "]");
                
                //Find the control that matches the distinguished name on the form
                Control[] txtboxctrls = this.Controls.Find(this.txtbxNm[i], false);

                for (int j = 0; j < txtboxctrls.Length; j++)
                {
                    //Get the contents of the obtained text box as a character string
                    TextBox tbox = (TextBox)txtboxctrls[j];

                    sb.Append(tbox.Text);
                }
                
                //If it is not the final element, "/Separated by
                if (i < ElementNum - 1)
                {
                    sb.Append(" / ");
                }
            }

            //Display a message
            MessageBox.Show("What I entered ..." + sb.ToString() + "is.");
        }
    }
}

First of all, the place to set the identification name in the text box, which should be acquired in the database or configuration file

this.txtbxNm = new string[ElementNum];
this.txtbxNm[0] = "txbUraga";
this.txtbxNm[1] = "txbKamakura";
this.txtbxNm[2] = "txbMisaki";
this.txtbxNm[3] = "txbKannonzaki";
this.txtbxNm[4] = "txbYokosuka";
this.txtbxNm[5] = "txbJogashima";
this.txtbxNm[6] = "txbShichirigahama";
this.txtbxNm[7] = "txbMabori";
this.manyTextBoxes[i].Name = txtbxNm[i];

If you don’t do this, you won’t be able to see the textbox first. This is because the ** button buttonCheckEntry click event searches for the corresponding text box by finding the distinguished name **.

Control[] txtboxctrls = this.Controls.Find(this.txtbxNm[i], false);

The arguments of the Find method are ** distinguished name and flag ** to find child elements, respectively. Since only the text form is used here, the child elements below it are not conscious, so set it to false. If an element is found, the number of elements in txtboxctrls above will be greater than 0 (1 or greater).

実行結果

This was also successful.
Finally, check if the number of elements can be changed dynamically.

Try changing the number of elements dynamically

Form1.Corrected part of cs


//Number of elements in the array(11 here)
private const int ElementNum = 11;

 …(Omission)

string[] msgs = new string[ElementNum];
msgs[0] = "Uraga";
msgs[1] = "Kamakura";
msgs[2] = "Misaki";
msgs[3] = "Kannonzaki";
msgs[4] = "Yokosuka Chuo";
msgs[5] = "Jogashima";
msgs[6] = "Shichirigahama";
msgs[7] = "Umahori";
msgs[8] = "Hashirimizu";
msgs[9] = "Zushi";
msgs[10] = "Hayama";

this.txtbxNm = new string[ElementNum];
this.txtbxNm[0] = "txbUraga";
this.txtbxNm[1] = "txbKamakura";
this.txtbxNm[2] = "txbMisaki";
this.txtbxNm[3] = "txbKannonzaki";
this.txtbxNm[4] = "txbYokosuka";
this.txtbxNm[5] = "txbJogashima";
this.txtbxNm[6] = "txbShichirigahama";
this.txtbxNm[7] = "txbMabori";
this.txtbxNm[8] = "txbHashirimizu";
this.txtbxNm[9] = "txbZushi";
this.txtbxNm[10] = "txbHayama";

At the end, increase the number of elements from 8 to 11, then increase the number of buttons to 11, and you will have to identify 11 text boxes, so fix it as described above.

実行結果

Again, I was able to correctly identify the 11 text boxes! !! (˶ ・ ᴗ ・) ੭⚐⚑

next time

This time, I’m tired of mixing the story with the database master and configuration file at the same time, so I focused on how to add controls dynamically, but next I will dynamically control from the database master data I will try to add (˙꒳ ˙ᐢ)

References

-[.NET] Create control at runtime
-[.NET] Dynamically register events
Get control from string representing control name –Access control from string type of string name –C # programming
-Add an event to a (dynamic) control generated from a C # _ program
-Control.ControlCollection.Find (String, Boolean) method –Microsoft .NET Official
-How to search for controls on Windows Forms? [2.0 or later, C #, VB] –atmark IT