Lektion 18: lösningsförslag
- class MyForm : Form
- {
- private NumericUpDown temperatureBox;
- private NumericUpDown volumeBox;
- public MyForm()
- {
- TableLayoutPanel table = new TableLayoutPanel
- {
- RowCount = 3,
- ColumnCount = 2,
- Dock = DockStyle.Fill
- };
- Controls.Add(table);
- Label temperatureLabel = new Label
- {
- Text = "Temperature"
- };
- table.Controls.Add(temperatureLabel);
- temperatureBox = new NumericUpDown();
- table.Controls.Add(temperatureBox);
- Label volumeLabel = new Label
- {
- Text = "Volume"
- };
- table.Controls.Add(volumeLabel);
- volumeBox = new NumericUpDown();
- table.Controls.Add(volumeBox);
- Button button = new Button
- {
- Text = "Is This Habitable?",
- Dock = DockStyle.Fill
- };
- table.SetColumnSpan(button, 2);
- table.Controls.Add(button);
- button.Click += ClickedEventHandler;
- }
- private void ClickedEventHandler(object sender, EventArgs e)
- {
- double temperature = (double) temperatureBox.Value;
- double volume = (double) volumeBox.Value;
- bool isHabitable = temperature >= 18 && temperature <= 26 && volume <= 45;
- MessageBox.Show("Is Habitable: " + isHabitable);
- }
- }
- class MyForm : Form
- {
- public MyForm()
- {
- TableLayoutPanel table = new TableLayoutPanel
- {
- RowCount = 5,
- ColumnCount = 5,
- Dock = DockStyle.Fill
- };
- Controls.Add(table);
- for (int i = 1; i <= 25; i += 1)
- {
- Button button = new Button
- {
- Text = "Button " + i
- };
- table.Controls.Add(button);
- button.Click += ClickedEventHandler;
- }
- }
- private void ClickedEventHandler(object sender, EventArgs e)
- {
- Button button = (Button) sender;
- MessageBox.Show("You clicked: " + button.Text);
- }
- }