Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Dynamically creation of textBox

Posted on 2010-09-14
Medium Priority
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?

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


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 India.here 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

730 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