Diese Dokumentation wurde archiviert und wird nicht länger gepflegt.

ComboBox.SelectedIndexChanged-Ereignis

Tritt ein, wenn die SelectedIndex-Eigenschaft geändert wurde.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public event EventHandler SelectedIndexChanged
/** @event */
public void add_SelectedIndexChanged (EventHandler value)

/** @event */
public void remove_SelectedIndexChanged (EventHandler value)

JScript unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.

Für dieses Ereignis können Sie einen Ereignishandler erstellen, um zu bestimmen, wann sich der in ComboBox ausgewählte Index geändert hat. Dies eignet sich für die Anzeige von Informationen in anderen Steuerelementen, die von der aktuellen Auswahl in der ComboBox abhängen. Mit dem Ereignishandler für dieses Ereignis können Sie die Informationen in die anderen Steuerelemente laden.

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter Behandeln von Ereignissen.

Im folgenden Codebeispiel wird das Initialisieren eines ComboBox-Steuerelements veranschaulicht. Dazu werden die MaxDropDownItems-Eigenschaft und die DropDownStyle-Eigenschaft festgelegt, und mithilfe der FindStringExact-Methoden wird die ComboBox durchsucht. Außerdem wird die Behandlung des SelectedIndexChanged-Ereignisses dargestellt. Zum Ausführen des Beispiels fügen Sie folgenden Code in ein Formular ein, das eine TextBox mit dem Namen TextBox1 enthält, und rufen Sie die InitializeComboBox-Methode im Konstruktor oder im Load-Ereignis des Formulars auf.

	// Declare comboBox1 as a ComboBox.
	internal System.Windows.Forms.ComboBox ComboBox1;
    
	// This method initializes the combo box, adding a large string array
	// but limiting the drop-down size to six rows so the combo box doesn't 
	// cover other controls when it expands.
	private void InitializeComboBox()
	{
		this.ComboBox1 = new System.Windows.Forms.ComboBox();
		string[] employees = new string[]{"Hamilton, David", "Hensien, Kari",
				"Hammond, Maria", "Harris, Keith", "Henshaw, Jeff D.", 
				"Hanson, Mark", "Harnpadoungsataya, Sariya", 
				"Harrington, Mark", "Harris, Keith", "Hartwig, Doris", 
				"Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", 
				"Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", 
				"Henshaw, Jeff D.", "Hensien, Kari", "Harris, Keith", 
				"Henshaw, Jeff D.", "Hensien, Kari", "Hasselberg, Jonas",
				"Harrington, Mark", "Hedlund, Magnus", "Hay, Jeff", 
				"Heidepriem, Brandon D."};

		ComboBox1.Items.AddRange(employees);
		this.ComboBox1.Location = new System.Drawing.Point(136, 32);
		this.ComboBox1.MaxDropDownItems = 5;
		this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
		this.ComboBox1.Name = "ComboBox1";
		this.ComboBox1.Size = new System.Drawing.Size(136, 81);
		this.ComboBox1.TabIndex = 0;
		this.Controls.Add(this.ComboBox1);
		
		// Associate the event-handling method with the 
		// SelectedIndexChanged event.
		this.ComboBox1.SelectedIndexChanged += 
			new System.EventHandler(ComboBox1_SelectedIndexChanged);
	}
...	// This method is called when the user changes his or her selection.
	// It searches for all occurrences of the selected employee's
	// name in the Items array and adds the employee's name and 
	// the number of occurrences to TextBox1.Text.

	// CAUTION   This code exposes a known bug: If the index passed to the 
	// FindStringExact(searchString, index) method is the last index 
	// of the array, the code throws an exception.
	private void ComboBox1_SelectedIndexChanged(object sender, 
		System.EventArgs e)
	{

		ComboBox comboBox = (ComboBox) sender;

		// Save the selected employee's name, because we will remove
		// the employee's name from the list.
		string selectedEmployee = (string) ComboBox1.SelectedItem;

		int count = 0;
		int resultIndex = -1;

		// Call the FindStringExact method to find the first 
		// occurrence in the list.
		resultIndex = ComboBox1.FindStringExact(selectedEmployee);

		// Remove the name as it is found, and increment the found count. 
		// Then call the FindStringExact method again, passing in the 
		// index of the current found item so the search starts there 
		// instead of at the beginning of the list.
		while (resultIndex!=-1)
		{
			ComboBox1.Items.RemoveAt(resultIndex);
			count += 1;
			resultIndex = ComboBox1.FindStringExact(selectedEmployee, 
				resultIndex);
		}
		// Update the text in Textbox1.
		TextBox1.Text = TextBox1.Text+ "\r\n" + selectedEmployee + ": "
			+ count;
	}

    // Declare comboBox1 as a ComboBox.
    System.Windows.Forms.ComboBox comboBox1;

    // This method initializes the combo box, adding a large string array
    // but limiting the drop-down size to six rows so the combo box doesn't 
    // cover other controls when it expands.
    private void InitializeComboBox()
    {
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        String employees[] = new String[] { "Hamilton, David", "Hensien, Kari", 
            "Hammond, Maria", "Harris, Keith", "Henshaw, Jeff D.", 
            "Hanson, Mark", "Harnpadoungsataya, Sariya", "Harrington, Mark",
            "Harris, Keith", "Hartwig, Doris", "Harui, Roger", "Hassall, Mark",
            "Hasselberg, Jonas", "Harnpadoungsataya, Sariya", 
            "Henshaw, Jeff D.","Henshaw, Jeff D.", "Hensien, Kari", 
            "Harris, Keith", "Henshaw, Jeff D.", "Hensien, Kari", 
            "Hasselberg, Jonas", "Harrington, Mark", "Hedlund, Magnus", 
            "Hay, Jeff", "Heidepriem, Brandon D." };

        comboBox1.get_Items().AddRange(employees);
        this.comboBox1.set_Location(new System.Drawing.Point(136, 32));
        this.comboBox1.set_MaxDropDownItems(5);
        this.comboBox1.set_DropDownStyle(ComboBoxStyle.DropDownList);
        this.comboBox1.set_Name("comboBox1");
        this.comboBox1.set_Size(new System.Drawing.Size(136, 81));
        this.comboBox1.set_TabIndex(0);
        this.get_Controls().Add(this.comboBox1);
        // Associate the event-handling method with the 
        // SelectedIndexChanged event.
        this.comboBox1.add_SelectedIndexChanged(new System.EventHandler(
            comboBox1_SelectedIndexChanged));
    } //InitializeComboBox
...    // This method is called when the user changes his or her selection.
    // It searches for all occurrences of the selected employee's
    // name in the Items array and adds the employee's name and 
    // the number of occurrences to textBox1.Text.
    // CAUTION   This code exposes a known bug: If the index passed to the 
    // FindStringExact(searchString, index) method is the last index 
    // of the array, the code throws an exception.
    private void comboBox1_SelectedIndexChanged(Object sender, 
        System.EventArgs e)
    {
        ComboBox comboBox = (ComboBox)sender;
        // Save the selected employee's name, because we will remove
        // the employee's name from the list.
        String selectedEmployee = (String)(comboBox1.get_SelectedItem());

        int count = 0;
        int resultIndex = -1;
        // Call the FindStringExact method to find the first 
        // occurrence in the list.
        resultIndex = comboBox1.FindStringExact(selectedEmployee);
        // Remove the name as it is found, and increment the found count. 
        // Then call the FindStringExact method again, passing in the 
        // index of the current found item so the search starts there 
        // instead of at the beginning of the list.
        while (resultIndex != -1) {
            comboBox1.get_Items().RemoveAt(resultIndex);
            count += 1;
            resultIndex = comboBox1.FindStringExact(selectedEmployee, 
                resultIndex);
        }
        // Update the text in textBox1.
        textBox1.set_Text(textBox1.get_Text() + "\r\n" + selectedEmployee 
            + ": " + count);
    } //comboBox1_SelectedIndexChanged

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0
Anzeigen: