Created On:  12 September 2012

Problem:

Customer has a Visual COBOL Windows Forms application on which several comboBox controls reside. When the user selects a new item in the combobox the application needs to set some textbox controls to the value of the selected item.

The problem is that the SelectedIndexChanged event also fires when the combobox is first created during the form load.

How can SelectedIndexChanged event be used only when a user actually clicks on a new item and not when the form is being loaded?

Resolution:

You can set up a condition flag in working-storage so that you can avoid the first time the SelectedindexChanged event is fired.

Example:

      class-id testcomboselect.Form1 is partial
                inherits type System.Windows.Forms.Form.
      working-storage section.
      01 isInitial   condition-value value true.
      method-id NEW.
      procedure division.
          invoke self::InitializeComponent
          goback.
      end method.
      method-id Form1_Load final private.
      procedure division using by value sender as object e as type System.EventArgs.
          invoke comboBox1::Items::Add("Red")
          invoke comboBox1::Items::Add("Blue")
          invoke comboBox1::Items::Add("Green")
          invoke comboBox1::Items::Add("White")
          set comboBox1::SelectedIndex to 2
      end method.
      method-id comboBox1_SelectedIndexChanged final private.
      procedure division using by value sender as object e as type System.EventArgs.
          if isInitial
             set isInitial to false
          else
             set textBox1::Text to comboBox1::SelectedItem
          end-if
      end method.
      end class.