Solved

comboBox SelectedIndex Changed

Posted on 2008-10-07
8
6,294 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
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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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