MessageBox
MessageBox.Show(this, "You are doing great!", "My Title",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Show() returns a DialogResult enum, which indicates what button was pressed
DialogResult result = MessageBox.Show(this, "Are you crazy?", "My Title",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
// Yes clicked
else if (result == DialogResult.No)
// No clicked
Options,
which will hold the app's options
public class Options
{
public enum LineType { Solid, Dashed, Dotted };
public LineType SelectedLineType { get; set; }
public bool IsLineVisible { get; set; }
}
OptionsForm
DialogResult
property to OK
DialogResult to
Cancel
Options field to OptionsForm, and make the constructor require an Options object
public partial class OptionsForm : Form
{
private Options options;
public OptionsForm(Options options)
{
InitializeComponent();
this.options = options;
// Make dialog box reflect options
visibleCheckBox.Checked = options.IsLineVisible;
switch (options.SelectedLineType)
{
case Options.LineType.Solid: solidRadioButton.Checked = true; break;
case Options.LineType.Dashed: dashedRadioButton.Checked = true; break;
case Options.LineType.Dotted: dottedRadioButton.Checked = true; break;
}
}
...
}
Click event handler for the OK button that
sets the Option object's properties to the selected options
private void OkButton_Click(object sender, EventArgs e)
{
// Make options reflect what was selected in dialog box
options.IsLineVisible = visibleCheckBox.Checked;
if (solidRadioButton.Checked)
{
options.SelectedLineType = Options.LineType.Solid;
}
else if (dashedRadioButton.Checked)
{
options.SelectedLineType = Options.LineType.Dashed;
}
else
{
options.SelectedLineType = Options.LineType.Dotted;
}
}
Options field to the main form's class and instantiate it in the form's constructor
public partial class Form1 : Form
{
private Options options;
public Form1()
{
InitializeComponent();
options = new Options { IsLineVisible = true, SelectedLineType = Options.LineType.Solid };
}
...
}
ShowDialog()
private void button1_Click(object sender, EventArgs e)
{
var optionsForm = new OptionsForm(options);
DialogResult result = optionsForm.ShowDialog();
// TODO: Check result
}
ShowDialog() returns back a
DialogResult, which will be OK
or Cancel, depending on which button was pressed (closing the
dialog box will also return Cancel)
if (result == System.Windows.Forms.DialogResult.OK)
{
if (options.SelectedLineType == Options.LineType.Solid)
{
label1.Text = "Solid";
}
else if (options.SelectedLineType == Options.LineType.Dashed)
{
label1.Text = "Dashed";
}
else
{
label1.Text = "Dotted";
}
label1.Visible = options.IsLineVisible;
}
FormBorderStyle = FixedDialog
MaximizeBox = False
StartPosition = CenterParent