当前位置: 代码迷 >> C# >> 自定义ComboBox控件,完美解决C#自带的ComboBox效率慢的有关问题
  详细解决方案

自定义ComboBox控件,完美解决C#自带的ComboBox效率慢的有关问题

热度:104   发布时间:2016-05-05 03:09:15.0
自定义ComboBox控件,完美解决C#自带的ComboBox效率慢的问题

自定义ComboBox控件,完美解决C#自带的ComboBox效率慢的问题

 欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

在做项目的时候,用到了ComboBox来做下拉列表的功能;但是在用的时候发现C#自带的ComboBox效率非常的慢!


当然我添加的是股票、期货的合约数据,大概有几千条信息,算是比较大的。如果用到的数据很少,就可以直接用C#自带的。

 

C#自带的combobox之所以慢,是因为内部做了大量的转型、匹配工作。具体这里就不分析了。有兴趣的朋友可以去找找资料研究一下。

 

我这里用了WPF做了一个自定义combobox控件。自己写了一个和C#自带的combobox一样功能的空间,但是效率却非常快!废话不多说,直接看代码:

 欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

前台代码:

<UserControl x:Class=" MyComboBox"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             Loaded="UserControl_Loaded"

             >

欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

    <Grid>

        <DockPanel>

            <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1">

               <StackPanel Orientation="Horizontal">

                    <TextBox Margin="0,0,1,0" Name="textBox_ComboBox" KeyUp="textBox_ComboBox_KeyUp" />

                    <Button Width="13" Name="button_drop" HorizontalAlignment="Right" FontSize="7" Click="button_drop_Click"></Button>

               </StackPanel>

            </Border>

            <ListBox DockPanel.Dock="Top" Margin="0,1,0,0" Name="listBox_ComboBox" Visibility="Collapsed" MouseDoubleClick="listBox_ComboBox_MouseDoubleClick" />         

        </DockPanel>

    </Grid>

</UserControl>

 欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

后台代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace MyComboBox_real

