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?

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.


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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

733 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