Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Two field in my database table "name" and "mobile" no. and two textbox in form .my 1st testbox is AutoComplete textBox by fetching name from database. I want , when i select name in text box then load mobile no in 2nd text box .... can some one help this ?

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            AutoComplete();
        }
        public void AutoComplete()
        {
            SqlConnection conn = new SqlConnection(@"Database Source.....");
            conn.Open();
            AutoCompleteStringCollection AutoItem = new AutoCompleteStringCollection();
            SqlCommand command = new SqlCommand("select * from table1",conn);
            SqlDataAdapter adp = new SqlDataAdapter(command);
            DataTable tbl = new DataTable();
            adp.Fill(tbl);
            foreach (DataRow rw in tbl.Rows)
            {
                AutoItem.Add(rw["name"].ToString());
            }
                textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.AutoCompleteCustomSource = AutoItem;
	}
Posted
Updated 22-Apr-14 2:05am
v3
Comments
Ankur\m/ 22-Apr-14 8:03am    
What is the problem with the code you have given?
guru14 22-Apr-14 14:12pm    
Mr ankur no problem in my code. i want mobile no in second textbox based on first textbox selection.so please tell me how to do this
BulletVictim 22-Apr-14 9:50am    
Try doing that on the textbox leave event or you will be doing multiple checks for every key press in the textbox otherwise you might need to use a drop down list.
gggustafson 22-Apr-14 11:59am    
But doing checks on each key is what the OP should be doing!

1 solution


My response to BulletVictim was incorrect. The AutoComplete functionality of the TextBox takes care of the character-by-character input of the user. And BulletVictim is correct in suggesting that you need a Leave event handler.



The solution that I provide is somewhat like the MSDN documentation TextBox.AutoCompleteSource Property [^].



First the code you seek:


C#
//#define SQL

using System;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace AutoComplete
    {

    // *************************************************** class Form1

    public partial class Form1 : Form
        {

        TextBox     autocomplete_textbox = null;

        // ***************************************************** Form1

        public Form1 ( )
            {
            InitializeComponent ( );

            if ( autocomplete_textbox != null )
                {
                autocomplete_textbox.Dispose ( );
                autocomplete_textbox = null;
                }
            }

        // ******************************* create_autocomplete_textbox

        TextBox create_autocomplete_textbox ( 
                                AutoCompleteStringCollection source )
            {
            TextBox text_box = new TextBox
                              {
                              AutoCompleteCustomSource = source,
                              AutoCompleteMode = 
                                  AutoCompleteMode.SuggestAppend,
                              AutoCompleteSource =
                                  AutoCompleteSource.CustomSource,
                              Location = new Point ( 20, 20 ),
                              Width = ( ClientRectangle.Width - 40 ),
                              Visible = true
                              };
            return ( text_box );
            }
#if SQL
        // ************************************************ Form1_Load

        private void Form1_Load ( object    sender, 
                                  EventArgs e )
            {
            AutoCompleteStringCollection    names;
            string                          command_text;
            string                          connection_string;
            DataTable                       data_table;
            SqlCommand                      sql_command;
            SqlConnection                   sql_connention;
            SqlDataAdapter                  sql_data_adapter;


            connection_string = @"Database Source.....";
            sql_connention = new SqlConnection ( connection_string );
            sql_connention.Open ( );
            command_text = "select * from table1";
            sql_command = new SqlCommand ( command_text,
                                           sql_connention );
            sql_data_adapter = new SqlDataAdapter ( sql_command );
            data_table = new DataTable ( );
            sql_data_adapter.Fill ( data_table );
                                        // names are now in data_table

                                        // create the autocomplete 
            names = new AutoCompleteStringCollection ( );
            foreach ( DataRow data_row in data_table.Rows )
                {
                names.Add ( data_row [ "name" ].ToString ( ) );
                }
                                        // create the textbox
            autocomplete_textbox = create_autocomplete_textbox ( 
                                                          names );
            autocomplete_textbox.Leave += 
                new EventHandler ( 
                    autocomplete_textbox_Leave );

            this.Controls.Add ( autocomplete_textbox );
            }
#else
        // ************************************************ Form1_Load

        // http://msdn.microsoft.com/en-us/library/
        //     system.windows.forms.textbox.
        //     autocompletesource(v=vs.90).aspx

        private void Form1_Load ( object sender,
                                  EventArgs e )
            {
                                        // Create the list to use as 
                                        // the custom source. 
            var months = new AutoCompleteStringCollection ( );
            months.AddRange ( new string [ ] { "January",
                                               "February",
                                               "March",
                                               "April",
                                               "May",
                                               "June",
                                               "July",
                                               "August",
                                               "September",
                                               "October",
                                               "November",
                                               "December"
                                               } );
                                        // initialize the text box
            autocomplete_textbox = create_autocomplete_textbox ( 
                                                            months );
            autocomplete_textbox.Leave += 
                new EventHandler ( 
                    autocomplete_textbox_Leave );

            this.Controls.Add ( autocomplete_textbox );
            }
#endif
        // ******************************** autocomplete_textbox_Leave

        // remember that this event handler is invoked only when there 
        // is more than one control on the form

        void autocomplete_textbox_Leave ( object    sender, 
                                         EventArgs e )
            {
            string  chosen_name = String.Empty;
            TextBox text_box = ( TextBox ) sender;

            chosen_name = text_box.Text;

            MessageBox.Show ( "Chosen: " + chosen_name );
            }

        // ******************************************** exit_BUT_Click

        private void exit_BUT_Click ( object    sender, 
                                      EventArgs e )
            {

            Application.Exit ( );
            }

        } // class Form1

    } // namespace AutoComplete


There are two Form1_Load event handlers. Only one is compiled depending on whether or not the symbol SQL is defined. I added the directives because I do not know your connection string. If you remove the C# preprocessor directives, make sure that you remove the second Form1_Load event handler.



Leaving the code the way it is, all you need is a Form (Form1) and an exit button (exit_BUT) on your form. Leave room at the top for the autocomplete textbox.



I was surprised that I needed to create the TextBox dynamically. But that was my initial problem.



Hope that helps.

 
Share this answer
 
Comments
guru14 23-Apr-14 9:23am    
Thanks @gggustafson.
gggustafson 23-Apr-14 10:55am    
Please mark as solved
gggustafson 22-May-14 14:30pm    
Once again, please mark as solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900