Dynamically creation of textBox

Posted on 2010-09-14
Last Modified: 2013-12-16
My question is
how to create dynamically textboxes and how those textboxes will pass values to queries.
For clarification of my question please see the attached image.

hoping for a vivid explanation as I have never done so.
Thanking you,
Question by:ANINDYA
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 16

Expert Comment

ID: 33672832

Some questions to understand it better:

For example, when the user selects 'Opp By Title', the query is:
select * from Table_Opportunity where opportunity_title like '%[title]%'
For this query, the number of text boxes to create is ONE. Is that right?

Can give you an example for the case of two text boxes?

Why do you create the textboxes/to use where?


Author Comment

ID: 33672919
Expert kris per
Thanks for replying.

Opp by Title and Location
Opp by multiple locations ( at that time atleast 4 locations will be an option for the user to put an input)
Opp by CompanyName and Location
Opp by Price range ( At that time atleast 2 textboxes  are required)
Opp by Date ( two dates)

in this way many more will be there .
I am first trying to execute the location option only .
So that I can understand  the concept and then the same later I can use on all other queries.

Thanking you

LVL 16

Expert Comment

ID: 33673188

Can you confirm the following:

For 'Opp by Title and Location', the query will be like:

select * from Table_Opportunity where opportunity_title like '%[title]%' and Location like '%[title]%'

i.e. the query will have an extra 'and Location like '%[title]%' .

Is that correct?

Basically I am trying to understand, number of textboxes depends on what?

Is it like - if the query has two field names in the where expression, then the number of text boxes is two. Can this be taken as a rule. Will it work for you?

And again, how the textboxes will be used after created? Should their value used for some purpose?

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.


Author Comment

ID: 33673271
Expert kris_per
number of textboxes depends on number of input parameters only . Not necessarily they are supposed to have a title name only .
Those parameters can be title,location,date,etc.
I do not know how to do that as they will have separate datatypes .
But sir it is requirement
Thanking you

Author Comment

ID: 33673316
Expert kris_per
I am from my office is closing now. So furthermore I will not be able to reply you instantly.
Tomorrow morning I will again come at ( USA local time night 10PM).
So please do not think I am not replying  you.
Thanking you
LVL 16

Accepted Solution

kris_per earned 500 total points
ID: 33673947

OK. No problem.

Hope the below code is getting you closer to the solution....Let me know for explanation on the code.
public partial class Form1 : Form
        private string connectionString = "your connection string here";
        private DataTable dataTable;
        private List<TextBox> textBoxes = new List<TextBox>();
        private List<Label> labels = new List<Label>();

        public Form7()

            // Get the query details from database
            // and databind to comboBoxOpportunityCategories 
            using (SqlConnection connection = new SqlConnection(connectionString))

                SqlCommand c = new SqlCommand("SELECT * FROM Table_Query", connection);

                SqlDataAdapter a = new SqlDataAdapter(c);

                dataTable = new DataTable();


            comboBoxOpportunityCategories.DataSource = dataTable;
            comboBoxOpportunityCategories.DisplayMember = "QDesc";
            comboBoxOpportunityCategories.ValueMember = "Query";

        private void comboBoxOpportunityCategories_SelectedIndexChanged(object sender, EventArgs e)
            string query = comboBoxOpportunityCategories.SelectedValue.ToString();
            //string query = @"select * from Table_Opportunity where opportunity_title like '%[title]%' and Location like '%[loc]%' and testfield like '%[test]%'  and testfield2 like '%[test2]%'";

            List<string> fieldList = GetFieldListFromQuery(query);

            // clear previous text boxes and labels in the form
            foreach (TextBox t in textBoxes)
            foreach (Label l in labels)

            // start the textboxes from the same Y and to the left of comboBoxOpportunityCategories
            int startY = comboBoxOpportunityCategories.Location.Y;
            int startX = comboBoxOpportunityCategories.Location.X + comboBoxOpportunityCategories.Width + 10;

            int controlStartX = startX;
            int controlStartY = startY;
            int textBoxesPerLine = 3;
            int textBoxWidth = 100;
            int labelWidth = 40;

            for (int f = 0; f < fieldList.Count; f++)
                // add a label before textbox
                Label label = new Label();
                label.Location = new System.Drawing.Point(controlStartX, controlStartY+3);
                label.Name = "labelField" + (f + 1);
                label.AutoSize = false;
                label.Size = new System.Drawing.Size(labelWidth, 20);
                label.Text = fieldList[f] + ":";

                controlStartX += label.Width;

                // add the textbox for the field
                TextBox textBox = new TextBox();
                textBox.Location = new System.Drawing.Point(controlStartX, controlStartY);
                textBox.Name = "textBoxField" + (f+1);
                textBox.Size = new System.Drawing.Size(textBoxWidth, 20);
                textBox.Tag = fieldList[f];

                if ((f + 1) % textBoxesPerLine == 0)
                    controlStartX = startX;
                    controlStartY += 25;
                    controlStartX += textBox.Width + 5;

        private List<string> GetFieldListFromQuery(string query)
            // Logic used here is: The text within a pair of % is the field name.
            // may be better logic can be used later; but for now this works

            string[] splits = query.Split('%');
            List<string> fieldList = new List<string>();
            for (int f = 0; f < splits.Length; f++)
                if (f % 2 == 1)

            return fieldList;

        // When user clicks the Find button after typing values in the textboxes

        private void buttonFind_Click(object sender, EventArgs e)
            string query = comboBoxOpportunityCategories.SelectedValue.ToString();
            //string query = @"select * from Table_Opportunity where opportunity_title like '%[title]%' and Location like '%[loc]%' and testfield like '%[test]%'  and testfield2 like '%[test2]%'";

            // replace the tokens like [title], etc in the query with the values from the textbox
            // to make the full query to use in database.
            foreach (TextBox textBox in textBoxes)
                query.Replace(textBox.Tag.ToString(), textBox.Text);

            DataTable dataTableResults = null;

            using (SqlConnection connection = new SqlConnection(connectionString))

                SqlCommand c = new SqlCommand(query, connection);

                SqlDataAdapter a = new SqlDataAdapter(c);

                dataTableResults = new DataTable();


            // now dataTableResults has the results of the query
            // use dataTableResults as required

Open in new window


Author Closing Comment

ID: 33688693
Expert kris_per
Thanks a lot for the code .
It has really provided me a lot of knowledge and there after I arranged the things properly.

Thanks for the help.
Hope to get the same in future too.
Thanking you,
Anindya Chatterjee

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question