TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Vali Maties
NA
66
2.7k
WinForms Combobox Drop Down on Enter and on Click
Mar 6 2020 5:00 AM
Hi.
I have a custom class based on ComboBox, in Winforms. It is set as DropDownList, as I only want to select an item, not to insert or modify the Text area of it.
I want to make it to drop its list when it has focus, from keyboard, by iterate thru form's items using TAB key, on leave to close its dropped list. All works fine by having this code in Enter and Leave methods (Console lines are only for debugging):
The problem is when I click with mouse on it. The list will drop down, it will close and open again.
The order of events fired on click, if this control does not have focus, is Enter and then MouseClick, Leave event does not fire, so I don't know why DropDownList is closing!
How to make it drop only one time if the focus is not on it and I click with mouse?
PS: I cannot use ListBox, because of space on the form. I post an image with my checkbox...
private
void
MyNewComboBoxCheckable_SelectionChangeCommitted(
object
sender, EventArgs e)
{
SuspendLayout();
if
(readOnly)
{
SelectedIndex = prevIndex;
Console.WriteLine(
"SelectionChangeCommited"
);
}
ResumeLayout(
false
);
}
private
void
MyNewComboBoxCheckable_Enter(
object
sender, EventArgs e)
{
prevIndex = SelectedIndex;
if
(ReadOnly ==
false
)
{
BorderColor = Color.Red;
if
(!DroppedDown && Droppable)
{
DroppedDown =
true
;
Console.WriteLine(
"Enter - Dropped = set to true, {0}"
, DroppedDown);
}
}
Invalidate();
}
private
void
MyNewComboBoxCheckable_Leave(
object
sender, EventArgs e)
{
DroppedDown =
false
;
BorderColor = Color.DarkGray;
Invalidate();
Console.WriteLine(
"Leave - Dropped = false"
);
}
private
void
MyNewComboBoxCheckable_MouseClick(
object
sender, MouseEventArgs e)
{
DroppedDown =
true
;
Console.WriteLine(
"MouseClick"
);
}
protected
override
void
WndProc(
ref
Message m)
{
if
(ReadOnly)
{
switch
(m.Msg)
{
case
0x201:
case
0x203:
break
;
default
:
base
.WndProc(
ref
m);
break
;
}
}
else
{
base
.WndProc(
ref
m);
}
if
(m.Msg == WM_PAINT)
{
using
(var g = Graphics.FromHwnd(Handle))
{
// Uncomment this if you don't want the "highlight border".
using
(var p =
new
Pen(
this
.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}
}
}
}
Reply
Answers (
1
)
How to zip multiple folder at a time.
print array stars