Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. listbox selectedvaluechanged event fires programmaticlly !!! [modified]

listbox selectedvaluechanged event fires programmaticlly !!! [modified]

Scheduled Pinned Locked Moved C#
databasetutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hussam Fattahi
    wrote on last edited by
    #1

    I'm populating a listbox using it's DataSource property in form's load event what happens is that the selectedValueChanged event or selectedIndexChanged fires twice when starting the form how can i prevent such behaviour i want the event to fire only when user change listbox by mouse there is an event straight for this(SelectionChangeCommitted), but it's availabe only on combbox control i need to use listbox for htis. how can i achive such behaviour secondly: when i first run the form, the selectedValueChanged event always fired once again selecting the firs index in the listbox how to stop this also i want to run the form without any event being fired unless the user change the value manually thanks

    modified on Tuesday, October 27, 2009 7:04 AM

    H 1 Reply Last reply
    0
    • H Hussam Fattahi

      I'm populating a listbox using it's DataSource property in form's load event what happens is that the selectedValueChanged event or selectedIndexChanged fires twice when starting the form how can i prevent such behaviour i want the event to fire only when user change listbox by mouse there is an event straight for this(SelectionChangeCommitted), but it's availabe only on combbox control i need to use listbox for htis. how can i achive such behaviour secondly: when i first run the form, the selectedValueChanged event always fired once again selecting the firs index in the listbox how to stop this also i want to run the form without any event being fired unless the user change the value manually thanks

      modified on Tuesday, October 27, 2009 7:04 AM

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      Your ListBox is behaving as designed and as far as I am aware it is not possible to stop it. However one of the many ways to cope with the problem is as follows: 1) Modify the Constructor for your Form

      private bool stillInitializing = false;    //<========== NEW CODE ===================
      public MyForm()
      {
          this.stillInitializing = true;    //<========== NEW CODE ===================
      
          InitializeComponent();
      
          this.stillInitializing = false;    //<========== NEW CODE ===================
      }
      
      1. Modify your event handler(s)

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        if (this.stillInitializing)
        {
        return;
        }

         // your code goes here
        

        }

      Hope this helps! :)

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      H 1 Reply Last reply
      0
      • H Henry Minute

        Your ListBox is behaving as designed and as far as I am aware it is not possible to stop it. However one of the many ways to cope with the problem is as follows: 1) Modify the Constructor for your Form

        private bool stillInitializing = false;    //<========== NEW CODE ===================
        public MyForm()
        {
            this.stillInitializing = true;    //<========== NEW CODE ===================
        
            InitializeComponent();
        
            this.stillInitializing = false;    //<========== NEW CODE ===================
        }
        
        1. Modify your event handler(s)

          private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
          {
          if (this.stillInitializing)
          {
          return;
          }

           // your code goes here
          

          }

        Hope this helps! :)

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        H Offline
        H Offline
        Hussam Fattahi
        wrote on last edited by
        #3

        didn't work when applied on form construcor so i tried this

        private void Form1_Load(object sender, EventArgs e)
        {
        this.stillInitializing = true; //NEW CODE

                unitMeasureCollection.GetMulti(null);
                vendorCollection.GetMulti(null);
                shipMethodCollection.GetMulti(null);
                purchaseOrderDetailCollection.GetMulti(null);
        
                /\*PredicateExpression filter = new PredicateExpression(UnitMeasureFields.UnitMeasureCode == "BOX");
                unitMeasureCollection1.GetMulti(filter);
                \*/
        
                comboBoxUnitMeasure.DataSource = unitMeasureCollection;
                comboBoxUnitMeasure.ValueMember = "UnitMeasureCode";
        
                listBoxVendor.DataSource = vendorCollection;
                listBoxVendor.DisplayMember = "Name";
                listBoxVendor.ValueMember = "VendorID";
        
                listBoxShipMethod.DataSource = shipMethodCollection;
                listBoxShipMethod.DisplayMember = "Name";
                listBoxShipMethod.ValueMember = "ShipMethodID";
        
                dataGridView1.DataSource = purchaseOrderDetailCollection;
        
                PopulateCategoriesAndSubCategories();
        
                //To clear textboxes affected by lisbox event that fired automatically
                ClearTextBoxes();
                DisableButtons();
        
                this.stillInitializing = false;                                       //NEW CODE
            }
        

        and it worked just fine thanks, Henry Minute :)

        H 1 Reply Last reply
        0
        • H Hussam Fattahi

          didn't work when applied on form construcor so i tried this

          private void Form1_Load(object sender, EventArgs e)
          {
          this.stillInitializing = true; //NEW CODE

                  unitMeasureCollection.GetMulti(null);
                  vendorCollection.GetMulti(null);
                  shipMethodCollection.GetMulti(null);
                  purchaseOrderDetailCollection.GetMulti(null);
          
                  /\*PredicateExpression filter = new PredicateExpression(UnitMeasureFields.UnitMeasureCode == "BOX");
                  unitMeasureCollection1.GetMulti(filter);
                  \*/
          
                  comboBoxUnitMeasure.DataSource = unitMeasureCollection;
                  comboBoxUnitMeasure.ValueMember = "UnitMeasureCode";
          
                  listBoxVendor.DataSource = vendorCollection;
                  listBoxVendor.DisplayMember = "Name";
                  listBoxVendor.ValueMember = "VendorID";
          
                  listBoxShipMethod.DataSource = shipMethodCollection;
                  listBoxShipMethod.DisplayMember = "Name";
                  listBoxShipMethod.ValueMember = "ShipMethodID";
          
                  dataGridView1.DataSource = purchaseOrderDetailCollection;
          
                  PopulateCategoriesAndSubCategories();
          
                  //To clear textboxes affected by lisbox event that fired automatically
                  ClearTextBoxes();
                  DisableButtons();
          
                  this.stillInitializing = false;                                       //NEW CODE
              }
          

          and it worked just fine thanks, Henry Minute :)

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Good stuff! :thumbsup: It is nice to attempt to help someone with the intelligence to see the basis of your answer and apply it for their own circumstances. :)

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups