Solved

comboBox SelectedIndex Changed

Posted on 2008-10-07
8
6,299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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