[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 11488
  • Last Modified:

combo box default value setting in c# windows form

i had a combobox with 5 items in it the first one is blank
1)
2)a
3)b
4)c
5)d
so my question is  i want to set it to blank if i press spacebar key instead of using up arrow key to set it to balnk (for eg if my combobox is showing "d" i have to press up arrow 4 times to make it balnk instead i want to make it blank by pressing "spacebarkey"

and one more thing the properties of combobox ----dropdownstyle is set to DropDownList
can any one hep me please

0
kranthi4uonly
Asked:
kranthi4uonly
  • 2
  • 2
1 Solution
 
Jaime OlivaresCommented:
you can implement a KeyPress event for that combo and detect if keycode is 32 to select first item.
0
 
kranthi4uonlyAuthor Commented:
i had done that
 private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)(Keys.Space))
            {
             // what to do here
            }
thansk for ur reply
0
 
Jaime OlivaresCommented:
also ensure your form has the KeyPreview property set to true
0
 
kranthi4uonlyAuthor Commented:
i had done that
 private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)(Keys.Space))
            {
          comboBox1.Text = "";
            }

0
 
p_davisCommented:
comboBox1.SelectedIndex = 0; if the blank is the first item in the list.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now