Solved

comboBox SelectedIndex Changed

Posted on 2008-10-07
8
6,282 Views
Last Modified: 2013-12-17
Hi,
I am working on Winform comboBox
I want the control to enter selectedIndexChanged_Event only if an item is selected from comboBox.
ComboBox is bound to a dataset

Even though i put these statements when the form loads it gets inside the event. ... ComboBox index starts with 0(zero) when I select
first item in combo the selected index is Zero

if (combobox1.SelectedIndex > -1 &&  combobox1.SelectedValue.ToString() != string.Empty)

How can I control this.
currently I have combo box bound this way

    this.cboCategory.DataSource = _ds.Tables[0];
    this.cboCategory.DisplayMember = "Student_Name";
                    this.Combobox1.ValueMember = "student_ID";
                    this.Combobox1.SelectedIndex = -1;
                    this.Combobox1.SelectedIndexChanged += new System.EventHandler(this.Combobox1_SelectedIndexChanged);
             
0
Comment
Question by:dotnet0824
  • 4
  • 2
  • 2
8 Comments
 
LVL 18

Expert Comment

by:Priest04
Comment Utility
>> I want the control to enter selectedIndexChanged_Event only if an item is selected from comboBox.

this is not possible. SelectedIndexChanged event is fired on any ocassion where currently selected item is changed.

1) User opens a dropdown list and selects an item with mouse
2) User clicks on a text portion of a combobox, and, for example, press down arrow
3) comboBox1.SelectedIndex = 3
4) can be triggered by binding mechanism

etc

So, in order to be SURE that user selected item through UI, and not through code, you would need to have a private bool variable (ex userActivity)that will be set to false, whever you run some code that will change SelectedIndex property, and after you have done with the code, set userActivity to true. Then in SelectedIndexChanged just check the current value for userActivity, if true, its changed from UI by user.

Currently there is no built in way to achieve what you want.

Goran
0
 
LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
selectedIndexChanged_Event event will be fired when ever you change something in the combobox
can we try to understand what you wnat to achineve so that we can help in a better way
0
 

Author Comment

by:dotnet0824
Comment Utility
Ok.. After the form loads and when an item is selected from comboBox I want to get the studentID and get the relevant record. The problem now is that when ever form loads it gets into selected change event and Gives error as studentID is not selected yet
 private void cboStudent_SelectedIndexChanged(object sender, EventArgs e)
  if (cboStudent.SelectedIndex > -1 &&  cboStudent.SelectedValue.ToString() != string.Empty)
{
    _DV.RowFilter = "Student_ID ="  + int.parse(cboStudent.selectedvalue.tostring())
     cboSchool.Datasource = DV;
     cboSchool.Displaymember = "School_Name";
     cboSchool.valuemember = "school_ID"   (Everything is fine except I am not sure how to control so that it gets into this ONLY when a value is selected from COMBOBOX
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 250 total points
Comment Utility
If it is the form load that is bothering you (binding mechanism), then have a form variable, as I have suggested above

bool formLoading;

void Form_Load(.....)
{
   formLoading = true;
    // have your code for binding here
    // after you are done, set it to false
    formLoading = false;
}


void combo_SelectedIndexChanged(......)
{
    if (formLoading) return;
    // rest of the code
}
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:Priest04
Comment Utility
maybe you could also check if SelectedValue is null, not sure, havent got the IDE with me to test. But the above method works for sure.
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 250 total points
Comment Utility
i tried ur code logic and found selected change event gets fired twice even of page load and it always gives something in the selected value in that event
i tried SelectionChangeCommitted event and it did not fire on the page load when the combobox was bound
and when i changed something in the combobox it worked perfectly as i required

here is my event code

private void comboBox1_SelectionChangeCommitted (object sender, EventArgs e)
{
      if (comboBox1.SelectedIndex > -1 && comboBox1.SelectedValue.ToString () != string.Empty)
      {
            MessageBox.Show (comboBox1.SelectedValue.ToString ());
      }
}
0
 
LVL 18

Expert Comment

by:Priest04
Comment Utility
I am glad you ound a solution.
0
 

Author Comment

by:dotnet0824
Comment Utility
Thanks a lot both of you .
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now