• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1833
  • Last Modified:

C# How do I programmatically raised listBox1_SelectedIndexChanged event?

My app fills a listbox and I want to execute this event handler listBox1_SelectedIndexChanged with the first item as selected item.

How do I raise/trigger this event in my code?

Thanks.
0
rj94070
Asked:
rj94070
  • 3
  • 2
1 Solution
 
cyberdevil67Commented:
Hi rj94070,

 The quickest and easiest way is in the VS IDE click the listbox, and over in the properties tab you will see what looks like a yellow lightning bolt. This bolt is the events that the object can do, then you can say when this happens fire this event. So in this case it would be something like OnChange event.

Cheers!
0
 
rj94070Author Commented:
Hi cyberdevil67,

What you described is not what I want to do.

I know that when I click on a different item in the listbox, the SelectedIndexChanged event is triggered.

But I want to trigger this event in my code, without the user's clicking on the listbox. In other words, I want to RAISE the event, not to HANDLE it.

Thanks.
0
 
cyberdevil67Commented:
Yes, but the OnChange event does just that will it not. The way I understand this, is that if you set this event up on the listbox, change it via program or not it wil fire this event. But you need to have this event setup as well.

But if you want to manually raise the event, you need to know the handler, and here is more info from the MSDN.

Raises the SelectedIndexChanged event.

[Visual Basic]
Protected Overridable Sub OnSelectedIndexChanged( _
   ByVal e As EventArgs _
)

