[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

comboBox SelectedIndex Changed

Posted on 2008-10-07
8
Medium Priority
?
6,328 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
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.

 
LVL 18

Accepted Solution

by:
Priest04 earned 1000 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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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