Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27036

VS 2010 Strange code, how make it better?

$
0
0
Hi!

I stumbled upon this code in a project

Code:

private void TextBox_KeyDown(object sender, KeyEventArgs e) {
            var t = (TextBox)sender;

            if (t.IsReadOnly == true && e.Key != Key.F2) {
                t.Text = "";
                t.IsReadOnly = false;
            }

            if (e.Key == Key.F2) {             
                t.IsReadOnly = false;
                t.CaretIndex = t.Text.Length;
                t.Cursor = Cursors.IBeam;
                e.Handled = true;
            }
        }

I asked around and it seems like there is a requirement that a cell in a datagrid shoould get some "excel-like" behaviour. For example, when you select a cell and start type, the existing text disappear, and when you press f2 or double click, the cursor should be positioned at the end of the text.
Im a web guy myself and want to ask the experts here ifall this code is really needed, and if something can be done to make it look more "pretty", for example, is e.handle really needed and what about the if- statements?

kind regards
Henrik

Viewing all articles
Browse latest Browse all 27036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>