L
L
lens23142021-11-09 17:04:29
C++ / C#
lens2314, 2021-11-09 17:04:29

How to simplify this c# code?

if (e.KeyCode == Keys.A)
            {
                textBox1.Text = e.KeyCode.ToString();
            }
            if (e.KeyCode == Keys.B)
            {
                textBox1.Text = e.KeyCode.ToString();
            }
            if (e.KeyCode == Keys.C)
            {
                textBox1.Text = e.KeyCode.ToString();
            }

How can the code be lightened so as not to write every letter after Keys.Letter) ?
I saw somewhere using a for loop and signs < >

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-11-09
@lens2314

if(Keys.A <= e.KeyCode && e.KeyCode <= Keys.Z)
  textBox1.Text = e.KeyCode.ToString();

And where is the for loop to use - xs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question