using System;
using System.Windows.Forms;
namespace ComboBox下拉控件做浏览器网址输出框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form_Load);
}
private void Form_Load(object sender, EventArgs e)
{
CreateComboBox();
}
private bool EditState = false;//全局变量标识
ComboBox cb=null;
private void CreateComboBox()
{
cb = new ComboBox();
cb.Items.Add("http://www.baidu.com");
cb.Items.Add("http://www.163.com");
cb.Items.Add("http://www.qq.com");
cb.Parent = this;
cb.Left = 50;
cb.Top = 50;
cb.Width = 300;
this.Controls.Add(cb);
cb.TextChanged += new EventHandler(CombBoxTextChange);
cb.KeyDown += new KeyEventHandler(ComboBox_KeyDown);
}
/// <summary>
/// 当文字改变时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CombBoxTextChange(object sender, EventArgs e)
{
if (EditState)
{
if (cb != null)
{
string text = cb.Text;
int index = cb.FindString(text);
if (index > -1)
{
EditState = false;
cb.SelectedIndex = index;
EditState = true;
cb.Select(text.Length, text.Length);
}
}
}
}
/// <summary>
/// 当在框中输出内容时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
EditState = (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete);
cb.DroppedDown = true;
}
}
}
用ComboBox制作浏览器输入框效果
最新推荐文章于 2021-06-03 05:53:23 发布