在自动填充文本框中选择名称,然后在第二个文本框中加载移动否 [英] select name in autocomplete text box then load mobile no in 2nd text box

查看:67
本文介绍了在自动填充文本框中选择名称,然后在第二个文本框中加载移动否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库表中的两个字段name和mobileno。表单中的两个文本框.my第一个testbox是通过从数据库中提取名称来自动完成textBox。我想,当我在文本框中选择名称然后在第二个文本框中加载手机号码....有人可以帮忙吗?



  private   void  textBox1_TextChanged( object  sender ,EventArgs e)
{
AutoComplete();
}
public void 自动完成()
{
SqlConnection conn = new SqlConnection( @ 数据库源。 ......);
conn.Open();
AutoCompleteStringCollection AutoItem = new AutoCompleteStringCollection();
SqlCommand command = new SqlCommand( select *来自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;
}

解决方案



我对BulletVictim的回答不正确。 TextBox的AutoComplete功能负责用户的逐个字符输入。并且BulletVictim建议你需要一个Leave事件处理程序是正确的。





我提供的解决方案有点像MSDN文档 TextBox.AutoCompleteSource属性 [ ^ ]。





首先是您寻求的代码:

  //   #define SQL  

使用系统;
使用 System.Data;
使用 System.Drawing;
使用 System.Data.SqlClient;
使用 System.Windows.Forms;

命名空间自动完成
{

// ************************************* **************类Form1

public 部分 Form1:表单
{

TextBox autocomplete_textbox = ;

// **************** ************************************* 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 ),
宽度=(Cl ientRectangle.Width - 40 ),
可见= true
};
return (text_box);
}
#if if SQL
// ******************************************** **** Form1_Load

private void Form1_Load(< span class =code-keyword> 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 = @ 数据库来源.....;
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);
// 名称现在位于data_table

// 创建自动填充
names = new AutoCompleteStringCollection();
foreach (DataRow data_row in data_table.Rows)
{
names.Add(data_row [ name] .ToString());
}
// 创建文本框
autocomplete_textbox = create_autocomplete_textbox(
名字);
autocomplete_textbox.Leave + =
new EventHandler(
autocomplete_textbox_Leave);

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

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

private void Form1_Load( object sender,
EventArgs e)
{
// 创建列表用作
// 自定义来源。
var months = new AutoCompleteStringCollection();
months.AddRange( new string [] { 1月
二月
三月
< span class =code-string>
April
可能
6月
July
8月
九月
十月
11月
12月
});
// 初始化文本框
autocomplete_textbox = create_autocomplete_textbox(
几个月);
autocomplete_textbox.Leave + =
new EventHandler(
autocomplete_textbox_Leave);

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

// 请记住,仅当
// 表单上有多个控件

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

selected_name = text_box.Text;

MessageBox.Show( 选择: + selected_name);
}

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

private void exit_BUT_Click( object sender,
EventArgs e)
{

Application.Exit();
}

} // class Form1

} // 命名空间自动完成



有两个 Form1_Load 事件处理程序。根据是否定义了符号SQL,只编译一个。我添加了指令,因为我不知道你的连接字符串。如果删除C#预处理程序指令,请确保删除第二个 Form1_Load 事件处理程序。





按原样保留代码,只需要表单上的Form( Form1 )和退出按钮( exit_BUT )即可。在自动填充文本框的顶部留出空间。





我很惊讶我需要创建TextBox动态。但这是我最初的问题。





希望有所帮助。


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 ?

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;
	}

解决方案


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:

//#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.


这篇关于在自动填充文本框中选择名称,然后在第二个文本框中加载移动否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