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
  1. Home
  2. General Programming
  3. C#
  4. ComboBox problem windows application

ComboBox problem windows application

Scheduled Pinned Locked Moved C#
helptutorial
4 Posts 3 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.
  • Y Offline
    Y Offline
    yogsworld
    wrote on last edited by
    #1

    Hi friends i am developing windows application . I have two combobox on form. I added comboBox1_SelectedIndexChanged event for both combobox. In form load i bind both combobox with ArrayList arr and arr2 object ..The problem is that when i give combobox1.DataSource = arr it call combobox1_SelectedIndexChanged event why it call .... i didn't call it...because i have diff functionality for combobox1_SelectedIndexChanged event and it call on form load when i give combobox1.DataSource and my logic is failed how to avoid this ...... Thanks and regards

    D 1 Reply Last reply
    0
    • Y yogsworld

      Hi friends i am developing windows application . I have two combobox on form. I added comboBox1_SelectedIndexChanged event for both combobox. In form load i bind both combobox with ArrayList arr and arr2 object ..The problem is that when i give combobox1.DataSource = arr it call combobox1_SelectedIndexChanged event why it call .... i didn't call it...because i have diff functionality for combobox1_SelectedIndexChanged event and it call on form load when i give combobox1.DataSource and my logic is failed how to avoid this ...... Thanks and regards

      D Offline
      D Offline
      DigitalKing
      wrote on last edited by
      #2

      This happens because when the combobox is populated, the first item is automatically selected, and therefore, the SelectedIndexChanged event is called. Use a boolean flag to prevent this from happening:

      bool AllowSelectedIndexChange = true;

      void Form1_Load()
      {
      ...
      AllowSelectedIndexChange=false;
      combobox1.DataSource = arr;
      AllowSelectedIndexChange=true;
      ...
      }
      void ComboBox1_SelectedIndexChanged(...)
      {
      if (AllowSelectedIndexChange)
      {
      //current SelectedIndexChanged code goes here
      }
      }

      Hope this helps, DigitalKing

      Y M 2 Replies Last reply
      0
      • D DigitalKing

        This happens because when the combobox is populated, the first item is automatically selected, and therefore, the SelectedIndexChanged event is called. Use a boolean flag to prevent this from happening:

        bool AllowSelectedIndexChange = true;

        void Form1_Load()
        {
        ...
        AllowSelectedIndexChange=false;
        combobox1.DataSource = arr;
        AllowSelectedIndexChange=true;
        ...
        }
        void ComboBox1_SelectedIndexChanged(...)
        {
        if (AllowSelectedIndexChange)
        {
        //current SelectedIndexChanged code goes here
        }
        }

        Hope this helps, DigitalKing

        Y Offline
        Y Offline
        yogsworld
        wrote on last edited by
        #3

        thanks it working now

        1 Reply Last reply
        0
        • D DigitalKing

          This happens because when the combobox is populated, the first item is automatically selected, and therefore, the SelectedIndexChanged event is called. Use a boolean flag to prevent this from happening:

          bool AllowSelectedIndexChange = true;

          void Form1_Load()
          {
          ...
          AllowSelectedIndexChange=false;
          combobox1.DataSource = arr;
          AllowSelectedIndexChange=true;
          ...
          }
          void ComboBox1_SelectedIndexChanged(...)
          {
          if (AllowSelectedIndexChange)
          {
          //current SelectedIndexChanged code goes here
          }
          }

          Hope this helps, DigitalKing

          M Offline
          M Offline
          microsoc
          wrote on last edited by
          #4

          hi DigitalKing & yogsworld! :) you can also try these options: option 1:

          void Form1_Load()
          {
          ...
          combobox1.SelectedIndexChanged -= new EventHandler(SelectedIndexChanged_Handler);
          combobox1.DataSource = arr;
          combobox1.SelectedIndexChanged += new EventHandler(SelectedIndexChanged_Handler);
          ...
          }

          option 2: remove the attaching of the SelectedIndexChanged event handler in the InitializeComponent, then just do it in the Form1_Load after the setting of DataSource property of the combobox. just alternatives instead of using a flag. :) microsoc :cool:

          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