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
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How To Disable Some Items Of A Combobox
Apurba Ranjan
Jul 22
2016
Code
16.2
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
C# Code
private
void
Form1_Load(
object
sender, EventArgs e)
{
UnselectItem();
}
public
void
UnselectItem()
{
this
.comboBox1.ValueMember =
"Value"
;
this
.comboBox1.DisplayMember =
"Text"
;
this
.comboBox1.Items.AddRange(
new
[] {
new
ComboBoxItem() { Selectable =
false
, Text=
"Unselectable"
, Value=0},
new
ComboBoxItem() { Selectable =
true
, Text=
"Selectable1"
, Value=1},
new
ComboBoxItem() { Selectable =
true
, Text=
"Selectable2"
, Value=2},
new
ComboBoxItem() { Selectable =
false
, Text=
"Unselectable"
, Value=3},
new
ComboBoxItem() { Selectable =
true
, Text=
"Selectable3"
, Value=4},
new
ComboBoxItem() { Selectable =
true
, Text=
"Selectable4"
, Value=5},
});
this
.comboBox1.SelectedIndexChanged += (cbSender, cbe) =>
{
var cb = cbSender
as
ComboBox;
if
(cb.SelectedItem !=
null
&& cb.SelectedItem
is
ComboBoxItem && ((ComboBoxItem)cb.SelectedItem).Selectable ==
false
)
{
// deselect item
cb.SelectedIndex = -1;
MessageBox.Show(
"You cannot select this Item"
);
}
};
}
//User Defined class for Combobox Item with the
Selectable
property
private
class
ComboBoxItem
{
public
int
Value {
get
;
set
; }
public
string
Text {
get
;
set
; }
public
bool
Selectable {
get
;
set
; }
}
C#
Disable Items Of A Combobox