Implementing ?: operators with DialogResult Boxes C#

27 views Asked by At

I have this simple piece of code that focuses a text field if the user hits yes on a DialogResult, or exits the application if s/he hits no.

DialogResult dialogResult = MessageBox.Show("Client Not Verified\n" + " " + txtUserName.Text + " " + "Already Taken.\n Try Again?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Yes)
{
    txtUserName.Focus();
    return;
}
else
{
    Application.Exit();
}

how I can write the code with the same output by using the ?: operators? so it would look something like this:

dialogResult == DialogResult.Yes ? txtUserName.Focus : Application.Exit();
0

There are 0 answers