Solved

comboBox SelectedIndex Changed

Posted on 2008-10-07
8
6,291 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
ID: 22661620
>> 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
ID: 22661651
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
ID: 22663465
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 18

Accepted Solution

by:
Priest04 earned 250 total points
ID: 22663729
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
 
LVL 18

Expert Comment

by:Priest04
ID: 22663738
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
ID: 22664752
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
ID: 22664798
I am glad you ound a solution.
0
 

Author Comment

by:dotnet0824
ID: 22669993
Thanks a lot both of you .
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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