DomainUpDown.OnSelectedItemChanged(Object, EventArgs) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Memunculkan kejadian SelectedItemChanged.
protected:
void OnSelectedItemChanged(System::Object ^ source, EventArgs ^ e);
protected void OnSelectedItemChanged (object source, EventArgs e);
protected void OnSelectedItemChanged (object? source, EventArgs e);
member this.OnSelectedItemChanged : obj * EventArgs -> unit
Protected Sub OnSelectedItemChanged (source As Object, e As EventArgs)
Parameter
- source
- Object
Sumber dari peristiwa tersebut.
Contoh
Contoh kode berikut membuat dan menginisialisasi DomainUpDown kontrol. Contoh ini memungkinkan Anda mengatur beberapa propertinya dan membuat koleksi string untuk ditampilkan di kotak putar (juga dikenal sebagai kontrol naik turun). Kode mengasumsikan bahwa TextBox, , CheckBoxdan Button telah diinstansiasi pada formulir. Contoh ini juga mengasumsikan bahwa Anda memiliki variabel anggota di tingkat kelas yang dinyatakan sebagai bilangan bulat bertanda tangan 32-bit bernama myCounter
. Anda dapat memasukkan string di kotak teks dan menambahkannya ke Items koleksi saat tombol diklik. Dengan mengklik kotak centang, Anda dapat beralih ke Sorted properti dan mengamati perbedaan dalam koleksi item dalam kotak putar.
protected:
DomainUpDown^ domainUpDown1;
private:
void MySub()
{
// Create and initialize the DomainUpDown control.
domainUpDown1 = gcnew System::Windows::Forms::DomainUpDown;
// Add the DomainUpDown control to the form.
Controls->Add( domainUpDown1 );
}
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( String::Concat(
(textBox1->Text->Trim()), " - ", myCounter.ToString() ) );
// Increment the counter variable.
myCounter = myCounter + 1;
// Clear the TextBox.
textBox1->Text = "";
}
void checkBox1_Click( Object^ sender, EventArgs^ e )
{
// If Sorted is set to true, set it to false;
// otherwise set it to true.
if ( domainUpDown1->Sorted )
{
domainUpDown1->Sorted = false;
}
else
{
domainUpDown1->Sorted = true;
}
}
void domainUpDown1_SelectedItemChanged( Object^ sender, EventArgs^ e )
{
// Display the SelectedIndex and SelectedItem property values in a MessageBox.
MessageBox::Show( String::Concat( "SelectedIndex: ",
domainUpDown1->SelectedIndex.ToString(), "\n", "SelectedItem: ",
domainUpDown1->SelectedItem->ToString() ) );
}
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.
Controls.Add(domainUpDown1);
}
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;
}
else
{
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());
}
Protected domainUpDown1 As DomainUpDown
Private Sub MySub()
' Create and initialize the DomainUpDown control.
domainUpDown1 = New System.Windows.Forms.DomainUpDown()
' Add the DomainUpDown control to the form.
Controls.Add(domainUpDown1)
End Sub
Private Sub button1_Click(sender As System.Object, e As System.EventArgs)
' 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 = ""
End Sub
Private Sub checkBox1_Click(sender As System.Object, e As System.EventArgs)
' If Sorted is set to true, set it to false;
' otherwise set it to true.
If domainUpDown1.Sorted Then
domainUpDown1.Sorted = False
Else
domainUpDown1.Sorted = True
End If
End Sub
Private Sub domainUpDown1_SelectedItemChanged _
(sender As System.Object, e As System.EventArgs)
' Display the SelectedIndex and SelectedItem property values in a MessageBox.
MessageBox.Show(("SelectedIndex: " & domainUpDown1.SelectedIndex.ToString() & _
ControlChars.Cr & "SelectedItem: " & domainUpDown1.SelectedItem.ToString()))
End Sub
Keterangan
Menaikkan peristiwa memanggil penanganan aktivitas melalui delegasi. Untuk informasi selengkapnya, lihat Menangani dan Meningkatkan Peristiwa.
Metode ini OnSelectedItemChanged juga memungkinkan kelas turunan untuk menangani peristiwa tanpa melampirkan delegasi. Ini adalah teknik yang disukai untuk menangani peristiwa di kelas turunan.
Catatan Bagi Inheritor
Saat mengambil alih di kelas turunan OnSelectedItemChanged(Object, EventArgs) , pastikan untuk memanggil metode kelas OnSelectedItemChanged(Object, EventArgs) dasar sehingga delegasi terdaftar menerima peristiwa tersebut.