Solved

Dynamically creation of textBox

Posted on 2010-09-14
7
431 Views
Last Modified: 2013-12-16
Experts
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,
error.JPG
0
Comment
Question by:ANINDYA
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:kris_per
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?


0
 

Author Comment

by:ANINDYA
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

0
 
LVL 16

Expert Comment

by:kris_per
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?


0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ANINDYA
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
0
 

Author Comment

by:ANINDYA
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
Anindya
0
 
LVL 16

Accepted Solution

by:
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()
        {
            InitializeComponent();

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

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

                SqlDataAdapter a = new SqlDataAdapter(c);

                dataTable = new DataTable();

                a.Fill(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)
            {
                this.Controls.Remove(t);
            }
            foreach (Label l in labels)
            {
                this.Controls.Remove(l);
            }

            // 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] + ":";
                this.Controls.Add(label);
                labels.Add(label);

                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];
                this.Controls.Add(textBox);
                textBoxes.Add(textBox);

                if ((f + 1) % textBoxesPerLine == 0)
                {
                    controlStartX = startX;
                    controlStartY += 25;
                }
                else
                {
                    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)
                {
                    fieldList.Add(splits[f]);
                }
            }

            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))
            {
                connection.Open();

                SqlCommand c = new SqlCommand(query, connection);

                SqlDataAdapter a = new SqlDataAdapter(c);

                dataTableResults = new DataTable();

                a.Fill(dataTableResults);
            }

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

Open in new window

0
 

Author Closing Comment

by:ANINDYA
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
Bangalore
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.

791 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