vb combobox style问题

查看: 703|回复: 2
请问,VB中的combobox通过下拉菜单改变内容时,并不激活Change事件?
阅读权限50
在线时间 小时
我的现象是:
1.通过程序赋值,可以激活Combobox的change事件
2.通过键盘输入,可以激change事件
3.通过鼠标下拉选择,不激活Change事件
请问,这是为什么?谢谢!
阅读权限100
在线时间 小时
使用Click事件
阅读权限95
在线时间 小时
没有问题,可以触发Change事件:
Private Sub UserForm_Initialize()
& & ComboBox1.List = Array(1, 2, 3)
Private Sub ComboBox1_Change()
& & If ComboBox1.ListIndex && -1 Then MsgBox ComboBox1.Value
玩命加载中,请稍候
玩命加载中,请稍候
本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!
本站特聘法律顾问:徐怀玉律师 李志群律师
Powered by您还未登陆,请登录后操作!
VB的ComboBox的使用问题
omboBox的Change事件为什么不支持中文输入?比如将ComboBox的rowsource属性改为sheet!A1:A5,A1=abcd,然后在ComboBox中输入a,就会自动跳出abcd,类似于工作表列中的自动记忆键入功能, 中文能否实现以上功能?
友情帮顶,希望楼主再日找到自己想要的答案.
祝你一帆风顺,财源丰盛!
大家还关注您所在的位置: &
讲解VB.NET COMBOBOX控件
讲解VB.NET COMBOBOX控件
这里介绍VB.NET COMBOBOX控件,包括介绍Visual Basic.NET是基于微软。大多的VB.Net程序员使用Visual Studio .Net作为IDE。SharpDevelop是另一种可用的开源的IDE。
学习Visual Basic.NET时,你可能会遇到VB.NET COMBOBOX控件问题,这里将介绍VB.NET COMBOBOX控件问题的解决方法,在这里拿出来和大家分享一下。
Visual Basic.NET是基于微软.NET Framework之上的面向对象的中间解释性语言,可以看作是Visual Basic在.Net Framework平台上的升级版本,增强了对面向对象的支持。但由于改动太大,导致VB.net对VB的向后兼容性不好,在业界引起不小的争议。
大多的VB.NET程序员使用Visual Studio .Net作为IDE(integrated development environment). SharpDevelop是另一种可用的开源的IDE。VB.NET需要在.Net Framework平台上才能执行。
前不久,有位朋友发帖,寻求颜色选择的VB.NET COMBOBOX控件的制作方法,经过试验,整理了出来,仅供参考。Private&Sub&filllistboxwithcolors() &<FONT color=#boBox1.DrawMode&=&DrawMode.OwnerDrawFixed &<FONT color=#boBox1.DropDownStyle&=&ComboBoxStyle.DropDownList &<FONT color=#boBox1.ItemHeight&=&15&'避免闪烁beginupdate &<boBox1.BeginUpdate() &ComboBox1.Items.Clear() &Dim&pi&As&Reflection.PropertyInfo &For&Each&pi&In&GetType(Color).GetProperties(Reflection.BindingFlags.Public&Or&Reflection.BindingFlags.Static) &<boBox1.Items.Add(pi.Name) &Next &ComboBox1.EndUpdate() &End&Sub &&Private&Sub&ComboBox1_DrawItem(ByVal&sender&As&Object,&ByVal&e&As&System.Windows.Forms.DrawItemEventArgs)&Handles&ComboBox1.DrawItem &If&e.Index&&&Then&Exit&Sub &&Dim&rect&As&Rectangle&=&e.Bounds&'每一项的边框 &&'绘制项如果被选中则显示高亮显示背景,否则用白色 &If&e.State&And&DrawItemState.Selected&Then &e.Graphics.FillRectangle(SystemBrushes.Highlight,&rect) &Else &e.Graphics.FillRectangle(SystemBrushes.Window,&rect) &End&If &&Dim&colorname&As&String&=&ComboBox1.Items(e.Index) &Dim&b&As&New&SolidBrush(Color.FromName(colorname)) &&'缩小选定项区域() &rect.Inflate(-16,&-2) &'填充颜色(文字对应的颜色) &e.Graphics.FillRectangle(b,&rect) &'绘制边框() &e.Graphics.DrawRectangle(Pens.Black,&rect) &Dim&b2&As&Brush &'确定显示的文字的颜色() &If&CInt(b.Color.R)&+&CInt(b.Color.G)&+&CInt(b.Color.B)&&128&*&3&Then &b2&=&Brushes.Black &Else &b2&=&Brushes.White &&End&If &e.Graphics.DrawString(colorname,&boBox1.Font,&b2,&rect.X,&rect.Y) &&End&Sub &&Private&Sub&Form1_Load(ByVal&sender&As&System.Object,&ByVal&e&As&System.EventArgs)&Handles&MyBase.Load &filllistboxwithcolors() &End&Sub&
【编辑推荐】
【责任编辑: TEL:(010)】
关于的更多文章
这周的头版头条是“双十一”。凌晨12点守在电脑前抢东西的各位,
本次的专刊为大家提供了Oracle最新推出的Java SE 8详细的开发教程,从解读到探究Java 8最新
这周Windows8.1正式版发布了,不知道各位有没有去更新
十一长假归来上班,好像更累了;早上也越来越堵了。小
数据结构课程,貌似是大学计算机、网络、软件等专业的
SQL Server 2005微软官方权威参考手册。
是Inside Microsoft SQL Server 2005系列书中的第一本,SQL Server类的顶尖之作。
51CTO旗下网站If e.State = DrawItemState.Selected Then
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
这样得到的combox和原来的不一样,原来的下拉框下拉的时候,鼠标放到某一项时则替颜色是白色的,其余的是黑色的而我用以上代码重画combobox时,不管鼠标放没放到显示的都是黑色的。请各位高手指教
我在CSDN上还看到用API来实现的,但是这段代码不知道怎么用。。。
using System.Collections.G
using System.T
using System.Windows.F
using System.Runtime.InteropS
using System.D
using System.T
namespace comboBoxApp
/**//// &summary&
/// 可带ToolTip的组合框控件
/// &/summary&
public class ComboBoxEx : ComboBox
/**//// &summary&
/// 这个子类窗口用来存放下拉列表窗口,通过它来操作下拉列表
/// &/summary&
private SubWindow m_SubW
/**//// &summary&
/// 通常的构造函数
/// &/summary&
public ComboBoxEx()
/**//// &summary&
/// 处理Windows的消息
/// &/summary&
/// &param name="m"&&/param&
protected override void WndProc(ref Message m)
//通过这个消息可以得到下拉列表的窗口名柄
if (m.Msg == 0x210 && (int)m.WParam == 0x3e80001)
//构建子类化窗口
SubWindow sw = new SubWindow();
//把当前ComboBox实例做为属性传入方便处理
sw.Owner = this;
//把得到的列表句柄关联到子类窗口类上。
sw.AssignHandle(m.LParam);
//这里的做用是保证子类窗口和ComboBoxEx生存期同步
this.m_SubWindow =
base.WndProc(ref m);
/**//// &summary&
/// 重写以释放子类
/// &/summary&
/// &param name="disposing"&&/param&
protected override void Dispose(bool disposing)
if (disposing && this.m_SubWindow != null)
this.m_SubWindow.DestroyHandle();
base.Dispose(disposing);
/**//// &summary&
/// 子类化窗口的类
/// &/summary&
internal class SubWindow : NativeWindow
/**//// &summary&
/// 为了得到列表上的鼠标坐标而使用Api函数及其所用到的数据结构
/// &/summary&
[StructLayout(LayoutKind.Sequential)]
public class POINT
public int
public int
public POINT(int x, int y)
/**//// &summary&
/// 映射窗体的坐标
/// &/summary&
/// &param name="hWndFrom"&源窗口句柄&/param&
/// &param name="hWndTo"&要影射到的窗口句柄&/param&
/// &param name="pt"&转换前后的坐标数据&/param&
/// &param name="cPoints"&&/param&
/// &returns&&/returns&
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] POINT pt, int cPoints);
/**//// &summary&
/// 为了得到指定坐标下的项而需要向列表发送消息
/// &/summary&
/// &param name="hWnd"&&/param&
/// &param name="msg"&&/param&
/// &param name="wParam"&&/param&
/// &param name="lParam"&&/param&
/// &returns&&/returns&
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
/**//// &summary&
/// 为了得到指定索引的列表的内容而需要向列表发送消息,因为列表文本可能被格式化,所以这是合理的。
/// &/summary&
/// &param name="hWnd"&&/param&
/// &param name="msg"&&/param&
/// &param name="wParam"&&/param&
/// &param name="lParam"&&/param&
/// &returns&&/returns&
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);
/**//// &summary&
/// 上一索引值
/// &/summary&
private int m_I
/**//// &summary&
/// 用来显示信息ToolTip
/// &/summary&
private ToolTip toolT
/**//// &summary&
/// 所属性的ComboBox
/// &/summary&
private Control m_O
/**//// &summary&
/// 构造函数
/// &/summary&
public SubWindow()
this.m_Index = -1;
this.toolTip = new ToolTip();
/**//// &summary&
/// 所属的控件
/// &/summary&
public Control Owner
get ...{ return m_O }
set ...{ m_Owner = }
/**//// &summary&
/// 处理鼠标的消息以显示ToolTip信息
/// &/summary&
/// &param name="m"&&/param&
protected override void WndProc(ref Message m)
if (m.Msg == 0x200)
//获取鼠标坐标
Point msPoint = Cursor.P
POINT pt = new POINT(msPoint.X, msPoint.Y);
//影射到列表上的坐标
MapWindowPoints(IntPtr.Zero, this.Handle, pt, 1);
//获取鼠标下的项的索引
int index = SendMessage(m.HWnd, 0x1a9, 0, (pt.y && 0x10) | (pt.x & 0xffff));
if (((index && 0x10) & 0xffff) == 0)
index = (index & 0xffff);
if (m_Index != index)
//获取项的字符串的长度
int num = SendMessage(this.Handle, 0x18a, index, 0);
StringBuilder lParam = new StringBuilder(num + 1);
//获取项的字符串内容
SendMessage(this.Handle, 0x189, index, lParam);
//获取鼠标在所属的控件的坐标信息
Point owPoint = this.Owner.PointToClient(msPoint);
//设置ToolTip信息并显示
this.toolTip.RemoveAll();
this.toolTip.Show(lParam.ToString(), this.Owner, owPoint.X + 10, owPoint.Y + 10, 1000);
base.WndProc(ref m);
用了楼主的代码,发现可以实现。有个combobox属性drawmode要变成OwnerDrawFixed
VB.NET code
Public Class Form1
Private ToolTipCmb As ToolTip
Private CboGraphSelection As ComboBox
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.State = DrawItemState.Selected Then
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ToolTipCmb = New ToolTip
CboGraphSelection = ComboBox1
回复楼上,得到的新的下拉框和系统自带的有些不一样,你发现了么?
下拉框颜色还没找准,应该可以了。
VB.NET code
Private ToolTipCmb As ToolTip
Private CboGraphSelection As ComboBox
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.State = DrawItemState.Selected Then
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)
e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ToolTipCmb = New ToolTip
CboGraphSelection = ComboBox1
还是不对... 你仔细看下背景色是对的,不对的是他的字体的颜色正常的下拉框,你下拉之后,鼠标没有滑倒某项时,字体都是黑色的,北京是白色的用这个方法重画的下拉框,下拉之后,鼠标没有滑倒某项时字体是黑色的,滑倒某项时,字体还是黑色的现在CSDN打不开,等会能打开上个图
插入两张图片 看一下效果就知道了
填充字体在填充矩形之后
VB.NET code
Private ToolTipCmb As ToolTip
Private CboGraphSelection As ComboBox
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.State = DrawItemState.Selected Then
e.Graphics.FillRectangle(Brushes.MidnightBlue, e.Bounds)
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ToolTipCmb = New ToolTip
CboGraphSelection = ComboBox1
引用 8 楼 dabuyingyeyaoda 的回复:
填充字体在填充矩形之后
我按照你写的,得到结果是下图,不知道和你的结果一不一样
这时候的鼠标是放在第一行"11111"的那里的,还是不对...
刚才那个图片有点小,看这个

我要回帖

更多关于 vb combobox 的文章

 

随机推荐