Results 1 to 4 of 4

Thread: Strange code, how make it better?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Strange code, how make it better?

    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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Strange code, how make it better?

    The code that you have is C#. This is how it'd look in vb.net:
    Code:
    Private Sub TextBox_KeyDown(sender As Object, e As KeyEventArgs)
    	Dim t As TextBox = DirectCast(sender, TextBox)
    
    	If t.IsReadOnly = True AndAlso e.Key <> Key.F2 Then
    		t.Text = ""
    		t.IsReadOnly = False
    	End If
    
    	If e.Key = Key.F2 Then
    		t.IsReadOnly = False
    		t.CaretIndex = t.Text.Length - 1
    		t.Cursor = Cursors.IBeam
    		e.Handled = True
    	End If
    End Sub
    That says that if the textbox is readonly and the key that was pressed wasn't F2 then set the text to String.Empty and set the textbox's readonly to false. If that first condition fails and the key is F2, then the textbox's readonly is set to false the Caret is set to the end of the text, the Cursor is the I beam, like a regular textbox, and a true value for Handled prevents most handlers along the event route from handling the same event again.

    Edit - The only thing different I'd do is: instead of having 2 if/then statements, I'd have an If/Then...ElseIf/Then...End If format:
    Code:
    If (condition1) Then
       'Matches condition1
    ElseIf (condition2) Then
       'Doesn't match condition1, but matches condition2
    Else
       'Doesn't match condition1 or condition2
    End If
    Last edited by dday9; Jun 7th, 2013 at 10:08 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Strange code, how make it better?

    @Henrik: Are you looking for a C# Solution or a VB.net Solution?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Strange code, how make it better?

    Quote Originally Posted by koolsid View Post
    @Henrik: Are you looking for a C# Solution or a VB.net Solution?
    Hi!

    Either would be fine, but I guess we can call the issue solved, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width