Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How To Disable Some Items Of A Combobox
WhatsApp
Apurba Ranjan
Jul 22
2016
18.2
k
0
1
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
; }
}
Disable Items Of A Combobox
C#
Up Next
How To Disable Some Items Of A Combobox