{

    ///<summary>

    /// Interaction logic for MyComboBox.xaml

    ///重写的一具有部分combobox功能¨?¨???mycombobox,¨??¨¨-combobox?¨?¨¤??ê?

    ///¨?¤??

    ///</summary>

    public partial class MyComboBox : UserControl

    {

        #region 内¨?部?类¤¨¤

        ///<summary>

        /// Items????館¤

        ///</summary>

        public class MyItems

        {

            #region 重?要°a变??量??

            ///<summary>

            ///????é???Y?¤????string¤¨¤¨????yY

            ///</summary>

            private List<string> listItemMSG = new List<string>();

            ///<summary>

            ///????é???Y?¤????string¤¨¤¨????yY

            ///</summary>

            public List<string> ListItemMSG

            {

                get { return this.listItemMSG; }

                set { this.listItemMSG = value; }

            }

            #endregion

 

            #region 构1造¨?函??数?y

            ///<summary>

            ///T?1¨????y

            ///</summary>

            public MyItems()

            {

 

            }

            #endregion

 欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

            #region 业°??务?处?|理¤¨?函??数?y

 

            ///<summary>

            ///¤???????¨???¤???¨¤???¨??D??¤???

            ///</summary>

            ///<param name="msg"></param>

            ///<returns></returns>

            public List<string> getMatchDatas(string msg)

            {

                return this.ListItemMSG.Where

                    (

                    f => ((f.Length >= msg.Length) && (((f.Substring(0, msg.Length)).ToLower()) == (msg.ToLower())))

                    ).ToList<string>();

            }

 

            ///<summary>

            ///?¨?¨?string¤¨¤¨????yY

            ///</summary>

            ///<param name="msg">?yY</param>

            ///<returns>¨?|¤???1</returns>

            public int add(string msg)

            {

                this.ListItemMSG.Add(msg);

                return 1;

            }

 

            ///<summary>

            ///?yitems

            ///</summary>

            ///<returns></returns>

            public int clear()

            {

                this.ListItemMSG.Clear();

                return 1;

            }

            #endregion

        }

 

        #endregion

 

        #region 重?要°a变??量??

        ///<summary>

        ///???|¤¨?

        ///</summary>

        public MyItems myItems = new MyItems();

 

        #endregion

 

        #region 构1造¨?函??数?y

        ///<summary>

        ///T?1¨????y

        ///</summary>

        public MyComboBox()

        {

            InitializeComponent();

            //this.listBox_ComboBox.ItemsSource = this.myItems.getMyItems();

        }

 

        #endregion

 

        #region 业°??务?处?|理¤¨?函??数?y

        ///<summary>

        ///¨¤?¨?¨?¨????

        ///</summary>

        ///<param name="sender"></param>

        ///<param name="e"></param>

        private void textBox_ComboBox_KeyUp(object sender,KeyEventArgs e)

        {

            TextBox tb = senderasTextBox;

 

            if (tb == null)

            {

                return;

            }

 

            //  ¨?¨?“??↑¨?”???é“??←?”??¨?

            if ((e.Key == Key.Up) || (e.Key == Key.Left))

            {

                if (this.listBox_ComboBox.SelectedIndex > 0)

                {

                    this.listBox_ComboBox.SelectedIndex--;

                }

            }

            //  ¨?¨?“??↓y”???é“??→¨?”??¨?

            else if ((e.Key == Key.Down) || (e.Key == Key.Right))

            {

                if (this.listBox_ComboBox.SelectedIndex < (this.listBox_ComboBox.Items.Count - 1))

                {

                    this.listBox_ComboBox.SelectedIndex++;

                }

            }

            // ???¨?

            else if (e.Key == Key.Enter)

            {

                tb.Text = this.listBox_ComboBox.SelectedItem ==null ? tb.Text :this.listBox_ComboBox.SelectedItem.ToString();

                this.listBox_ComboBox.Visibility =Visibility.Collapsed;

            }

            else

            {

                if ((tb.Text != null) && (tb.Text != ""))

                {

                    if (this.myItems.getMatchDatas(tb.Text).Count > 0)

                    {

                        this.listBox_ComboBox.ItemsSource =this.myItems.getMatchDatas(tb.Text);

                        this.listBox_ComboBox.Visibility =Visibility.Visible;

                    }

                    else

                    {

                        this.listBox_ComboBox.Visibility =Visibility.Collapsed;

                    }

                }

                else

                {

                    this.listBox_ComboBox.Visibility =Visibility.Collapsed;

                }

            }

 

            // ??a?¨???

            tb.Select(tb.Text.Length, 1);

        }

 

        ///<summary>

        ///??¨?¨Y

        ///</summary>

        ///<param name="sender"></param>

        ///<param name="e"></param>

        private void listBox_ComboBox_MouseDoubleClick(object sender,MouseButtonEventArgs e)

        {

            ListBox lb = senderasListBox;

            if (lb == null)

            {

                return;

            }

 

            if (lb.SelectedItem ==null)

            {

                lb.Visibility = Visibility.Collapsed;

                return;

            }

 

            this.textBox_ComboBox.Text = lb.SelectedItem.ToString();

            lb.Visibility = Visibility.Collapsed;

            // ??a?¨???

            this.textBox_ComboBox.Select(this.textBox_ComboBox.Text.Length, 1);

        }

        #endregion

 欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

        #region 私?有?D函??数?y

        ///<summary>

        ///???t¨?¨¨

        ///</summary>

        ///<param name="sender"></param>

        ///<param name="e"></param>

        private void UserControl_Loaded(object sender, RoutedEventArgs e)

        {

            this.textBox_ComboBox.Width =this.ActualWidth - 1 -this.button_drop.ActualWidth;

        }

 

        ///<summary>

        ///a?????¤-

        ///</summary>

        ///<param name="sender"></param>

        ///<param name="e"></param>

        private void button_drop_Click(object sender, RoutedEventArgs e)

        {

            if (this.listBox_ComboBox.Visibility ==Visibility.Visible)

            {

                this.listBox_ComboBox.Visibility =Visibility.Collapsed;

            }

            else

            {

                this.listBox_ComboBox.ItemsSource =this.myItems.ListItemMSG;

                this.listBox_ComboBox.Visibility =Visibility.Visible;

            }

        }

 

        #endregion

 

 

    }

}

 

 

 欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

效果图:

 


欢迎关注http://blog.csdn.net/aaa123524457

转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675

需要注意的一点是:如果是在winform里面添加这个自定义控件,需要稍微把自定义控件的高度拉长一点,不然会被遮住的哦;如果在wpf里面引用就无所谓了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案