The following code example lists numbers in a list box on a form. Each time you click button1, the application adds another number to the list.
The Main method calls Run to start the application, which creates the form, listBox1 and button1. When the user clicks button1, the button1_Click method displays a MessageBox. If the user clicks No on the MessageBox, the button1_Click method adds a number to the list. If the user clicks Yes, the application calls Exit to process all remaining messages in the queue and then to quit.
Note: |
|---|
The call to
Exit will fail in partial trust.
|
Public Class Form1
Inherits Form
<STAThread()> _
Shared Sub Main()
' Start the application.
Application.Run(New Form1)
End Sub
Private WithEvents button1 As Button
Private WithEvents listBox1 As ListBox
Public Sub New()
button1 = New Button
button1.Left = 200
button1.Text = "Exit"
listBox1 = New ListBox
Me.Controls.Add(button1)
Me.Controls.Add(listBox1)
End Sub
Private Sub button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles button1.Click
Dim count As Integer = 1
' Check to see whether the user wants to exit the application.
' If not, add a number to the list box.
While (MessageBox.Show("Exit application?", "", _
MessageBoxButtons.YesNo) = DialogResult.No)
listBox1.Items.Add(count)
count += 1
End While
' The user wants to exit the application.
' Close everything down.
Application.Exit()
End Sub
End Class
public class Form1 : Form
{
[STAThread]
public static void Main()
{
// Start the application.
Application.Run(new Form1());
}
private Button button1;
private ListBox listBox1;
public Form1()
{
button1 = new Button();
button1.Left = 200;
button1.Text = "Exit";
button1.Click += new EventHandler(button1_Click);
listBox1 = new ListBox();
this.Controls.Add(button1);
this.Controls.Add(listBox1);
}
private void button1_Click(object sender, System.EventArgs e)
{
int count = 1;
// Check to see whether the user wants to exit the application.
// If not, add a number to the list box.
while (MessageBox.Show("Exit application?", "",
MessageBoxButtons.YesNo)==DialogResult.No)
{
listBox1.Items.Add(count);
count += 1;
}
// The user wants to exit the application.
// Close everything down.
Application.Exit();
}
}
public ref class Form1: public System::Windows::Forms::Form
{
private:
Button^ button1;
ListBox^ listBox1;
public:
Form1()
{
button1 = gcnew Button;
button1->Left = 200;
button1->Text = "Exit";
button1->Click += gcnew EventHandler( this, &Form1::button1_Click );
listBox1 = gcnew ListBox;
this->Controls->Add( button1 );
this->Controls->Add( listBox1 );
}
private:
void Form1::button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
int count = 1;
// Check to see whether the user wants to exit
// the application. If not, add a number to the list box.
while ( MessageBox::Show( "Exit application?", "", MessageBoxButtons::YesNo ) == ::DialogResult::No )
{
listBox1->Items->Add( count );
count += 1;
}
// The user wants to exit the application.
// Close everything down.
Application::Exit();
}
};
int main()
{
// Starts the application.
Application::Run( gcnew Form1 );
}
public class Form1 extends Form
{
/** @attribute STAThread()
*/
public static void main(String[] args)
{
// Start the application.
Application.Run(new Form1());
} //main
private Button button1;
private ListBox listBox1;
public Form1()
{
button1 = new Button();
button1.set_Left(200);
button1.set_Text("Exit");
button1.add_Click(new EventHandler(button1_Click));
listBox1 = new ListBox();
this.get_Controls().Add(button1);
this.get_Controls().Add(listBox1);
} //Form1
public void button1_Click(Object sender, System.EventArgs e)
{
int count = 1;
// Check to see whether the user wants to exit the application.
// If not, add a number to the list box.
while ((MessageBox.Show("Exit application?", "",
MessageBoxButtons.YesNo).Equals(get_DialogResult().No))) {
listBox1.get_Items().Add(new Integer(count));
count += 1;
}
// The user wants to exit the application.
// Close everything down.
Application.Exit();
} //button1_Click
} //Form1