WinForms manually calling SelectedIndexChanged on combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GiJeet

    WinForms manually calling SelectedIndexChanged on combobox

    Hello, I'd like to force a call to the SelectedIndexCh anged of a combo
    box from within a method. I tried to just call it like so:
    this.myComboBox .SelectedIndexC hanged; but of course the event takes 2
    parameters - object sender, EventArgs e and not sure how to pass these
    paras from my method. Should I just make up fake ones to pass?

    Any help would be appreciated.

    G
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: WinForms manually calling SelectedIndexCh anged on combobox

    GiJeet,

    You aren't going to be able to do it. Events can only be called by the
    instance itself, you don't have access to the delegates and won't be given
    access to them to call.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "GiJeet" <gijeet@yahoo.c omwrote in message
    news:d52deae1-f7f5-452a-9d34-6dbec61174d5@p3 5g2000prm.googl egroups.com...
    Hello, I'd like to force a call to the SelectedIndexCh anged of a combo
    box from within a method. I tried to just call it like so:
    this.myComboBox .SelectedIndexC hanged; but of course the event takes 2
    parameters - object sender, EventArgs e and not sure how to pass these
    paras from my method. Should I just make up fake ones to pass?
    >
    Any help would be appreciated.
    >
    G

    Comment

    • Tom Dacon

      #3
      Re: WinForms manually calling SelectedIndexCh anged on combobox


      GiJeet, at the time you're in the method, the SelectedIndexCh anged event
      handler has already been called when it was set to whatever value it holds
      at the time.

      You CAN actually call it yourself like this:

      this.comboBox1_ SelectedIndexCh anged(this.comb oBox1, new EventArgs());

      but remember that it's already been called once for the current value.

      The fact that you are wanting to do this makes me suspect that you may have
      code in the SelectedIndexCh ange event handler that should possibly be
      refactored out into a separate private method on the form. Having done so,
      you could freely call it from wherever you want.

      Tom Dacon
      Dacon Software Consulting


      "GiJeet" <gijeet@yahoo.c omwrote in message
      news:d52deae1-f7f5-452a-9d34-6dbec61174d5@p3 5g2000prm.googl egroups.com...
      Hello, I'd like to force a call to the SelectedIndexCh anged of a combo
      box from within a method. I tried to just call it like so:
      this.myComboBox .SelectedIndexC hanged; but of course the event takes 2
      parameters - object sender, EventArgs e and not sure how to pass these
      paras from my method. Should I just make up fake ones to pass?
      >
      Any help would be appreciated.
      >
      G

      Comment

      • Matthias Krug

        #4
        Re: WinForms manually calling SelectedIndexCh anged on combobox

        Tom Dacon schrieb:
        The fact that you are wanting to do this makes me suspect that you may have
        code in the SelectedIndexCh ange event handler that should possibly be
        refactored out into a separate private method on the form. Having done so,
        you could freely call it from wherever you want.
        For what it's worth, I sometimes "unsubsribe " from event handlers during
        some actions, if these actions also might replace datasources and such
        stuff.

        After all work is done, I usually re-subscribe to the event, and -
        similar to the OP - call the eventhandler "manually" once.

        Is there a better approach?

        Comment

        • GiJeet

          #5
          Re: WinForms manually calling SelectedIndexCh anged on combobox

          On Nov 10, 11:44 am, "Tom Dacon" <tda...@communi ty.nospamwrote:
          The fact that you are wanting to do this makes me suspect that you may have
          code in the SelectedIndexCh ange event handler that should possibly be
          refactored out into a separate private method on the form. Having done so,
          you could freely call it from wherever you want.
          Tom, you are correct, the code in the SelectedIndex event should be
          moved out. But I didn't write the code...just fixing some bug and not
          sure I want to go thru the exercise of factoring out all the places
          that need it. But that is the best move.

          I decided to just pass null,null and see what happends
          eg: this.MyComboBox _SelectedIndexC hanged(null, null);

          I'll report back....
          G

          Comment

          • GiJeet

            #6
            Re: WinForms manually calling SelectedIndexCh anged on combobox

            >>On Nov 10, 12:41 pm, GiJeet <gij...@yahoo.c omwrote:
            I'll report back....
            G
            Since I'm not using Sender or EventArgs calling
            this.MyComboBox _SelectedIndexC hanged(null, null); with the nulls works
            ok.
            you jump right into the SelectedIndexCh anged event.

            G

            Comment

            Working...