[C#]
protected virtual void OnSelectedIndexChanged(
   EventArgs e
);

[C++]
protected: virtual void OnSelectedIndexChanged(
   EventArgs* e
);

[JScript]
protected function OnSelectedIndexChanged(
   e : EventArgs
);

Parameters
e
An EventArgs that contains the event data.
Remarks
Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnSelectedIndexChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:  When overriding OnSelectedIndexChanged in a derived class, be sure to call the base class's OnSelectedIndexChanged method so that registered delegates receive the event.

Plus more information on creating the event

Event functionality is provided by three interrelated elements: a class that provides event data, an event delegate, and the class that raises the event. The .NET Framework has a convention for naming classes and methods related to events. If you want your class to raise an event named EventName, you need the following elements.
A class that holds event data, named EventNameEventArgs. This class must derive from System.EventArgs.
A delegate for the event, named EventNameEventHandler.
A class that raises the event. This class must provide:
An event declaration.
[C#]
public event EventNameEventHandler EventName;
[Visual Basic]
Public Event EventName As EventNameEventHandler
A method named OnEventName that raises the event.
The event data class and the event delegate class might already be defined in the .NET Framework class library or in a third-party class library. In that case, you do not have to define those classes.
If you are not familiar with the delegate model for events in the .NET Framework, see Events and Delegates.
To provide event functionality
Define a class that provides data for the event. This class must derive from System.EventArgs, which is the base class for event data. An example follows.
Note   This step is not needed if an event data class already exists for the event or if there is no data associated with your event. If there is no event data, use the base class System.EventArgs.
[C#]
public class AlarmEventArgs : EventArgs {
   private readonly int nrings = 0;
   private readonly bool snoozePressed = false;
   
   //Properties.
   public string AlarmText {  
      ...
   }
   public int NumRings {
      ...
   }
   public bool SnoozePressed{
      ...
   }
   ...
}
[Visual Basic]
Public Class AlarmEventArgs
   Inherits EventArgs
   Private nrings As Integer = 0
   Private _snoozePressed As Boolean = False
   
   'Properties.
   Public ReadOnly Property AlarmText() As String
      ...
   End Property
   
   Public ReadOnly Property NumRings() As Integer
      ...
   End Property
   
   Public ReadOnly Property SnoozePressed() As Boolean
      ...
   End Property
   ...
End Class
Declare a delegate for the event, as in the following example.
Note   You do not have to declare a custom delegate if the event does not generate data. In that case, use the base event handler System.ComponentModel.EventHandler.
[C#]
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
[Visual Basic]
Public Delegate Sub AlarmEventHandler(sender As Object, e As AlarmEventArgs)
Define a public event member in your class using the event keyword whose type is an event delegate, as in the following example.
[C#]
public class AlarmClock
{
 ...
 public event AlarmEventHandler Alarm;  
}
[Visual Basic]
Public Class AlarmClock
   ...
   Public Event Alarm As AlarmEventHandler
End Class
In the AlarmClock class the Alarm event is a delegate of type AlarmEventHandler. When the compiler encounters an event keyword, it creates a private member such as
[C#]
private AlarmEventHandler al = null;
and the two public methods add_Alarm and remove_Alarm. These methods are event hooks that allow delegates to be combined or removed from the event delegate al. The details are hidden from the programmer.
Note   In languages other than C# and Visual Basic .NET, the compiler might not automatically generate the code corresponding to an event member, and you might have to explicitly define the event hooks and the private delegate field.
Provide a protected method in your class that raises the event. This method must be named OnEventName. The OnEventName method raises the event by invoking the delegates. The code example at the end of this topic shows an implementation of OnEventName.
Note   The protected OnEventName method also allows derived classes to override the event without attaching a delegate to it. A derived class must always call the OnEventName method of the base class to ensure that registered delegates receive the event.
[C#]
public class AlarmClock
   {
   ...
   public event AlarmHandler Alarm;
   protected virtual void OnAlarm(AlarmEvent e){...}
   }
[Visual Basic]
Public Class AlarmClock
   ...
   Public Event Alarm As AlarmEventHandler
   Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
      ...
   End Sub
End Class
The following code fragment puts together all of the elements discussed in this section. For a complete sample that implements and uses events, see Event Sample.
[C#]
//Step 1. Class that defines data for the event
//
public class AlarmEventArgs : EventArgs
   {  
      private readonly bool snoozePressed = false;
      private readonly int nrings = 0;
      // Constructor.
      public AlarmEventArgs(bool snoozePressed, int nrings) {...}
      // Properties.
      public int NumRings{ get { return nrings;}}
      public bool SnoozePressed { get { return snoozePressed;}}    
      public string AlarmText { get {...}}

   }
   
//Step 2. Delegate declaration.
//
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);

// Class definition.
//
public class AlarmClock
{
//Step 3. The Alarm event is defined using the event keyword.
//The type of Alarm is AlarmEventHandler.
  public event AlarmEventHandler Alarm;
//
//Step 4. The protected OnAlarm method raises the event by invoking
//the delegates. The sender is always this, the current instance of
//the class.
//  
protected virtual void OnAlarm(AlarmEventArgs e)
   {
    if (Alarm != null)
     {
       //Invokes the delegates.
       Alarm(this, e);
      }
   }
}
[Visual Basic]
'Step 1. Class that defines data for the event
'
Public Class AlarmEventArgs
   Inherits EventArgs
   Private _snoozePressed As Boolean = False
   Private nrings As Integer = 0
   
   ' Constructor.
   Public Sub New(snoozePressed As Boolean, nrings As Integer)
      ...
   End Sub

   ' Properties.
   Public ReadOnly Property NumRings() As Integer
      Get
         Return nrings
      End Get
   End Property
   
   Public ReadOnly Property SnoozePressed() As Boolean
      Get
         Return _snoozePressed
      End Get
   End Property
   
   Public ReadOnly Property AlarmText() As String
      Get
         ...
      End Get
   End Property
End Class

'Step 2. Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, e As AlarmEventArgs)

' Class definition.
'
Public Class AlarmClock
   'Step 3. The Alarm event is defined using the event keyword.
   'The type of Alarm is AlarmEventHandler.
   Public Event Alarm As AlarmEventHandler
   
   '
   'Step 4. The protected OnAlarm method raises the event by invoking
   'the delegates. The sender is always this, the current instance of
   'the class.
   '  
   Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
      'Invokes the delegates.
      RaiseEvent Alarm(Me, e)
   End Sub
End Class
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
TDSnetCommented:
Wow...

Try this:

object x = null;
System.EventArgs y = null;
this.listBox1_SelectedIndexChanged(x, y);

Cheers,
Jack
0
 
TDSnetCommented:
Or if you want to select the first Item in the list do this:

  this.listBox1.SelectedIndex = 0;

That will then fire the Event (provided the selected Index is not already 0)

Cheers,
Jack
0
 
cyberdevil67Commented:
> Or if you want to select the first Item in the list do this:
>
>   this.listBox1.SelectedIndex = 0;
>
> That will then fire the Event (provided the selected Index is not already 0)

Which is why I said what I said in my first post, by changing the SelectedIndex the event will get fired as soon as it is changed, and your not having to worry rasing the event.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now