DomainUpDown.OnSelectedItemChanged Method (Object, EventArgs)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Raises the SelectedItemChanged event.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

protected void OnSelectedItemChanged(
	object source,
	EventArgs e


Type: System.Object

The source of the event.

Type: System.EventArgs

An EventArgs that contains the event data.

Raising an event invokes the event handler through a delegate. For more information, see Handling and Raising Events.

The OnSelectedItemChanged 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 OnSelectedItemChanged in a derived class, be sure to call the base class’s OnSelectedItemChanged method so that registered delegates receive the event.

The following code example creates and initializes a DomainUpDown control. The example allows you to set some of its properties and create a collection of strings for display in the spin box (also known as an up-down control). The code assumes that a TextBox, CheckBox, and Button have been instantiated on a form. The example also assumes that you have a member variable at the class level declared as a 32-bit signed integer named myCounter. You can enter a string in the text box and add it to the Items collection when the button is clicked. By clicking the check box, you can toggle the Sorted property and observe the difference in the collection of items in the spin box.

protected DomainUpDown domainUpDown1;

private void MySub()
    // Create and initialize the DomainUpDown control.
    domainUpDown1 = new System.Windows.Forms.DomainUpDown();

    // Add the DomainUpDown control to the form.

 private void button1_Click(System.Object sender, 
                           System.EventArgs e)
    // Add the text box contents and initial location in the collection
    // to the DomainUpDown control.
    domainUpDown1.Items.Add((textBox1.Text.Trim()) + " - " + myCounter);

    // Increment the counter variable.
    myCounter = myCounter + 1;

    // Clear the TextBox.
    textBox1.Text = "";

 private void checkBox1_Click(System.Object sender, 
                             System.EventArgs e)
    // If Sorted is set to true, set it to false; 
    // otherwise set it to true.
    if (domainUpDown1.Sorted)
       domainUpDown1.Sorted = false;
       domainUpDown1.Sorted = true;

 private void domainUpDown1_SelectedItemChanged(System.Object sender, 
                                               System.EventArgs e)
    // Display the SelectedIndex and SelectedItem property values in a MessageBox.
    MessageBox.Show("SelectedIndex: " + domainUpDown1.SelectedIndex.ToString() 
       + "\n" + "SelectedItem: " + domainUpDown1.SelectedItem.ToString());

.NET Framework
Available since 1.1
Return to top