Results 1 to 2 of 2

Thread: [RESOLVED] [02/03] Supressing Ctrl-M in TextBox

  1. #1

    Thread Starter
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Resolved [RESOLVED] [02/03] Supressing Ctrl-M in TextBox

    I had someone else bring this to me this morning. I found a VS2005 solution but it didn't work in VS2003 that they're using in their project. I don't have VS2003 installed on my system so I wanted to see if anyone out there had a solution.

    Here's the situation.

    The task is to add a comments hotkey to an existing VS2003 app. The users want to use Ctrl-M to insert a pre-defined partial comment into a multi-line textbox. The following code was placed in the KeyUp event of the textbox

    vb Code:
    1. If e.Control = True And e.KeyCode = Keys.M Then
    2.     txtComment.Text = txtComment.Text & " " & CurrentUserProfile.DefaultComment
    3.     e.Handled = True
    4. End If

    The comment appears as it should but a carriage return is added to the beginning of it.

    I can surpress this in VS2005 by moving the code to the KeyDown event and adding the following line

    vb Code:
    1. e.SuppressKeyPress = True

    But this property isn't available in VS2003 from what I understand.

    So, is there an easy way to supress this CR in VS2003?

    We can probably talk them into using Alt-M for their macro if we have to. It's just that they have another, 3rd party, program that uses Ctrl-M for this and they would like it to be consistant if we can do it that way. Also, they want the field blank when the form is loaded which eliminates loading the default comment on Form_Load as an option.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] Supressing Ctrl-M in TextBox

    vb Code:
    1. Private ctrlM As Boolean = False
    2.  
    3. Private Sub TextBox1_KeyDown(ByVal sender As Object, _
    4.                              ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    5.     If e.KeyCode = Keys.M AndAlso _
    6.        e.Control AndAlso _
    7.        Not e.Shift AndAlso _
    8.        Not e.Alt Then
    9.         Me.TextBox1.AppendText("This is a comment.")
    10.         Me.ctrlM = True
    11.     End If
    12. End Sub
    13.  
    14. Private Sub TextBox1_KeyPress(ByVal sender As Object, _
    15.                               ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    16.     If e.KeyChar = ControlChars.Cr AndAlso Me.ctrlM Then
    17.         e.Handled = True
    18.         Me.ctrlM = False
    19.     End If
    20. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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