Hi Frank,
Based on my understanding, you'd like to prevent the user from selecting
certain items in a ComboBox control. If I'm off base, please feel free to
let me know.
I think your basic thinking of solving this problem is right. We could
implement IMessageFilter interface and add the implementation class to the
application's message pump to filter out a message.
Quote:
The dropped down portion of the combobox has a different Hwnd.
Yes, you're right. The dropped down portion of a ComboBox control is
separate window, which has a different handle from the ComboBox control.
Quote:
The documentation (
http://msdn.microsoft.com/library/de...l=/library/en-
us/shellcc/platform/commctls/comboboxes/comboboxreference/comboboxmessages/c
bn_dropdown.asp ) states that a message CBN_DROPDOWN is sent down with the
WM_COMMAND.
Quote:
>For whatever reason, this data does not come down via the
IMessageFilter.PreFilterMessage method. It does come down via the
overridden WndProc method.
Since the drop down portion of a ComboBox control is a separate window, the
CBN_DROPDOWN notification won't go through IMessageFilter.PreFilterMessage
method. Actually, .NET class ComboBox has exposed the DropDown event which
is a wrapper of the CBN_DROPDOWN notification.
What's more, I don't think we need to catch the CBN_DROPDOWN notification
in this case.
I have tried to implement the function you want. The following is the
sample code. The sample prevents the user from selecting the item whose
index is 1 by clicking it or pressing up/down key.
public partial class Form1 : Form
{
IMessageFilter myfilter = new MessageFilter();
private int previousSelectIndex = -1;
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(myfilter);
this.Load +=new EventHandler(Form1_Load);
this.comboBox1.DropDownClosed += new
EventHandler(comboBox1_DropDownClosed);
this.comboBox1.SelectedIndexChanged += new
EventHandler(comboBox1_SelectedIndexChanged);
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.SelectedIndex != 1)
{
this.previousSelectIndex = this.comboBox1.SelectedIndex;
}
else
{
this.comboBox1.SelectedIndex = this.previousSelectIndex;
}
}
void comboBox1_DropDownClosed(object sender, EventArgs e)
{
if (this.comboBox1.SelectedIndex == 1)
{
this.comboBox1.SelectedIndex = this.previousSelectIndex ;
}
}
private void Form1_Load(object sender, EventArgs e)
{
(myfilter as MessageFilter).RelatdCombobox = this.comboBox1;
}
}
class MessageFilter : IMessageFilter
{
private ComboBox m_combobox;
public ComboBox RelatdCombobox
{
get { return m_combobox; }
set { m_combobox = value; }
}
#region IMessageFilter Members
private int WM_LBUTTONDOWN = 0x0201;
private int WM_LBUTTONDBLCLK = 0x0203;
bool IMessageFilter.PreFilterMessage(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN || m.Msg == WM_LBUTTONDBLCLK)
{
if (m_combobox.SelectedIndex == 1 && m_combobox.DroppedDown)
{
return true;
}
else
{
return false;
}
}
return false;
}
#endregion
}
Hope this helps.
If my sample code is not what you want, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.